Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Add function "printf" to AVR core class "print"#5938
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This "printf" realization not use "sprintf" and buffers in RAM. If it is not used, the compiler does not add any extra code.Exelent for AVR.
If this idea is promising, then I can write variants for ARM,ESP8266. |
trlafleur commentedMar 25, 2017
yes we need this. this is long over due.... |
Don't you need to pull the Also, it's worth pointing out that this won't work with Arduino's Other than that, I think this would be a useful addition. I have already proposed a variadic |
u48 commentedMay 18, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
It not need.
This will work with theString type, as usual, thes.c_str(). Theoretically, it is possible to make the printf accept an argument of the "String" type.
Classicprintf is more standard, and consumes less RAM, which is small. Moreover, it is all the same as outputting to the serial port, and on the LSD, TFT display and files theformatted output is almost always needed.
|
Where is the conflict? Added self-contained functions. Also i see also added "flush()". It's good. Ps: I and my friends use "printf" for a year, and we add it every time we update Arduino, and I did not notice any problems. |
My bad; I was not aware of that
My point was that this is not obvious so I was emphasizing that it would need to be clearly stated in the documentation. (Also it is not as newbie-friendly as passing it directly.)
Not directly at least.
Which reminds me, AVR's Also, apparently (Regarding size, I want to clarify that#5829 heavily relies on inlining so it won't use more memory than multiple calls to
I think we can all agree that, when it comes to efficient formatting,nothing beats However, I think#5829 is more convenient for the average user, but there's no reason to not merge both. |
Flush() - for libc.stream it is necessary toremove, and we do not use it. Buffering for the print () stream is not needed. |
ideaalab commentedMar 22, 2019
Is this going somewhere? |
No, it is not going anywhere, and you should not expect itever go anywhere. Massimo Banzi said very clearly on the Arduino developers mail list that Arduino does not believe the printf syntax belongs in the official Arduino API. |
ideaalab commentedMar 25, 2019
Thats a pity... And i can not understand why. It just adds functionality, that you can use or not... If you dont use it, then it should not even enlarge the compiled file. Simple print is ok for newbies and hobbyist... but its not elegant or professional to write in 10 lines of what it could be programmed in just single line with printf... |
This is a pity and makes little sense. I understand that Arduino aims for simplicity and that printf has a somewhat complex syntax, but I imagine many Arduino programmers may come from C and be used to printf syntax, so why not leave that as an option as well? Even if Arduino promotes the default Serial.print as the preferred one, it'd be nice to have Serial.printf as well, especially for C programmers. It gives a lot of flexibility and@u48's implementation seems very elegant. Alternatively, a syntax like Python's str.format() would be nice and simple enough, but that seems hard to implement.
Another alternative addressing that would be something like what I proposed in#5829 (IMO very clean and simple too), where you can just put multiple arguments to Serial.print() and they get concatenated, but that doesn't seem to be going anywhere either. |
PaulStoffregen commentedJun 3, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Are you really, honestly & genuinely asking this question?
Or is is merely rhetorical, where no answer will be considered as a valid reason? (what I've seen from absolutely everyone else who's talked as you do now) |
It was mostly rhetorical indeed (let's say about 70%), but more as a prompt for constructive debate and trying to make a point rather than an "I'm right and you'll never convince me otherwise" statement. I would also like to understand the possible arguments why adding Print.printf may be a bad idea (if the only reason is "the syntax is ugly and hard to document, and doesn't quite fit with the rest of the Arduino syntax" (which is an acceptable argument, don't get me wrong), I personally think the advantages may outweight the disadvantages). Re: Massimo Banzi having said very clearly that "Arduino does not believe the printf syntax belongs in the official Arduino API"; all he said (if I found the right mail) is that he would personally avoid it, but it didn't seem like a resounding "no" to me. (He also points out that sprintf can be used as an alternative if you really want to use printf syntax, but I think that's a bit complicated for people who just know enough C to use printf.) Maybe we should take this discussion to the mailing list instead? |
Related:arduino/ArduinoCore-API#28 |
If we'll ever decide that we cannot live without a |
Add to AVR core class "print":
size_t printf (const char *szFormat, ...);
size_t printf (const __FlashStringHelper *szFormat, ...);
This is does not use the "sprintf" and buffers in RAM.
If it is not used, the compiler does not add any extra code.
For AVR Excellent.
You can write more readable code:
Serial.printf (F ("Var = 5%"), Var);
Serial.printf ("Var = 5%", Var);
LCD.printf (F ("Var2 = 3% Var3=0x%04X"), Var2, VAR2);
On large projects, the overall code size is significantly reduced when using printf.
Note:
For ARM this is code not working.
For ARM need to use GNU extension "fopencookie" or a "retarget putsch" way.