本帖最后由 Ricky 于 2014-3-24 10:10 编辑
对于复杂一些的代码,往往需要通过串口打印进行调试,下面的宏将方便调试过程,调试OK了,将#define _DEBUG 1 改为 #define _DEBUG 0即可,代码就变干净了。
1 调试宏:
#define _DEBUG 1 //1 = debug; 0 = normal
#if _DEBUG
#define DBG_BEGIN(baud) { Serial.begin(baud); while(!Serial); }
/*assert */
#define DBG_ASSERT(A) if( !(A) ) { Serial.print("ERR:F=");Serial.print(__FILE__);Serial.print(" L=");Serial.println(__LINE__); delay(1000); while(1);}
#define DBG_WARNING(A) if( !(A) ) { Serial.print("WARNING:F=");Serial.print(__FILE__);Serial.print(" L=");Serial.println(__LINE__); delay(500);}
/*print information*/
#define DBG_PRINT_FUNNAME { Serial.print("INFO:Fun=");Serial.println(__func__);} //put it into the begin of function, you can trace the flow of program
#define DBG_PRINT(info) { Serial.print(info);}
#define DBG_PRINTLN(info) { Serial.println(info);}
#define DBG_PRINTLN_VAR(var, type) { Serial.print(#var); Serial.print("=");Serial.print(var, type); Serial.print(" @"); Serial.println(type);}
#else
#define DBG_BEGIN
#define DBG_ASSERT(A)
#define DBG_WARNING(A)
#define DBG_PRINT_FUNNAME
#define DBG_PRINT(info)
#define DBG_PRINTLN(info)
#define DBG_PRINTLN_VAR(val, type)
#endif 复制代码
2、使用样例
int led = 13;
void setup() 复制代码
3、样例在debug状态下色输出
INFO:Fun=softBlink
period=300 @10
times=11 @2
ERR:F=Blink.ino L=50