I have attached a tiny library, MinPrintf, that provides simple printf style functionality for any class derived from the Arduino Print class. I include examples for Serial, SD.h , and SdFat.
I wrote MinPrintf as a prototype for a very complete version of printf that implements most of the C11/C++2013 standard. I found this tiny library useful for many apps since it uses very little flash/SRAM but has key printf functionality.
The printf format string can be stored in flash using the F() macro.
I have not included floating point since this always makes the library very large and is not too useful without precision support. You can still print floating point with the Arduino print(val, prec) member function.
I will post the large complete printf library later.
Here is a demo example comparing Arduino Print with MinPrintf
// Demo of MinPrintf
#include <MinPrintf.h>
// Serial MinPrintf object.
MinPrintf mpr(&Serial);
//------------------------------------------------------------------------------
// MinPrintf
void min_print() {
mpr.printf(F("time: %lu usec, adc: %u\n"), micros(), analogRead(5));
}
//------------------------------------------------------------------------------
// Arduino print.
void ArduinoPrint() {
Serial.print(F("time: "));
Serial.print(micros());
Serial.print(F(" usec, adc: "));
Serial.println(analogRead(5));
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
min_print();
ArduinoPrint();
}
void loop() {}
Here is the output:
time: 60 usec, adc: 1023
time: 884 usec, adc: 1023
MinPrintf20140419.zip (5.46 KB)