Replies: 2 comments
-
That's a better way definitely, thanks! I tried initially with |
Beta Was this translation helpful? Give feedback.
-
I just found a problem with the above, printf and vsnprintf return int:
https://en.cppreference.com/w/c/io/vfprintf So I am modifying my Print wrapper to check for a negative return from vsnprintf/vsnprintf_P or if write(const char*, size_t) has an error and return a negative values from my printf/printf_P. I found this when I tried to write a template for either vsnprintf or vsnprintf_P to support AVR PSTR():
I was mystified why it didn't work. Then I finally found the return problem. |
Beta Was this translation helpful? Give feedback.
-
This is not an issue, just information on how I handled a similar problem.
While looking at whether printf is implemented in various Arduino board packages I noticed this comment.
I recently had the same problem for a wrapper I wrote to add printf and printf_P to classes derived from Print.
Here is my solution modified to remove cut-n-paste from your version of Print. It's one way to avoid duplicate code.
Notice I used
va_copy
so arg is not used twice. I was surprised to learnva_lis
t is not a simple pointer to the stack in some implementations where arguments may be passed in registers and only one pass over the arguments will be allowed.See this for the recommended way to use va_copy in the future.
https://en.cppreference.com/w/cpp/utility/variadic/va_copy
Beta Was this translation helpful? Give feedback.
All reactions