printf and sprintf example

Status
Not open for further replies.

stevech

Well-known member
printf and thus sprintf are in the latest teensyDuino releases. See example below.
I did use the fancy asterisk in the formatting to compute field width at run time.
google printf for all the details on formatting control characters and syntax.
Library code is large, like 10K, but not much impact to the T3s.
Unfortunately, the printf/sprintf we have now seems to pull in support for floating point formatting even if it's not used.

EDIT: Serial.printf() takes the formatted text and sends it to the serial port as text. Don't do print() alone, as it might try to use stdout which isn't configured, by default.

Code:
void setup() {
  Serial.begin(9600);
}


void loop() {
  int n = 0;
  float fp = 0.0;
  char buf[80];
  while (1)  {
    delay(200);
    fp += 0.99999;
    Serial.println();
    Serial.print(++n);
    Serial.println(" hello");
    // now...
    Serial.printf("%3d 0x%.*X %5.5f %f %s\n", n, sizeof(int)*2, n, fp, sqrt(n), " printf");

    sprintf(buf, "%3d 0x%.*X %5.5f %f %s\n", n, sizeof(int)*2, n, fp, sqrt(n), " sprintf");
    Serial.print(buf);   
  }

}
 
Last edited:
While it does not support all the features of printf/sprintf, our version does work with the Teensy and not pull in all of the floating point libraries.

We are working on ARMbasic for Teensy, and got it starting to work over the holidays (not big college football fans here). To get that going we have ported our gcc tools to Teensy. In them you can find sources for printf (derived from some earlier open source integer versions that we added floating point support to, without calling of the std float libraries).

www.coridium.us/files/setupC.exe
 
PS, the size of our printf is < 2K, and only uses the lowest level float routines (mult, divide, add, sub ...). But it doesn't do * formatting, take a swing at adding it...
 
In terms of newlib based *printf functions, by default, the normal *printf functions do contain support for floating point (%f, %g, etc. which brings in the floating point emulator). There are also *iprintf functions in the library, that do not have support for floating point. So instead of calling sprintf, call siprintf, and instead of calling snprintf, call sniprintf: http://newlib.sourcearchive.com/documentation/1.14.0-2ubuntu1/siprintf_8c-source.html.

The AVR libraries used in the Arduino, tend to be built so that sprintf does not have the floating point support in it (and every so often you get complaints of people who want to print out floating point numbers), but the Arm libraries tend to be built defaulting to including floating point support.
 
While it does not support all the features of printf/sprintf, our version does work with the Teensy and not pull in all of the floating point libraries.

We are working on ARMbasic for Teensy, and got it starting to work over the holidays (not big college football fans here). To get that going we have ported our gcc tools to Teensy. In them you can find sources for printf (derived from some earlier open source integer versions that we added floating point support to, without calling of the std float libraries).

www.coridium.us/files/setupC.exe
Coridium BASIC will be fun on the Teensy 3.x, - but there are a number of I/O libraries to make wrappers for or some such.
 
Status
Not open for further replies.
Back
Top