hi,
I have a setup of 1 master and 2 slaves communicating in I2C.
The data received from slave 1 is saved into array 'b' and slave 2 into array 'c',
since array b and array c are the same size, I would like to make them into a matrics of 2 rows instead.
It'd great to have some insight in how to do it.
Here's my code using array b and array c
saving slave 1 data and slave 2 data into array b and array c respectively
static float DAC[2], thermistor[2];
char b[2*sizeof(float)];
char c[2*sizeof(float)];
int main() {
i2c.read(slave_addr[0],(char*) c,sizeof(c));
DAC[0] = *(float*)(c); // convert bytes back to float
thermistor[0] = *(float*)(c+sizeof(float));
i2c.read(slave_addr[1],(char*) b,sizeof(b));
DAC[1] = *(float*)(b); // convert bytes back to float
thermistor[1] = *(float*)(b+sizeof(float));
printf("DAC0:%.6f ,", DAC[0]);
printf("%.6f ,", DAC[1]);
printf("%.6f ,", thermistor[1]);
printf("%.6f \n", thermistor[0]);
}
my try to save data into a matrics instead, but seems like it's not working
save data into matrics
static float DAC[2], thermistor[2];
char b[2][2*sizeof(float)];
int main() {
i2c.read(slave_addr[0],(char*) b[0][0],sizeof(b[0]));
DAC[0] = *(float*)(b[0]); // convert bytes back to float
thermistor[0] = *(float*)(b[0]+sizeof(float));
i2c.read(slave_addr[1][0],(char*) b,sizeof(b[1][0]));
DAC[1] = *(float*)(b[1]); // convert bytes back to float
thermistor[1] = *(float*)(b[1]+sizeof(float));
printf("DAC0:%.6f ,", DAC[0]);
printf("%.6f ,", DAC[1]);
printf("%.6f ,", thermistor[1]);
printf("%.6f \n", thermistor[0]);
}
the second question is I'm using 4 lines to print the data, if I make it one line or add any words before %f, the program only prints 0s.
code only print zeros
//this will print all 0s
//like this
//DAC0:0.000000, DAC1:0.000000, thermistor0: 0.000000, thermistor1: 0.000000
printf("DAC0:%.6f, DAC1: %.6f, thermistor0: %.6f, thermistor1: %.6f", DAC[0], DAC[1], thermistor[0], thermistor[1]);
hi,
I have a setup of 1 master and 2 slaves communicating in I2C. The data received from slave 1 is saved into array 'b' and slave 2 into array 'c', since array b and array c are the same size, I would like to make them into a matrics of 2 rows instead. It'd great to have some insight in how to do it. Here's my code using array b and array c
saving slave 1 data and slave 2 data into array b and array c respectively
my try to save data into a matrics instead, but seems like it's not working
save data into matrics
the second question is I'm using 4 lines to print the data, if I make it one line or add any words before %f, the program only prints 0s.
code only print zeros