Including C libraries that have printf and sprintf statements

Is there a simple, easy way to modify an Arduino ino file to use existing C libraries that have printf or sprintf statements, without the need to replace those printf and sprintf statements with Serial.print?

Maybe a setup command(s) that directs the sprintf string or the printf stdout to the Serial class.

My research so far has discovered formatting issues with printf and sprintf, but not an answer to my question.

Thanks.

sprintf() is available by default. There is no file system on Arduino, so no stdout, unless you use the workarounds in AVRlibc. In that case you can direct printf() to the serial port.

Google will show you how.

jremington:
sprintf() is available by default. There is no file system on Arduino, so no stdout.

Thanks. How do I modify the Arduino ino file to send sprintf to the Arduino USB UART?

One of many resources: Arduino Playground - Printf

not a direct answer to your question, but maybe it will be interesting for you

sthudium:
Is there a simple, easy way to modify an Arduino ino file to use existing C libraries that have printf or sprintf statements, without the need to replace those printf and sprintf statements with Serial.print?

Maybe a setup command(s) that directs the sprintf string or the printf stdout to the Serial class.

My research so far has discovered formatting issues with printf and sprintf, but not an answer to my question.

Thanks.

This method is used by Teensy, it works as is with AVR's. No FP support as expected.

#include "printf.h"

void setup() {
  Serial.begin(115200);
  printf_begin();
}

void loop() {
  char s[40];
  printf("%x %i %s\n",0xDB,47,"string1");
  sprintf(s,"%x %i %s\n",0xEE,250,"string2");
  Serial.print(s);
}

We don't know what core is being used. It could be AVR but it might not be.
The i/o hooks needed to get a printf() to work for each core are different.

sthudium,
what Arduino core are you using?

--- bill

bperrybap:
sthudium,
what Arduino core are you using?

Hi bperrybap, I'm using a Zero. But, I would prefer a universal sol'n.

sthudium:
Hi bperrybap, I'm using a Zero. But, I would prefer a universal sol'n.

This library uses standard LIBC functions so it should work with any Arduino microcontroller (although I have not tried it with a Zero). It does work on an Arduino DUE (SAM3X8E based) and, of course, any AVR.

Note that you can also greatly improve the PRINT functions of ANY Arduino with this library: https://github.com/krupski/Print

This library fixes some mistakes in the stock Arduino print as well as adding extra functions to support printing from flash or eeprom, and sends a "CR/LF" sequence for printing the variable type CHAR which allows PRINT (and PRINTF) to use the standard "string_of_text\n" sequence rather than "string_of_text\r\n" currently required.

Note that the CR is only added for the CHAR type... other writes go straight through as they are supposed to.

krupski:
https://github.com/krupski/Stdinout

This library uses standard LIBC functions so it should work with any Arduino microcontroller (although I have not tried it with a Zero). It does work on an Arduino DUE (SAM3X8E based) and, of course, any AVR.

The examples in Stdinout don't compile for me using IDE 1.8.5 on any core I tried, AVR, SAM, SAMD, chipkit, esp8266.
There are issues with the use of Stream vs Print class.
Even after the compile issues are resolved, I'd be surprised if it would work on anything but AVR given it ties into the AVR specific fdev stuff.

While I have gotten printf() to work on various cores in the past, it required doing core specific things and creating core specific i/o functions.

--- bill