-
-
Notifications
You must be signed in to change notification settings - Fork 40
core: serial: implement Serial.printf() #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4d462a3 to
80c019b
Compare
80c019b to
d0b1e5e
Compare
Hey!
I often find it useful to have aprintffunction in the Serial class, so I implemented it for this core too.
It's optional and turned off by default for all boards - but is explicitly enabled on the Nano Matter.
It can be turned on for any board withCONFIG_ARDUINO_SERIAL_PRINTF=yand the buffer size is also configurable.
Let me know what you think!
I will second that sentiment! Would expand that to all zephyr boards - probably add it to prj.conf.
@per1234
per1234
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to submit this pull request @silabs-bozont!
If this is to be done, it must be implemented in the Print class.
There is already a proposal for that here:
arduino/ArduinoCore-API#28
@per1234
That issue has been open since 2019 - thats going on 7 years. Doesn't look like its ever going to be addressed. Maybe should be addressed here for zephyr boards and if ever addressed for other boards can be deleted for here or excluded from print class for zephyr?
Just a thought.
Also just read in post 2 from Paul
Didn't Massimo (@mbanzi) say very clearly, only 2 weeks ago on the developers mail list, that this would not be accepted?
so not sure what is up!
KurtE
commented
Dec 22, 2025
@silabs-bozont - I totally agree with you on the desire to have printf.
I have a hacked up version that I use in my Zephyr test sketches.
https://github.com/KurtE/zephyr_test_sketches/blob/master/common/src/UARTDevice.cpp#L139-L146
Was going to mention that on the Teensy instead of needing a large buffer to use sprintf, instead:
Paul (PJRC) - uses vdprintf to do the actual output, and in the code he has a version of _write that he uses to redirect to
the Print object that called printf:
extern "C" {
__attribute__((weak))
int _write(int file, char *ptr, int len)
{
if (file >= 0 && file <= 2) file = (int)&Serial;
return ((class Print *)file)->write((uint8_t *)ptr, len);
}
}
It was also interesting that on MBED versions, we at times could cheat and use the macro:
REDIRECT_STDOUT_TO(Serial)
And then printf's would go to Serial (or whatever object you specified).
Other options is to use:
#include <LibPrintf.h>
If this is to be done, it must be implemented in the
There is already a proposal for that here: arduino/ArduinoCore-API#28
@per1234 - I personally think that Arduino should prune stuff out, like this, that are over N years old. Sort of similar that
Zephyr will close out PRs if no action has happened on them in so many months.
I know every so often I will go through some of my own PRs and Issues and close them out as detritus .
Hey!
I often find it useful to have a
printffunction in the Serial class, so I implemented it for this core too.It's optional and turned off by default for all boards - but is explicitly enabled on the Nano Matter.
It can be turned on for any board with
CONFIG_ARDUINO_SERIAL_PRINTF=yand the buffer size is also configurable.Let me know what you think!