Is there anything wrong with the following way of packaging float data in to byte arrays (for sending over communication links, eeprom storage...) and recovering them afterwards?
I've tested and it does work, with no change of value to the floats (even to far more decimal places than they are stored to) ater recovery. But I don't know whether there is something about using memmove functions and pointers in this way which is frowned upon, or not guaranteed to work in all circumstances.
I know I have seen warnings against trying to do this transfer of data betwen variable types by use of union, and other pointer related methods, even though they appear to work.
My code is below, it can be compiled and run as it is, it demonstrates taking an array of floats, putting them in to a byte array, then copying that byte array in to another bunch of floats to reconstruct the originals. It then edits the originals and the byte array to prove this doesn't modify the copied floats (proving it is copy by value, not reference). And then edits the copies to show this doesn't affect the originals. It also copies out one float to be stored in a non-array float.
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
//pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("Float to array test");
volatile float Test1[5]={23.4,3.14159,87.1,-1.0005,0.0046};
volatile float Test2[5]={0};
volatile uint8_t ByteSet[20]={0};
Serial.println("Initially");
Serial.println("T1: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test1[i],24);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("T2: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test2[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("BS: ");
for(uint8_t i=0; i< 20; i++){
Serial.print((int)ByteSet[i]);
Serial.print(", ");
}
Serial.println(" ");
for(uint8_t i=0; i< 5; i++){
float* pf = &Test1[i];
uint8_t* pb = &ByteSet[i*4];
memmove(pb,pf,sizeof(Test1[i]));
}
Serial.println("Copied in to bytes");
Serial.println("T1: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test1[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("T2: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test2[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("BS: ");
for(uint8_t i=0; i< 20; i++){
Serial.print((int)ByteSet[i]);
Serial.print(", ");
}
Serial.println(" ");
for(uint8_t i=0; i< 5; i++){
uint8_t* pb = &ByteSet[i*4];
float* pf = &Test2[i];
memmove(pf,pb,sizeof(float));
}
float Var3=0;
uint8_t* pb = &ByteSet[3*4];
float* pf = &Var3;
memmove(pf,pb,sizeof(float));
Serial.println("Copied back from bytes");
Serial.println("T1: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test1[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("T2: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test2[i],24);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("BS: ");
for(uint8_t i=0; i< 20; i++){
Serial.print((int)ByteSet[i]);
Serial.print(", ");
}
Serial.println(" ");
Serial.print("Var3 ");
Serial.println(Var3,12);
float Var33=Var3+10;
for(uint8_t i=0; i< 5; i++){
if(i<4){
Test1[i]=Test1[i+1]+2.3;
}else{
Test1[i]=Test1[i]-6;
}
}
for(uint8_t i=0; i< 20; i++){
ByteSet[i]=11;
}
Serial.println("Originals edited");
Serial.println("T1: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test1[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("T2: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test2[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("BS: ");
for(uint8_t i=0; i< 20; i++){
Serial.print((int)ByteSet[i]);
Serial.print(", ");
}
Serial.println(" ");
Serial.print("Var3 ");
Serial.println(Var3,12);
Serial.print("Var33 ");
Serial.println(Var33,12);
for(uint8_t i=0; i< 5; i++){
Test2[i]=5.0*(Test2[i]+100.0);
}
Var33=2.971;
Serial.println("Copies edited");
Serial.println("T1: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test1[i],12);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("T2: ");
for(uint8_t i=0; i< 5; i++){
Serial.print(Test2[i],24);
Serial.print(", ");
}
Serial.println(" ");
Serial.println("BS: ");
for(uint8_t i=0; i< 20; i++){
Serial.print((int)ByteSet[i]);
Serial.print(", ");
}
Serial.println(" ");
Serial.print("Var3 ");
Serial.println(Var3,12);
Serial.print("Var33 ");
Serial.println(Var33,12);
}
// the loop function runs over and over again forever
void loop() {
}
Thank you