Arduino_STM32 icon indicating copy to clipboard operation
Arduino_STM32 copied to clipboard

Add method Print.printf(). Fixed & tested.

Open u48 opened this issue 6 years ago • 17 comments
trafficstars

Added the printf() method to the Print class.

With this implementation, Print.print() works with any devices - Uart, TFT, SDCard and others.

The previous version has not been tested. I'm sorry. This worked on other my platforms, but a bug was discovered on the Arduino STM32. Fixed.

u48 avatar Jan 25 '19 21:01 u48

Is there any way to add an additional format specifier for binary? I'd love to be able to use:

Serial.printf("bits=0b%08b\n", 0x33);

RickKimball avatar Jan 26 '19 15:01 RickKimball

Not tested, but this should work if you are not using nano.specs: https://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html

justinschoeman avatar Jan 26 '19 15:01 justinschoeman

you can choose one solution from here: https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format

stevstrong avatar Jan 26 '19 16:01 stevstrong

Thanks @justinschoeman! We learn something new everyday.

RickKimball avatar Jan 26 '19 17:01 RickKimball

Why does one of the the change to print.cpp include this block of comment ?

`/*
int Print::printf (__const char *__restrict __format, ...) //Bug detected { //failed: fopencookie( 0, "w+", { NULL, WR_fn, NULL, NULL }) FILE *__restrict __stream; //failed: fopencookie( 0, "rw+", { NULL, WR_fn, NULL, NULL }) int ret_status = 0; //

//worked: fopencookie( 0, "rw+", { RD_fn, WR_fn, NULL, NULL })

//worked: fopencookie( 0, "rw+", { NULL, WR_fn, NULL, NULL }) va_list args; // va_start(args,__format); //Resume: To fix - you should always specify the cookies_read function, zero is not valid. ret_status = vfprintf(__stream, __format, args); */`

As far as I can tell this code references the function

fopencookie

which does not exist

rogerclarkmelbourne avatar Jan 28 '19 08:01 rogerclarkmelbourne

Is there any way to add an additional format specifier for binary? I'd love to be able to use:

Serial.printf("bits=0b%08b\n", 0x33);

As implemented in LIBC. :) Need to check this.

u48 avatar Jan 28 '19 14:01 u48

As far as I can tell this code references the function fopencookie which does not exist

Hmm.. This is a LIBS extension defined in the header file studio.h. And provided by the compiler you are using.

This is supported in the native Linux gcc, Raspberry, gcc for Arduino DUE and ESP8286/32. But why is it disabled in the mingw. What compiler You use?

u48 avatar Jan 28 '19 15:01 u48

Why does one of the the change to print.cpp include this block of comment ? It's just to avoid questions why need an empty cookie_read_helper () function. And there was no attempt to remove it. Read and delete it.

u48 avatar Jan 28 '19 15:01 u48

I didn't test whether it compiled yet. I just wasnt aware that fopencookie was a function in the standard c libraries.

I'll locally merge in a separate branch and see whether I get any errors when I compile.

rogerclarkmelbourne avatar Jan 28 '19 21:01 rogerclarkmelbourne

OK. It looks like it fopencookie can be linked in.

But more testing would be needed before I can merge

rogerclarkmelbourne avatar Jan 28 '19 21:01 rogerclarkmelbourne

fopencookie () is an extension of LIBC.

The correct use of this extension is as follows:    #define _GNU_SOURCE    #include <stdio.h>

And not as it is now written in the source, only #include <stdio.h>

It is possible that such a trifle is a source of problems.

u48 avatar Jan 29 '19 07:01 u48

This PR does not seem to work for me.

Please post a small example sketch which works Also let me know if you have USB serial connected and what upload method you are using (as this affects the config of the USB Serial and Serial devices e.g. "Serial" can be either USB Serial or UART Serial depending on upload method

rogerclarkmelbourne avatar Feb 24 '19 07:02 rogerclarkmelbourne

I suspect that this is not compiled correctly or works. I take 1-4 days for tests.

I will do a few test sketches, and describe their behavior on various new platforms assembled from scratch. For experiments there is all the necessary and unnecessary hardware

u48 avatar Feb 25 '19 05:02 u48

OK.

Thanks

BTW. @RickKimball

Did this PR work for you ?

rogerclarkmelbourne avatar Feb 25 '19 05:02 rogerclarkmelbourne

Steve looks like the comments were cleaned up on February 28th, after your last comment.

victorpv avatar Jan 02 '20 19:01 victorpv

Maybe you could evaluate this: https://github.com/stm32duino/Arduino_Core_STM32/blob/master/cores/arduino/Print.cpp#L223-L228

fpistm avatar Jan 02 '20 19:01 fpistm

I think the solution from @fpistm should be applied, especially lines https://github.com/stm32duino/Arduino_Core_STM32/blob/42b2ab21373f6769cfc6a9b6ca03bf303fe9b4fa/cores/arduino/Print.cpp#L274-L290

stevstrong avatar Nov 03 '23 09:11 stevstrong