Skip to content

Add printf to print class #780

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

Merged
merged 6 commits into from
Nov 26, 2019
Merged

Add printf to print class #780

merged 6 commits into from
Nov 26, 2019

Conversation

MCUdude
Copy link
Contributor

@MCUdude MCUdude commented Nov 18, 2019

This PR uses Adafruits printf implementation.
For those who didn't know, printf is super useful when you want to format your output. When printf is a part of the Print class, all libraries that inherits the print class (such as Serial, LiquidCrystal, SoftwareSerial + other LCD libraries).

This PR makes this possible:

Serial.printf("Milliseconds since start: %d\n", millis());

@fpistm fpistm self-requested a review November 19, 2019 20:07
@fpistm fpistm added the enhancement New feature or request label Nov 19, 2019
@MCUdude
Copy link
Contributor Author

MCUdude commented Nov 20, 2019

Any updates on this? I forgot to mention that this does not add any extra flash usage when not used in the user program.

@fpistm
Copy link
Member

fpistm commented Nov 20, 2019

@MCUdude
I will review that when got a free time slot.
I have to check Arduino compatibility and what is involved and potential regression if any.

@uzi18
Copy link

uzi18 commented Nov 20, 2019

does it support F() macro?

@MCUdude
Copy link
Contributor Author

MCUdude commented Nov 20, 2019

The current PR does not. However, I can add it. I will probably use the code from the Teensy core files, since this supports the F() macro as well.

If you and @fpistm want, I can test this code and update the PR with these changes afterward.

@uzi18
Copy link

uzi18 commented Nov 20, 2019

nice to have possibility to redefine buf size

@MCUdude
Copy link
Contributor Author

MCUdude commented Nov 20, 2019

Actually, the printf code from Teensy didn't work very well when printing simultaneously on all serial ports. Adafruit's code work well, so I'll stick with this.

It doesn't seems like the F() macro doesn't do much really. It calls the PSTR() macro, which is just a dummy. But sure, There's no problem supporting F macros inside printf, but it won't affect the memory usage.

@fpistm
Copy link
Member

fpistm commented Nov 22, 2019

This addition requires to be documented in the Wiki:

  • Using printf will add additional code space. See example hereafter.
  • Have to not be used in third party library else this library would not be compatible across arch.
  • Use an hidden temporary buffer which can be redefined 😕

Code example:

#include <Streaming.h>

const char * lorem = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

void setup() {
  Serial.begin(9600);
}

void loop() {
  static uint32_t i = 0;
  // 1
  Serial.print("Iter ");
  Serial.print(i++);
  Serial.print(" --> ");
  Serial.println(lorem);
  // 2
  Serial.printf("Iter %i --> %s\n", i++, lorem);
  // 3
  Serial << "Iter " << i++ << " --> " << lorem << endl;
}

Size:

  • Current usage (1):
Sketch uses 9192 bytes (1%) of program storage space. Maximum is 524288 bytes.
Global variables use 320 bytes (0%) of dynamic memory, leaving 130752 bytes for local variables. Maximum is 131072 bytes.
  • Print::printf usage (2)
Sketch uses 11600 bytes (2%) of program storage space. Maximum is 524288 bytes.
Global variables use 448 bytes (0%) of dynamic memory, leaving 130624 bytes for local variables. Maximum is 131072 bytes.
  • Streaming usage (3)
Sketch uses 9176 bytes (1%) of program storage space. Maximum is 524288 bytes.
Global variables use 320 bytes (0%) of dynamic memory, leaving 130752 bytes for local variables. Maximum is 131072 bytes. 

As you can see Streaming usage is the best way.

MCUdude and others added 3 commits November 22, 2019 18:19
Co-Authored-By: Frederic Pillon <frederic.pillon@st.com>
Co-Authored-By: Frederic Pillon <frederic.pillon@st.com>
Co-Authored-By: Frederic Pillon <frederic.pillon@st.com>
@MCUdude
Copy link
Contributor Author

MCUdude commented Nov 22, 2019

I tried the online Resolve and commit functionality for the first time. Hopefully it is OK now

@MCUdude
Copy link
Contributor Author

MCUdude commented Nov 26, 2019

Cool! Will this be available in release 1.8.0?

@fpistm
Copy link
Member

fpistm commented Nov 26, 2019

Cool! Will this be available in release 1.8.0?

Well thinking about that, I really not like the buffer.
I'm currently testing the usage of vdprintf.

@fpistm
Copy link
Member

fpistm commented Nov 26, 2019

I merge this and will provide a hardening to remove PRINTF_BUFFER drawback.

@fpistm fpistm merged commit 0b9a591 into stm32duino:master Nov 26, 2019
@fpistm fpistm added this to the 1.8.0🎄 🎅 milestone Nov 26, 2019
@MCUdude
Copy link
Contributor Author

MCUdude commented Nov 26, 2019

Sure! I do agree that having a hardcoded buffer isn't optimal, but at least it is a start. I'm not sure how relevant it is, but here's how it is implemented on 8-bit AVRs, without any hardcoded buffer.

@gigaj0ule
Copy link
Contributor

Where can I find vdprintf?

system32/Arduino_Core_STM32/cores/arduino/Print.cpp:267:16: error: 'vdprintf' was not declared in this scope; did you mean 'vwprintf'?

It tried to include <stdio.h> but still no luck.

@fpistm
Copy link
Member

fpistm commented Jun 20, 2022

Could you be more precise? What is your setup? Board selected? Core version? Arduino IDE version? Sketch?...

Because I never seen this issue. vdprintf is provided by newlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants