Hello all
I am trying to write a debug routine.
There will be different levels of debugging as well as simple debugging messages and sensor values to debug.
The basic lay out with macros for the levels and so on work.
The idea was now to have a inline function or a macro that would expand at function call. The function call should pass the debug macro as well as the debug message or sensor value.
The version by macro didn't work at all. The inline function returned "expected primary-expression before 'if'" error for the debug macro which works fine without inline function.
Version with inline function. As I didnt know by which datatype to pass the macro I opted for template for both parameters passed to the function
#define DEBUG_SENSOR false
#define DEBUG_SENSOR_SERIAL if(DEBUG_SENSOR)Serial
#define DEBUG_ERROR true
#define DEBUG_ERROR_SERIAL if(DEBUG_ERROR)Serial
#if DEBUG_SENSOR == true || DEBUG_ERROR == true
#define DEBUG true
#else
#define DEBUG false
#endif
#define DEBUG_SERIAL if(DEBUG)Serial
const unsigned int debugOutputInterval = 1000;
unsigned long debugOutputTimestamp;
const unsigned long currentMillis;
template<typename T> inline void debugInterval (const T &debugSerial, const T &debugMsg)
{
if ( currentMillis - debugOutputTimestamp >= debugOutputInterval )
{
debugOutputTimestamp = currentMillis;
debugSerial.println(debugMsg);
}
}
void setup()
{
DEBUG_SERIAL.begin(115200);
#if DEBUG == true
while (!Serial);
#endif
}
void loop()
{
const unsigned long currentMillis = millis();
#if DEBUG_ERROR == true
debugInterval(DEBUG_ERROR_SERIAL, "Hello");
#endif
}
This would be the macro that was replaced by the inline function
#define DEBUGINTERVAL(x, y)\
do { if ( currentMillis - debugOutputTimestamp >= debugOutputInterval )\
{ debugOutputTimestamp = currentMillis;\
(x).println((y))} while(false)
How could I improve the code to make it work?
Thanks you!