View previous topic :: View next topic |
Author |
Message |
James Guest
|
Pointer / Multible Union Delcaration |
Posted: Wed May 31, 2006 12:08 pm |
|
|
Hello everyone,
I'm trying to set up a union (whatever works better) that always shows the data in my data[8] array but allows me to address the data differently depending on where it came from. That makes no sense, so for example:
If my data comes into my data[8] from spi, I know its going to be 3 bytes. But the last byte is bit mapped. So I want to be able to do some sort of
spidata.bitmapped1 = 0;
spidata.bitmapped6 = 0;
spidata.byte1 = 10;
but then later I'll be getting a message in from rs232 that will be 4 bytes, 2 8bit and 1 16bit in the middle.
rsr232data.byte1 = 255;
rsr232data.byte2 = 1000;
rsr232data.byte3 = 255;
I'd like to store all work in my data array (I'm only ever doing 1 thing at a time so I thought it would be a good idea to save space and only use 1 array).
I don't care if I address it all data.whatever or rs232data which would really point to &data. I just want to handle the data array differently depending on what it is coming in.
I tired this with stucts but had very little luck,
Is this a job for unions ? |
|
|
james Guest
|
Ummm.... This would work too |
Posted: Wed May 31, 2006 1:46 pm |
|
|
Ok, I've decided that since I really don't like the -> thing that you use with unions and I'm probably making this all more complex then it needs to be,
How about taking my data[8] and temporarily assigning a struct to it ?
Instead of Code: |
temp = (int16)data[0] << 8;
temp += data[1];
temp = temp / 43;
data[0] = temp >> 8;
data[1] = temp & 0xFF;
|
I could Code: |
struct _mystruct *data;
mystr.SignifigantlyNamedVariable /= 43;
|
And then have that int16 inside data be divd by 43.
Problem is &data is not &_mystruct so I can assign the values inside data to the struct, but when I change them it isn't refelected in data[]. Which is why I wanted to try unions.
I'm probably just confusing people. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Jun 01, 2006 2:55 pm |
|
|
This is a simple method of direct addressing variables in a fixed packet structure. This will compile very effectively.
Code: |
Int8 SPI_Data[8];
Int8 Var_0;
#locate Var_0 = SPI_Data+2
Int16 Var_1;
#locate Var_1 = SPI_Data+3
Int16 Var_3;
#locate Var_3 = SPI_Data+5
|
If you actually have a repeating structure you could benefit from declaring a structure and creating unions. |
|
|
JAMES! Guest
|
|
Posted: Fri Jun 02, 2006 1:36 am |
|
|
I can't beleive I didn't realize thats what I wanted all along!
Thank You Neutone !
One last thing tho. I did this:
Code: |
int data[8];
struct struct_spi {
int16 block1;
int16 block2;
int bitmapped;
};
struct struct_spi mySPIstruct
#locate mySPIstruct = data
|
Which works great! Except for one thing. The values in data[] come in the wrong endian. So, say my
data = 0x11,0x22,0x33,0x44,0x55
So I want:
mySPIstruct.block1 = 0x1122
mySPIstruct.block2 = 0x3344
mySPIstruct.bitmapped = 0x55
but ofcourse when I run this I get:
mySPIstruct.block1 = 0x2211
mySPIstruct.block2 = 0x4433
mySPIstruct.bitmapped =0x55
Is there a way to use locate and structs to solve this without swapping variables at runtime ? And not screwing up the order of data[] or any new structs that I locate in the same spot. |
|
|
|