Hi guys,
I recently got my hands on a Adafruit Fram Breakout. I'm trying to store different data types using a struct. So far I have not been able to do it successfully.
There are many ressources talking about this subject but I have not seen one that give a clear and complete answer. So far I've tried this:
/*
31.12.2020
Based on : https://arduino.stackexchange.com/questions/76951/arduino-i2c-external-32kb-fram-data-organization
*/
#include <Wire.h>
#include "Adafruit_FRAM_I2C.h"
Adafruit_FRAM_I2C fram = Adafruit_FRAM_I2C();
uint16_t framAddr = 0;
struct program_data {
bool variable0;
byte variable1;
int variable2;
float variable3;
long variable4;
} data;
void setup() {
Serial.begin(9600);
fram.begin(0x50);
data.variable0 = 1;
data.variable1 = 102;
data.variable2 = 569;
data.variable3 = 3.14;
data.variable4 = 100000;
save_data(&data);
restore_data(&data);
}
void loop() {
}
void save_data(struct program_data *data_ptr) {
byte *ptr = (byte *)data_ptr;
for (size_t i = 0; i < sizeof(struct program_data); i++) {
fram.write8(i, ptr[i]);
}
}
void restore_data(struct program_data *data_ptr) {
byte *ptr = (byte *)data_ptr;
for (size_t i = 0; i < sizeof(struct program_data); i++){
Serial.print(fram.read8(i)); Serial.print(",");
}
Serial.println("");
}
And this is the result :
1,102,57,2,195,245,72,64,160,134,1,0,
This issue is probably the way I'm printing the struct. What should I change in order to print the values correctly ?
Ideally I would like to have it like this :
1,102,569,3.14,100000
Thanks your suggestions/ideas.