sprintf() not working correctly with Atemis Thing Plus

Having an issue with formatted printing in an Artemis Thing Plus app. Running this code snippet:

char displayDigits[17];

sprintf(displayDigits, “%10s%-15d%10s”, “First”, 123, “Last”);

Serial.println(displayDigits);

On an Arduino Due gives this result on the serial monitor, as expected:

" First123 Last "

Running the same code snippet on an Artemis Thing Plus gives this result:

“First123Last”

The fields (integer and string) do not get padded with spaces as expected.

Any ideas?

The library version 2.x.x for the Apollo3/Artemis is using the MBED ‘minimal-printf’. This is the default for MBED and saves space. The source can be found in the library : “2.2.1/cores/mbed-os/platform/source/minimal-printf”.

It has a number of limitations e.g. when using floating-point precision. It can not be changed that easily all is explained in https://github.com/sparkfun/Arduino_Apollo3/issues/278.

In that post however, is also a workaround that I had created to use a full version. It does require to make a change in the library. Look in the issue post near the end dated April 7.

Thanks for the response, Paul. I found the April 7 post, and followed those instructions, then recompiled successfully. I still get the same effect; using the %5d format string doesn’t pad the result to 5 characters. I also tried %6s for a 5 character string and it did not get padded with a space. Any other ideas?

Because with the setup instruction you only change it for ‘serial.printf’, not sprintf or any of the others. Attached I created an updated version for you that has a new function ‘Artemis_sprintf’.

Install this version and use the included sketch to demonstrate that it works.

printf.zip (14.9 KB)

That does indeed work as expected! Thank you for the help on this.