Leotower 发表于 2014-3-23 15:33:02

分享:用于Arduino代码调试的宏

本帖最后由 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


nemon 发表于 2014-3-24 08:42:14
















Youyou 发表于 2014-3-25 10:21:33

很方便调试,收藏了。

Angelo 发表于 2014-3-28 11:41:24

样例程序似乎不太完整?
页: [1]
查看完整版本: 分享:用于Arduino代码调试的宏