A function to handle multiple datatypes

Multiple functions. If you have problems post your code.

However I don't have any objections to templates, which, in effect, produce multiple functions.

How do templates work? I have tried googleing but I can't seem to find a nice concise guide.

Yes. Sometimes you do.

Equally, why do you think they built in void pointer?

Type checking is very useful, a great invention. Sometimes however, it is nice to be able to side-step it if it gets in your way. C allows that.

EEPROMWriteAnything uses templates. Here:

#include <Arduino.h>  // for type definitions
#include <EEPROM.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
	    EEPROM.write(ee++, *p++);
    return i;
}

The templated type T is used to write to EEPROM.

pico:
Type checking is very useful, a great invention. Sometimes however, it is nice to be able to side-step it if it gets in your way. C allows that.

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

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

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

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. :roll_eyes:

pico:
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? =(

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

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

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

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

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

pico:
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. :roll_eyes:

What is this assembly language of which you speak?

While you were fixing dinner I was making a sandwich.

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

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

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

michael_x:
Arduino is famous for NoErrorHandling Software ...

So is Microsoft.

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

michael_x:

[quote author=Nick Gammon link=topic=130304.msg980369#msg980369 date=1351850852]

      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".
[/quote]

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

      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?

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

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