Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« on: May 09, 2011, 10:11:52 pm » |
I didn't know you could do this ... I saw it in another post recently: #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: 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
Shannon Member
Karma: 116
Posts: 10130
|
 |
« Reply #1 on: May 09, 2011, 10:14:39 pm » |
I vaguely recall that the streaming library uses more SRAM (in the form of stack space / local temporaries).
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Online
Faraday Member
Karma: 35
Posts: 5913
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #2 on: May 09, 2011, 10:25:59 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: May 09, 2011, 10:29:41 pm » |
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. 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #4 on: May 09, 2011, 10:42:45 pm » |
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: 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: Serial << "Testing: " << a << " " << b << " " << c << " " << d << endl;
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Online
Faraday Member
Karma: 35
Posts: 5913
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #5 on: May 09, 2011, 11:07:17 pm » |
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. 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10130
|
 |
« Reply #6 on: May 09, 2011, 11:25:45 pm » |
Examining the output from memoryFree returns the same value (1774) in both cases Excellent. Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Jr. Member
Karma: 0
Posts: 77
Arduino System Go!
|
 |
« Reply #7 on: May 10, 2011, 01:51:14 am » |
Cool indeed! Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 136
Posts: 18995
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: May 10, 2011, 01:53:42 am » |
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.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: May 10, 2011, 01:55:50 am » |
Well, beauty is in the eye of the beholder, eh? So I won't debate that with you. 
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6803
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #10 on: May 10, 2011, 04:04:58 am » |
I've been using it for a while, you can insert functions that return a string as well Serial << PadString (r.name, 8, '-'); to print a string padded with ----- to a width of 8 for example. I far prefer Serial << "Invalid IO registor (" << parms[0] << ") for the " << SERVER_PROCESSOR << endl; to Serial.print ("Invalid IO registor ("); Serial.print (parms[0]); Serial.print (") for the "); Serial.println (SERVER_PROCESSOR);
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 136
Posts: 18995
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: May 10, 2011, 04:06:56 am » |
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.
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6803
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #12 on: May 10, 2011, 05:04:43 am » |
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
|
|
|
|
|
North Yorkshire, UK
Offline
Faraday Member
Karma: 104
Posts: 5531
|
 |
« Reply #13 on: May 10, 2011, 01:19:15 pm » |
Even worse...
|
|
|
|
|
Logged
|
|
|
|
|
|