Go Down

Topic: A function to handle multiple datatypes (Read 7110 times) previous topic - next topic

Chris Parish

I tried to compile a sketch including the code you posted and I get the following errors:

Code: [Select]

sketch_nov01a:2: error: expected ',' or '...' before '&' token
sketch_nov01a:2: error: ISO C++ forbids declaration of 'T' with no type


Using Arduino 1.0.1 on a Mac

pico

#16
Nov 02, 2012, 08:32 am Last Edit: Nov 02, 2012, 08:34 am by pico Reason: 1

If you buy a gun, you can shoot yourself in the foot. If you want to.


True. It really comes down to whether or not you have the skill to handle the gun. You have to know the limits of your competence.

So I guess you never do any assembly language programming at all then, huh? How dangerous is that? No type checking at all. Every line of code a virtual death trap.  :smiley-roll:
WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Chris Parish


True. It really comes down to whether or not you have the skill to handle the gun. You have to know the limits of your competence.


Wow. Well this could always be settled with a duel. Handbags at dawn perhaps?
However, in the mean time.... why won't my code work?  =(

Chris Parish

Well whist you guys were fighting I tried this and it seems to work. Thanks

Code: [Select]

template <class T> void sendAnything(const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++) {
      if (*p == 0x7E || *p == 0x7D) { //byte stuffing
        Serial.write(0x7D);
        Serial.write(*p++ ^ 0x20);
      } else {
        Serial.write(*p++);
      }
    }
}

Chris Parish

Just one other quick question:
How might I modify the above code to send the bytes MSB first?

Coding Badly


How do you expect that to work when you call sendAnything with a string?

Chris Parish

Oh yeah, duh.
As I said. Just finished a night shift. Not optimum time for writing code.....

Nick Gammon


So I guess you never do any assembly language programming at all then, huh? How dangerous is that? No type checking at all. Every line of code a virtual death trap.  :smiley-roll:


What is this assembly language of which you speak?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


Well whist you guys were fighting I tried this and it seems to work. Thanks


While you were fixing dinner I was making a sandwich.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

michael_x


How do you expect that to work when you call sendAnything with a string?


Well, it works somehow. You might rename it to sendAnyValueOrItsAddress ;)
Arduino is famous for  NoErrorHandling Software, so I'd max. add a comment in the library .cpp source code.

@Chris: rather look at the Serial.print() ( resp. hardware\arduino\cores\arduino\Print.h  / .cpp ) code
for a nice sample of function overloading.

Nick Gammon

Quote
Code: [Select]
      if (*p == 0x7E || *p == 0x7D) { //byte stuffing


You lost me on that line. Perhaps I should learn assembler?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


Arduino is famous for  NoErrorHandling Software ...


So is Microsoft.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

michael_x


Quote
Code: [Select]
      if (*p == 0x7E || *p == 0x7D) { //byte stuffing


You lost me on that line. Perhaps I should learn assembler?


It's plain good old C, and provides } as an escape character to avoid ever sending a tilde character ~ 0x7e
Don't ask my why, nor why some might consider this "fun".

Nick Gammon



Quote
Code: [Select]
      if (*p == 0x7E || *p == 0x7D) { //byte stuffing


You lost me on that line. Perhaps I should learn assembler?


It's plain good old C, and provides } as an escape character to avoid ever sending a tilde character ~ 0x7e
Don't ask my why, nor why some might consider this "fun".


Oh I see. It's so obvious now. So really:

Code: [Select]

      if (*p == '~' || *p == '}') { //byte stuffing
        Serial.write('}');


That would certainly stuff the bytes. Particularly if the data type was, say, an int.

And the point of this line?

Code: [Select]

        Serial.write(*p++ ^ 0x20);


Honestly, this is why you shouldn't fiddle with pointers if you don't understand what you are doing.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

michael_x

#29
Nov 02, 2012, 11:57 am Last Edit: Nov 02, 2012, 12:02 pm by michael_x Reason: 1
Quote
And the point of this line?

Nick, are you joking?

It's sending  a ~ as }^ and a real } as }]

It's allowing to insert meta data in the stream, marked by a ~ starting character.
Or add other special characters -- by prefixing them with } 

Go Up