It's just the syntax I'm stuck on - pointers and *s and so on.
I'm streaming data from a Mac to an Arduino byte by byte, but the data represents int32_t values. I can control the byte order myself to make things as simple as possible, and I can handle chopping a signed int32_t into 8-bit chunks correctly on the Mac. What's the fastest way to read them out, though (ie for an Arduino, not needing to be portable, not needing to reconstruct them into another variable)?
So, I have a buffer:
unsigned char buffer[BUFFER_SIZE];
and two pointers:
unsigned char headPtr;
int32_t tailPtr;
but I can't do this:
headPtr = buffer;
tailPtr = buffer; // no can do, dude, long * =/= char *
How do I tell the compiler I know what I'm doing without having to resort to reconstructing the int32_t with shifts and ORs?
Made the mistake of asking on a C++ forum and couldn't persuade anyone why it might be a desirable approach when time is tight and your processor's got lots to do in a short time and a small space... and most importantly, doesn't need to be portable to other architectures. Points taken, though, I'm trying to keep things much more clean and safe at the Mac end of things...
You could also use a union with MartT's approach. Suppose you know that there will be 8 longs coming in, which would be 32 bytes in the stream. Then, something like the following might work:
#define MAXBYTES 32
union {
long input[MAXBYTES / sizeof(long)];
char buffer[MAXBYTES];
} myUnion;
Then read the incoming data into myUnion.buffer[] as a byte stream and pull it out from myUnion.input[] as an array of longs.
A time-honored approach is to define a macro GETLONG(byteptr) that fetches and reassembles the bytes in the correct byte order (beware "endianness"!)
On an AVR, you can probably do something like #define GETLONG(p) (*((long *)byteptr), but in other cases it would be more complicated (if you have to fix endianness, or allow for unaligned memory accesses on a system that doesn't allow that.) By using the macro (or a function), you allow the complicated cases to be implemented without destroying the readability of the code.
I've gone for econjack's union suggestion for this project - no need to worry about pointers, I can just use standard array notation. Gonna have to come back and re-read everything once I'm a bit more * happy