join Ints to byte

Hi there!

I would like to chain

uint_32 val_a uint_32 val_b uint_8 val_c uint_8 val_d

to a 10 Byte Datablock for transmitting over I2C

byte Datablock[10];

Afterwards i have to do Bytes _to_int again , obtaining the same values on the other Board.

I already can do this with single values but have no idea how to build chains and splitting them up afterwards.

Thanks for help !

//serialize
int i; 
for (i=0; i<4; i++) {

   DataBlock[i] = (val_a >>(i*8)) & 0x000000FF); 
}


//deserialize

val_a = (DataBlock[3]<< 24) | (DataBlock[2]<< 16) | (DataBlock[1]<< 8) | (DataBlock[0]);

Haven’t compiled and you still have to put some effort to put two 32 bit variables, but it is fairly simple to create a function to do that.

Thanks.

That is just the point i already reached. My question starts just one step beyound..

I do not know how to handle/append the next value

I started Programming the day i registered to this forum..

I’m not sure , if shifting will work right in your case, as you have 10 byte variable (non standard), plus mixing in size 32 and 8 bit internal.
Look on-line C/C++ tutorial on: Union
http://www.cplusplus.com/doc/tutorial/other_data_types/

=(

Okidoki - i think i Understand how to sort the 10 Bytes recieved with union.That is not that difficult.

But i do not get how to do a union of my values to generate the requestable Bytestream.. Will this here join my Ints to DATABLOCK so i can send it ? struct DATA { uint8_t VAL_D; uint8_t VAL_C; int32_t VAL_B; int32_t VAL_A; };

union DATABLOCKUnion { struct DATA senddata; uint8_t bytes[10]; } DATABLOCK;

Probably, something like this :

Wire.send(DATABLOCKUnion.bytes[i]);

inside for - loop, count by i =0 to 10.

http://www.neufeld.newton.ks.us/electronics/?p=241

i wil try it this way for the Slave:

#include "Wire.h"

byte i2cData[10];
static uint8_t VAL_D;
static uint8_t VAL_C;              
static int32_t VAL_B;
static int32_t VAL_A;

void setup() {
Serial.begin(38400);
setup_readpins();
Wire.begin(0x60);
//Wire.onReceive(incoming);
Wire.onRequest(outgoing);
}

void loop() {
ValuegetF()
}
void outgoing() {
Wire.send(i2cData,10);
}

struct I2cPart {
    uint8_t VAL_D;
    uint8_t VAL_C;              
    int32_t VAL_B;
    int32_t VAL_A; 
  };
  
  union ValueUnion {
    struct I2cPart I2cData;
    uint8_t bytes[10];
  } DATABLOCK;

and for the Master:

  TWBR = ((16000000L / 400000L) - 16) / 2; 
  i2c_rep_start(0x60);                     
  
  for(uint8_t i = 0; i < 9; i++)
    i2cData.bytes[i]= i2c_readAck();
  i2cData.bytes[9]= i2c_readNak(); 

for (Datablockunionviceversatogetgetvalues bladibla.. );

Still a little bit curious about a onWireReceive() event i have to gather and the Adress of the Slave talked to by master