Streaming
Streaming C++-style Output with Operator <<
Note: Streaming 5 is now Arduino 1.0 compatible.
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 Streaming5.zip.
Version
You can get the current version number of the library by inspecting STREAMING_LIBRARY_VERSION.
April 17th, 2009 → 7:27 pm
[…] Streaming […]
May 4th, 2010 → 7:18 am
[…] SSC32 library uses the Arduiniana Streaming library. Be sure to download and install it into your sketches/libraries/ […]
July 22nd, 2010 → 1:18 pm
[…] were used; PStringto handle parsing the incoming website, Flash for memory management, and Streaming for easier coding. Source:Sean Carney’s Weather Clock/ Please post a comment or leave a […]
July 14th, 2011 → 9:02 pm
[…] Streaming C++ style Output – print and println gets to be a pain after a while […]