Forum Administrator
Cambridge, MA
Offline
Faraday Member
Karma: 8
Posts: 3532
|
 |
« Reply #15 on: January 11, 2009, 05:08:53 pm » |
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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #16 on: January 22, 2009, 04:23:45 pm » |
Done! http://www.arduino.cc/playground/Main/StreamingOutput Referred to by the code library: http://www.arduino.cc/playground/Main/GeneralCodeLibraryThank you, mellis. Mikal
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #17 on: February 11, 2009, 07:32:52 am » |
Hum, I tried to use this command at the top of my sketch: 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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #18 on: February 11, 2009, 08:12:27 am » |
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
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #19 on: February 11, 2009, 08:49:03 am » |
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.zipI 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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #20 on: February 11, 2009, 09:12:59 am » |
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!  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
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #21 on: February 11, 2009, 09:37:51 am » |
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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #22 on: February 11, 2009, 11:42:25 am » |
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 Serial << "My name is " << name; Mikal
|
|
|
|
« Last Edit: February 11, 2009, 11:43:31 am by mikalhart »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #23 on: February 11, 2009, 12:36:09 pm » |
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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #24 on: February 11, 2009, 12:52:17 pm » |
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.): //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
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #25 on: February 11, 2009, 01:00:51 pm » |
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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #26 on: February 11, 2009, 01:27:55 pm » |
That works fine for me. If you replace Serial << "My name is"; with Serial.print("My name is"); does that work? That error message leaves much to be desired. Mikal
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #27 on: February 11, 2009, 02:07:28 pm » |
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
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #28 on: February 11, 2009, 02:15:08 pm » |
What happens with the non-templated version? //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
Jr. Member
Karma: 0
Posts: 51
Developer of the MIDI library
|
 |
« Reply #29 on: February 11, 2009, 02:26:59 pm » |
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
|
|
|
|
|
|