/*
* Define a REAL printf since Arduino doesn't have one
*
* SerialPrintf() will automatically put the format string in AVR program space
*
* 2009 - bperrybap
*/
#include <avr/pgmspace.h>
#define SerialPrintf(fmt, ...) _SerialPrintf(PSTR(fmt), ##__VA_ARGS__)
#define NullPrintf(fmt, ...)
extern "C" {
int serialputc(char c, FILE *fp)
{
if(c == '\n')
Serial.write('\r');
Serial.write(c);
}
}
void _SerialPrintf(const char *fmt, ...)
{
FILE stdiostr;
va_list ap;
fdev_setup_stream(&stdiostr, serialputc, NULL, _FDEV_SETUP_WRITE);
va_start(ap, fmt);
vfprintf_P(&stdiostr, fmt, ap);
va_end(ap);
}
/*
* map Printf() to SerialPrintf()
* (Can't use printf() as the IDE uses it to include stdio.h and then our define would create problems. )
*/
#define Printf SerialPrintf
/*
* To use it for debugging define something like this:
*/
#define Debug SerialPrintf
/*
* And to disable it (will eliminate all the Debug() print calls)
*/
//#define Debug NullPrintf
void setup()
{
Serial.begin(9600);
}
double x = PI;
void loop()
{
static int loop;
Printf("%02d:%02d:%02d %s %d.%05d\n",1,2,3,"abcd",(int)(x),(long)(x*100000L)%100000L);
Debug("loop: %d\n", loop++);
delay(2000);
}