Pages: [1]   Go Down
Author Topic: Streaming library - now that's cool!  (Read 1505 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't know you could do this ... I saw it in another post recently:

Code:
#include <Streaming.h>

void setup ()
{
Serial.begin (115200);

int a = 42;
char b = 'x';
char * c = "swordfish";
byte d = 100;

Serial << "Testing: " << a << " " << b << " " << c << " " << d << endl;
Serial << "Different bases:" << endl;

Serial << "Hex: " << _HEX (a) << endl;
Serial << "Octal: " << _OCT (a) << endl;
Serial << "Binary: " << _BIN (a) << endl;
Serial << "Byte: " << _BYTE (a) << endl;
}

void loop () {}

Outputting lots of different things to Serial without having to do lots of function calls is really useful.

Not only is that easier to read, it uses less memory! The more laborious way is to do it like this:

Code:
Serial.print ("Testing: ");
Serial.print (a);
Serial.print (" ");
Serial.print (b);
Serial.print (" ");
Serial.print (c);
Serial.print (" ");
Serial.println (d);

Serial.println ("Different bases:");

Serial.print ("Hex: ");
Serial.println (a, HEX);
Serial.print ("Octal: ");
Serial.println (a, OCT);
Serial.print ("Binary: ");
Serial.println (a, BIN);
Serial.print ("Byte: ");
Serial.println (a, BYTE);

Using the streaming library, the code takes 2394 bytes on a Uno, but using the non-streaming way it takes 2434 bytes, which is 40 bytes more.

I found the library here:

http://arduiniana.org/2009/04/new-streaming-library/

Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12937
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I vaguely recall that the streaming library uses more SRAM (in the form of stack space / local temporaries).
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting. I never got used to the C++ stream way of string output. I always use sprinft. You can control exactly what format you output and in how many digits, a must-have if you have limited space such as an lcd.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I vaguely recall that the streaming library uses more SRAM (in the form of stack space / local temporaries).

Examining the output from memoryFree returns the same value (1774) in both cases. I acknowledge that temporaries may use a few extra bytes, but I think that if you are that close to running out of memory that you cannot afford a few bytes of temporary stack space, you have quite big problems.

Code:
int memoryFree()
{
  extern unsigned long __bss_end;
  extern void *__brkval;
  int freeValue;  // currently bottom of memory

  if (__brkval == 0)
    return ((unsigned long)&freeValue) - ((unsigned long)&__bss_end);

  return ((unsigned long)&freeValue) - ((unsigned long)__brkval);

}//end memoryFree()
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting. I never got used to the C++ stream way of string output. I always use sprinft. You can control exactly what format you output and in how many digits, a must-have if you have limited space such as an lcd.

Indeed.

However it is more of a memory hog:

Sketch with this in it takes 3710 bytes of program memory:

Code:
char buf [100];
 
 sprintf (buf, "Testing: %i %c %s %i", a, b, c, d);
 Serial.println (buf);

But with the line below it takes 2338 bytes of program memory:

Code:
  Serial << "Testing: " << a << " " << b << " " << c << " " << d << endl;
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well,
I'll have to rewrite sprintf then. It's hard to change one's habits. Every day I go to my lecture hall, all my students sit almost exactly at their same seats. They even wait outside at their usual spots. Maybe it's "Ground Hog's Day" for me for the last semester. That could be why I was so tired the entire time. smiley-sleep
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12937
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Examining the output from memoryFree returns the same value (1774) in both cases

Excellent.  Thanks.
Logged

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool indeed! Thanks!

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Not only is that easier to read,
I think that's debatable.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, beauty is in the eye of the beholder, eh? So I won't debate that with you. smiley
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8531
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been using it for a while, you can insert functions that return a string as well

Code:
Serial << PadString (r.name, 8, '-');

to print a string padded with ----- to a width of 8 for example.

I far prefer

Code:
Serial <<  "Invalid IO registor (" << parms[0] << ") for the " << SERVER_PROCESSOR << endl;

to

Code:
Serial.print ("Invalid IO registor (");
Serial.print (parms[0]);
Serial.print (") for the ");
Serial.println (SERVER_PROCESSOR);

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Serial.print ("Invalid IO registor (");
Now all you have to figure is how to pipe it all through a spelling checker!   ;-)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8531
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

He he, my speling never was reel good. How's this

Serial.print ("Invalid IO regastor (");

______
Rob
« Last Edit: May 10, 2011, 05:06:15 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Even worse...
Logged

Pages: [1]   Go Up
Jump to: