Streaming

Update: Version 4 posted!

Streaming C++-style Output with Operator <<

New users sometimes wonder why the “Arduino language” doesn’t provide the kind of concatenation or streaming operations they have become accustomed to in Java/VB/C#/C++, etc.

// This doesn't work in Arduino.  Too bad.
lcd.print("The button was pressed " + counter + " times");

Meanwhile, experienced programmers chafe at having to synthesize streams with clumsy blocks of repetitive code like this:

lcd.print("GPS #");
lcd.print(gpsno);
lcd.print(" date: ");
lcd.print(day);
lcd.print("-");
lcd.print(month);
lcd.print("-");
lcd.println(year); // ugh!!

The Streaming library gives you the option of compressing those into “insertion style” code that, if not exactly the same, is reminiscent of the concatenation above:

lcd << "GPS #" << gpsno << " date: " <<
    day << "-" << month << "-" << year << endl;

This library works for any class that derives from Print:

Serial << "Counter: " << counter;
lcd << "Temp: " << t.get_temperature() << " degrees";
my_pstring << "Hi Mom!" << endl;

With the new library you can also use formatting manipulators like this:

Serial << "Byte value: " << _HEX(b) << endl;
lcd << "The key pressed was " << _BYTE(c) << endl;

This syntax is familiar to many, is easy to read and learn, and, importantly, consumes no resources.  (Because the operator functions are essentially just inline aliases for their print() counterparts, no sketch gets larger or consumes more RAM as a result of their inclusion.)

I hope someday that this simple template will become part of the Arduino core as a stylistic option to writing series of prints. In the meanwhile, I use it in every sketch I build now!

Download

The latest version of Streaming is available at Streaming4.zip.

Version

You can get the current version number of the library by inspecting STREAMING_LIBRARY_VERSION.

Page last updated on February 21, 2010 at 6:41 pm
5 Responses → “Streaming”

  1. Johann

    2 months ago

    Great library!

    Had a small issue to stream PStrings: Serial << myPString;

    error: passing ‘const PString’ as ‘this’ argument of ‘PString::operator const char*()’ discards qualifiers

    Removing the “const” in line29 of streaming.h seems to work
    inline Print &operator <<(Print &stream, const T arg)
    inline Print &operator <<(Print &stream, T arg)

    A bit less type checking, but not sure if it has memory implications…

    -kellerza


  2. Mikal

    2 months ago

    Very nice (and important) find. Thank you.

    I’ll be posting a new version shortly.

    Thanks again.

    Mikal

    [6 January: Version 3 of Streaming now addresses this problem.] — mh


  3. Soilboy

    3 weeks ago

    Hi Mikal

    Love the library, I use it as standard now. However I have come across an issue when using it in conjunction with your Flash library.
    I get the following type errors if I include both Flash.h AND Streaming.h:

    error: ‘endl’ was not declared in this scope

    when trying to do the following:

    Serial << data << endl;

    It’s probably something to do with the multiple of overloads of << but I’m no expert. I was hoping you may be able to shed some light on the issue.

    Cheers

    Soilboy


  4. Mikal

    3 weeks ago

    Soilboy –

    Thanks for the feedback. Yes, there are one or two interdependencies between Flash and Streaming that I need to sort out, but for now the solution is to #include Streaming.h BEFORE Flash.h.

    Thanks for the kind comments! I use that Streaming library in every sketch myself! :)

    Mikal

1 Trackbacks For This Post
  1. New Streaming Library | Arduiniana

    [...] Streaming [...]

Leave a Reply