Pages: 1 [2] 3 4   Go Down
Author Topic: No cost streams  (Read 5146 times)
0 Members and 1 Guest are viewing this topic.
Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool.  But please do post this to the playground (or somewhere).  Because while it's good to have a small, simple, consistent core, it's also good to have an varied, easily accessible selection of more advanced (or just different) things for people to use.  I added a reference to the playground page from the Reference in the hopes that it would make it easier for people to find and share useful code that doesn't necessarily fit in the core.  Other suggestions for helping capture this sort of thing are very welcome.
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Done!  http://www.arduino.cc/playground/Main/StreamingOutput

Referred to by the code library: http://www.arduino.cc/playground/Main/GeneralCodeLibrary

Thank you, mellis.

Mikal
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hum, I tried to use this command at the top of my sketch:

Code:
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }

But it gives me this error:

error: expected initializer before '&' token

I'm not using the Arduino core, but the Sanguino one (based on the ATmega 644p). Do you have any idea where the problem can be? I'm not quite used to this level of programming...
Logged


Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your whole sketch?  It must be something about the context.  The Print class wasn't supported until Arduino 0012; it's probably not available in your version.

Mikal
« Last Edit: February 11, 2009, 08:21:04 am by mikalhart » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The sketch itself is empty (just to test the compilation process).

I'm using Arduino 12 IDE, with this core:

http://frankyfuzzfire.free.fr/DIY/Arduino/Sanguino.zip


I tried with the classic Arduino core (ATmega 168), the sketch compiles well. It seems that it's just this core that does not work.
Logged


Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting!  I looked at the Sanguino.zip, and not only is the Print class supported, but someone has already added my original suggestion for streaming support! smiley

Code:
   inline Print &operator <<(char arg) {print(arg); return *this;}
    inline Print &operator <<(const char *arg) {print(arg); return *this;}
    inline Print &operator <<(uint8_t arg) {print(arg); return *this;}
    inline Print &operator <<(int arg) {print(arg); return *this;}
    inline Print &operator <<(unsigned int arg) {print(arg); return *this;}
    inline Print &operator <<(long arg) {print(arg); return *this;}
    inline Print &operator <<(unsigned long arg) {print(arg); return *this;}

You may be able to use << style streaming without doing anything!

Mikal
« Last Edit: February 11, 2009, 09:14:24 am by mikalhart » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, this person is me! And it did not work..

But this piece of code is not present in the arduino core, and it works with this this core.. I'm lost.


Print class supported: you mean that I don't need to copy the <template> line at the top of the sketch, and that I can use directly the operator << ?

It does not work:

 In function 'void loop()':
error: no match for 'operator<<' in 'Serial << "Some data" '
« Last Edit: February 11, 2009, 09:40:50 am by Franky » Logged


Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If your core contains Print.h and Print.cpp you should be able to use <<.  Originally I thought you had to add the 7 lines to Print.h, but later I realize that inserting the (single) template line at the top of your sketch would work just as well without having to change Print.h.

I doubt if you can do both, so I'd remove the code you added to Print.h and just use the single template line.

With that you should be able to do

Code:
Serial << "My name is " << name;

Mikal
« Last Edit: February 11, 2009, 11:43:31 am by mikalhart » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I did what you said, and it's still not working..

This core is special: there are 2 serial ports, Serial & Serial1, which can be used both in a sketch.. So there might be some modifications in the code that handle serial I/O...

Originally, the file Print.cpp was empty. On the code I gave you, I put the code of the Arduino core, nothing changed.
Logged


Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, looking more closely I see that your class HardwareSerial doesn't inherit from Print so none of the above is going to work.  But this might work for you (but only for Serial and Serial1, not for LCDs, etc.):

Code:
//alternative option for Franky's Sanguino
template<class T> inline HardwareSerial &operator <<(HardwareSerial &obj, T arg) { obj.print(arg); return obj; }

Mikal
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't worry about LCD, as mine works with serial port, I'll be able to modify my library once this stream trick works.. And with only 16 characters, I prefer to keep the control on what and where I display..

Here is what I have:

« Last Edit: February 11, 2009, 01:03:11 pm by Franky » Logged


Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That works fine for me.  If you replace

Code:
Serial << "My name is";

with

Code:
Serial.print("My name is");

does that work?  That error message leaves much to be desired.

Mikal
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, it works, very strange.. It looks like Serial needs to be instanciated somewhere else.. But it's already been in HarwareSerial if I'm right..
Logged


Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What happens with the non-templated version?

Code:
//alternative option for Franky's Sanguino
inline HardwareSerial &operator <<(HardwareSerial &obj, const char *arg) { obj.print(arg); return obj; }

void setup() {
  Serial.begin(38400);
  Serial << "My name is";
}

void loop() {
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Developer of the MIDI library
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

it compiles, but without the "const" before "char *"..

But fixing the arg type to char * makes it impossible to use with non-string variables, such as ints or bytes. Is there a generic type for this kind of case?
« Last Edit: February 11, 2009, 02:31:39 pm by Franky » Logged


Pages: 1 [2] 3 4   Go Up
Jump to: