user6635: 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!-

user4143: Have you thought of turning it into a small library?
libraries/StreamPrint/StreamPrint.cpp:
#include <StreamPrint.h>

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);
}

libraries/StreamPrint/StreamPrint.h:
#include <Arduino.h>

extern void StreamPrint_progmem(Print &out,PGM_P format,...);

#define Serialprint(format, ...) StreamPrint_progmem(Serial,PSTR(format),##__VA_ARGS__)
#define Streamprint(stream,format, ...) StreamPrint_progmem(stream,PSTR(format),##__VA_ARGS__)

Then in your code, wherever you want to use it:
#include <StreamPrint.h>

You can read more about writing libraries here: http://www.arduino.cc/en/Hacking/LibraryTutorial

6635: <p>I recently came across <a href="http://www.utopiamechanicus.com/article/low-memory-serial-print/" rel="nofollow">this</a> nice <code>SerialPrint()</code> implementation:</p>

<pre><code>void StreamPrint_progmem(Print &amp;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=&amp;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__)
</code></pre>

<p>I want to use it in my more complex projects to save precious RAM space.</p>

<p>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 <em>.cpp/</em>.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.</p>

<p>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).</p>

<p>Thanks in advance!-</p>
 4143: <p>Have you thought of turning it into a small library?</p>

<p>libraries/StreamPrint/StreamPrint.cpp:</p>

<pre><code>#include &lt;StreamPrint.h&gt;

void StreamPrint_progmem(Print &amp;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=&amp;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);
}
</code></pre>

<p>libraries/StreamPrint/StreamPrint.h:</p>

<pre><code>#include &lt;Arduino.h&gt;

extern void StreamPrint_progmem(Print &amp;out,PGM_P format,...);

#define Serialprint(format, ...) StreamPrint_progmem(Serial,PSTR(format),##__VA_ARGS__)
#define Streamprint(stream,format, ...) StreamPrint_progmem(stream,PSTR(format),##__VA_ARGS__)
</code></pre>

<p>Then in your code, wherever you want to use it:</p>

<pre><code>#include &lt;StreamPrint.h&gt;
</code></pre>

<p>You can read more about writing libraries here: <a href="http://www.arduino.cc/en/Hacking/LibraryTutorial" rel="nofollow">http://www.arduino.cc/en/Hacking/LibraryTutorial</a></p>
