Pages: 1 [2] 3 4 5   Go Down
Author Topic: A function to handle multiple datatypes  (Read 5732 times)
0 Members and 1 Guest are viewing this topic.
Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 830
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 02, 2012, 02:34:51 am by pico » Logged

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

Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  smiley-cry
Logged

Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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++);
      }
    }
}
Logged

Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 206
Posts: 12896
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Germany
Offline Offline
Faraday Member
**
Karma: 57
Posts: 3030
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Well, it works somehow. You might rename it to sendAnyValueOrItsAddress smiley-wink
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino is famous for  NoErrorHandling Software ...

So is Microsoft.
Logged


Germany
Offline Offline
Faraday Member
**
Karma: 57
Posts: 3030
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
      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".
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
      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:
      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:
        Serial.write(*p++ ^ 0x20);

Honestly, this is why you shouldn't fiddle with pointers if you don't understand what you are doing.
Logged


Germany
Offline Offline
Faraday Member
**
Karma: 57
Posts: 3030
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 } 
« Last Edit: November 02, 2012, 06:02:41 am by michael_x » Logged

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