Skip to content
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

Add basic C++ output streaming support #2551

Merged
merged 10 commits into from
Sep 13, 2022

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Sep 9, 2022

Add some improvements to the Print class, with support from new String methods.

A full STL stream implementation would require considerable work, with iostream wrappers, etc.

This PR adds basic support to the Print classs for the << insertion streaming operator. Avoiding printf is generally a good idea, and strong C++ types can do a much better job.

I've started going updating the sample projects using this capability to improve code flow and readability, but not yet complete. Let's see what people think before going any further!

C++ streaming operation <<

Building output is commonly done like this::

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C, humidity: ");
  Serial.print(humidity);
  Serial.println("%");

We can now do this::

  Serial << "Temperature" << temperature << " °C, humidity: " << humidity << "%" << endl;

Number Printing

Examples::

  Serial.print(12, HEX);         // "c"
  Serial.print(12, HEX, 4);      // "000c"
  Serial.print(12, HEX, 4, '.'); // "...c"
  Serial.print(12);              // "12"
  Serial.print(12, DEC, 4);      // "0012"

Similar extensions are provided for String construction::

  Serial << "0x" << String(12, HEX, 8);       // "0x0000000c"
  Serial << String(12, DEC, 4, '.');          // "..12"

Field-width control

Supported via String methods::

  Serial << String(12).padLeft(4);          // "  12"
  Serial << String(12).padLeft(4, '0');     // "0012"
  Serial << String(12).padRight(4);         // "12  "
  Serial << String(12).pad(-4, '0');        // "0012"
  Serial << String(12).pad(4);              // "12  "

Strongly-typed enumerations

Use of enum class is good practice as it produces strongly-typed and scoped values.
Most of these are also provided with a standard toString(E) function overload.

This allows string equivalents to be printed very easily::

  auto status = HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED;
  auto type = MIME_HTML;
  Serial.print(type); // "text/html"
  // "Status: HTTP Version Not Supported, type: text/html"
  Serial << "Status: " << status << ", type: " << type << endl;
  // Status: 505, type: 0
  Serial << "Status: " << int(status) << ", type: " << int(type) << endl;

@mikee47 mikee47 changed the title Add basic C++ streaming support Add basic C++ output streaming support Sep 9, 2022
@slaff slaff added this to the 4.7.0 milestone Sep 11, 2022
@slaff slaff merged commit 2345c3e into SmingHub:develop Sep 13, 2022
@mikee47 mikee47 deleted the feature/stream-insertion branch September 13, 2022 06:33
@mikee47
Copy link
Contributor Author

mikee47 commented Sep 13, 2022

@slaff I didn't revise all the samples; what are your thoughts on that? Should we complete the job?

@slaff
Copy link
Contributor

slaff commented Sep 13, 2022

what are your thoughts on that? Should we complete the job?

@mikee47 sure, but only if you have time for it.

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

Successfully merging this pull request may close these issues.

2 participants