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 14th, 2012 → 8:35 pm
[…] Download the Streaming library. […]
October 25th, 2012 → 3:07 pm
[…] just found the Steaming library which allows you concatenate your strings using […]
February 15th, 2013 → 4:30 pm
[…] not ported Streaming Lib Maintainer Page […]
February 25th, 2013 → 9:38 am
[…] not ported Streaming Lib Maintainer Page […]
April 9th, 2013 → 3:32 pm
[…] Streaming http://arduiniana.org/libraries/streaming/ […]
May 22nd, 2013 → 5:36 am
[…] 或者用C++一样的流式输出,由MIKAL HART写的库: […]
June 17th, 2013 → 3:38 pm
[…] Here is an example that prints out the decoded hexadecimal number of the received IR signal (if possible), and the protocol that the IR signal used: First of all, install the Streaming library at http://arduiniana.org/libraries/streaming/. […]
August 3rd, 2013 → 9:53 pm
[…] is the sensor value. I know the << isn't normal Arduino stuff, it's part of the streaming library Reply With Quote vBulletin.events.SkimlinksActivate.subscribe(function() […]
September 2nd, 2014 → 11:57 pm
[…] will also need some additional Arduino libraries before the script will compile, namely Streaming, CmdMessenger and Encoder. Once theese are in your libraries folder you should be good to […]
December 15th, 2014 → 7:03 am
[…] Streaming: http://arduiniana.org/libraries/streaming/ […]
February 13th, 2015 → 3:34 am
[…] will also need some additional Arduino libraries before the script will compile, namely Streaming, CmdMessenger and Encoder. Once theese are in your libraries folder you should be good to […]