PDA

View Full Version : printf and sprintf example



stevech
01-04-2014, 04:29 AM
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.



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

}

ARMbasic
01-05-2014, 06:45 PM
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

ARMbasic
01-05-2014, 06:56 PM
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...

MichaelMeissner
01-05-2014, 09:27 PM
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.

stevech
01-06-2014, 12:46 AM
I have a small integer only printf. But on the T3.1, size, er, ah, sorta matters less. :p

stevech
01-06-2014, 12:47 AM
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.