Go Down

Topic: Streaming library - now that's cool! (Read 1 time) previous topic - next topic

Nick Gammon

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

Code: [Select]
#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: [Select]
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/


Coding Badly


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

liudr

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.

Nick Gammon


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: [Select]

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()

Nick Gammon


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: [Select]
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: [Select]
  Serial << "Testing: " << a << " " << b << " " << c << " " << d << endl;

liudr

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:

Coding Badly

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


Excellent.  Thanks.

Anders 2009


AWOL

Quote
Not only is that easier to read,

I think that's debatable.
"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.

Nick Gammon

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

Graynomad

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

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

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

I far prefer

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

to

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


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

AWOL

Quote
Serial.print ("Invalid IO registor (");

Now all you have to figure is how to pipe it all through a spelling checker!   ;-)
"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.

Graynomad

#12
May 10, 2011, 12:04 pm Last Edit: May 10, 2011, 12:06 pm by Graynomad Reason: 1
He he, my speling never was reel good. How's this

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

mowcius


Go Up