For debugging, it's handy to print debug messages to the serial port and track down problems. But if you want to trigger the debugging on and off it becomes annoying to comment, and uncomment those Serial.print and Serial.println statements. I wrote this macro that can help in triggering the debugging by simply changing the #define SERIAL_DEBUG_ENABLED value to 0 or 1. The macro code is :
#define SERIAL_DEBUG_ENABLED 1
#define GET_NUM_ARGS(...) GET_NUM_ARGS_ACT(__VA_ARGS__, 5,4,3,2,1)
#define GET_NUM_ARGS_ACT(_1,_2,_3,_4,_5,N,...) N
#define macro_dispatcher(func, ...) \
macro_dispatcher_(func, GET_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(func, nargs) \
macro_dispatcher__(func, nargs)
#define macro_dispatcher__(func, nargs) \
func ## nargs
#if SERIAL_DEBUG_ENABLED
#define DebugPrint(...) macro_dispatcher(DebugPrint, __VA_ARGS__)(__VA_ARGS__)
#define DebugPrintln(...) macro_dispatcher(DebugPrintln, __VA_ARGS__)(__VA_ARGS__)
#define DebugPrint2(str,modifier) \
Serial.print(millis()); \
Serial.print(": "); \
Serial.print(__PRETTY_FUNCTION__); \
Serial.print(' '); \
Serial.print(__LINE__); \
Serial.print(' '); \
Serial.print(str,modifier);
#define DebugPrint1(str) \
Serial.print(millis()); \
Serial.print(": "); \
Serial.print(__PRETTY_FUNCTION__); \
Serial.print(' '); \
Serial.print(__LINE__); \
Serial.print(' '); \
Serial.print(str);
#define DebugPrintln2(str,modifier) \
Serial.print(millis()); \
Serial.print(": "); \
Serial.print(__PRETTY_FUNCTION__); \
Serial.print(' '); \
Serial.print(__LINE__); \
Serial.print(' '); \
Serial.println(str,modifier);
#define DebugPrintln1(str) \
Serial.print(millis()); \
Serial.print(": "); \
Serial.print(__PRETTY_FUNCTION__); \
Serial.print(' '); \
Serial.print(__LINE__); \
Serial.print(' '); \
Serial.println(str);
#else
#define DebugPrint(...) macro_dispatcher(DebugPrint, __VA_ARGS__)(__VA_ARGS__)
#define DebugPrintln(...) macro_dispatcher(DebugPrintln, __VA_ARGS__)(__VA_ARGS__)
#define DebugPrint1(str)
#define DebugPrintln1(str)
#define DebugPrint2(str,modifier)
#define DebugPrintln2(str,modifier)
#endif
and in your code all you need to do is something like this:
DebugPrint("The value of x is:");
DebugPrintln(x,DEC);
I hope this helps, let me know if you have any suggestions.
Tarek