A tiny printf style library for Serial, SD.h, SdFat.

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)

Thanks, amazing difference! why is it so much faster? 1 call instead of 4 ?

why is it so much faster?

It isn’t faster. I just used micros for something to print so it is not a timing.

I do have a future enhancement to SdFat that will be 5-10 times faster for print. Much of that will be due to stdio style buffering. I will also use your fast divide by 10.

About 40 years ago stdio provided a huge speedup in Unix. The disk drives had a max seek time 0f 55 ms so the read/write system calls were very slow. I was at Berkeley when BSD Unix was developed on the PDP-11. I have been looking at back at a release of that system, looks crude now.

1 call instead of 4 ?

This is why I like the printf style API.

It isn’t faster. I just used micros for something to print so it is not a timing.

:blush:
So used to see timing results in your lib that I completely misinterpreted the output :wink: