I want to have a function debug_print(fmt, args) which takes format string, arguments and build a char buffer which I can then send to UART. Basically I could pass all this to scanf, but the problem is that for formatting float numbers needs special care.
dtostrf( float_num, 3, 4, temp_buf );
So I need to replace %f with %s in format string and change float arg to temp_buf. All that has to be done in minimum steps, because of limited embedded environment capabilities.
Would appreciate if anyone could help to build such function.
Something like this:
void debug_print(char *fmt, ... )
{
va_list args;
va_start(args,fmt);
// use uart function for output
va_end(args);
}
But how to effectively iterate through placeholders and how to substitute float arg to char*?
Instead of changing the format string, which will be impossible if the passed string is actually a literal string, when you find the
"%f"format-code you convert the next argument using yourdtostrffunction, storing the value in a temporary small string, then concatenate that to the output string (i.e.strcat).