I recently came across this nice SerialPrint()
implementation:
void StreamPrint_progmem(Print &out,PGM_P format,...)
{
// program memory version of printf - copy of format string and result share a buffer
// so as to avoid too much memory use
char formatString[128], *ptr;
strncpy_P( formatString, format, sizeof(formatString) ); // copy in from program mem
// null terminate - leave last char since we might need it in worst case for result's \0
formatString[ sizeof(formatString)-2 ]='\0';
ptr=&formatString[ strlen(formatString)+1 ]; // our result buffer...
va_list args;
va_start (args,format);
vsnprintf(ptr, sizeof(formatString)-1-strlen(formatString), formatString, args );
va_end (args);
formatString[ sizeof(formatString)-1 ]='\0';
out.print(ptr);
}
#define Serialprint(format, ...) StreamPrint_progmem(Serial,PSTR(format),##__VA_ARGS__)
#define Streamprint(stream,format, ...) StreamPrint_progmem(stream,PSTR(format),##__VA_ARGS__)
I want to use it in my more complex projects to save precious RAM space.
This works really well when I have just one *.ino file where I'm using the SerialPrint() function. When I try to use it in my more complext projects, which consist of a central *.ino files and several .cpp/.h files (mostly one cpp/h pair per class). The problem is if I put the code below into the *.ino file, it will not be known inside any of the *.cpp files. The compiler is not complanining, but I'm never seeing the serial output of my code.
Any ideas how to fix it? I've tried putting this code in a central global.cpp/h file which I include everywhere, but it does not seem to work (no output on serial).
Thanks in advance!-
printf()
?printf_P()
and the other _P variants.