Quantcast
Viewing all articles
Browse latest Browse all 4872

SDK • Re: troubletrouble defining a macro to replace a zero argument fn call: micros()

Code:

       #define DEBUG_PRINTF "Serial2.printf"
Those quotes are the source of your current problem, as it expands to:

Code:

"Serial2.printf"("hello world\n");
and hence the error that you are getting.
Can you suggest how I get a generalised DEBUG_PRINTF function call using inline fns?
Getting it is easy, problem is what to put inside it unless your Serial2 object has a vprintf() (or vfprintf()) in it.

Code:

static inline void DEBUG_PRINTF(const char *fmt, ...){    va_list ap;    va_start(ap, fmt);    vprintf(fmt, ap);    va_end(ap);}
You can kludge it with the generic vsnprintf() but then you need a temporary buffer of undefined size (admittedly, for a debug printf limiting the size probably doesn't matter, but it falls short of your original objective of just calling the target printf.

Another approach (untested) using a constant function pointer which should optimise away just like inline:

Code:

typedef void (*printf_ptr)(const char *fmt, ...);static const printf_ptr DEBUG_PRINTF =#if whateverSerial1.printf;#elseSerial2.printf;#endif
(obviously the typedef is unnecessary, you could declare the function pointer directly, but that makes it hard to read IMO).

Statistics: Posted by arg001 — Fri Nov 22, 2024 11:11 am



Viewing all articles
Browse latest Browse all 4872

Trending Articles