template <class T>
void write_to_chunk_struct(size_t chunk, const T& value)
{
EEPROM.put(chunk,value);
Serial.println("printing after put struct");
for(int i = 0; i<size_of_eeprom;i++){
Serial.println(EEPROM.read(i));
}
}
After initialsing my eeprom class object, I am calling this function in my main as following:
eeprom_rotate eeprom1(50,10);//this is a class constructor
eeprom1.write_to_chunk_struct(0,configuration);
After writing, I read the EEPROM And the result is not quite as expected:
Value inside EEPROM 0 = 5
Value inside EEPROM 1 = 15
Value inside EEPROM 2 = 0 // why this empty byte?
Value inside EEPROM 3 = 0// why this empty byte?
Value inside EEPROM 4 = 0
Value inside EEPROM 5 = 2
Value inside EEPROM 6 = 0
Value inside EEPROM 7 = 0
Value inside EEPROM 8 = 0
Value inside EEPROM 9 = 1
Value inside EEPROM 10 = 0
Value inside EEPROM 11 = 0
As you can see, the first 2 values ( uint8_t ) are correct. But for some reason, before int, it leaves an empty gap of 2 bytes. I would expect my EEPROM to look something like:
Value inside EEPROM 0 = 5
Value inside EEPROM 1 = 15
//The following are 4 bytes for INT
Value inside EEPROM 2 = 0
Value inside EEPROM 3 = 2 //represents 512
Value inside EEPROM 4 = 0
Value inside EEPROM 5 = 0
//The following are 4 bytes for INT
Value inside EEPROM 6 = 0
Value inside EEPROM 7 = 1 //represents 256
Value inside EEPROM 8 = 0
Value inside EEPROM 9 = 0
Hello. In my case (programming ESP32) the int is definately 4 bytes. However, even if it was 2 bytes, it wouldnt explain why there are 2 empty bytes in EEPROM before the int.
Since I am reading the EEPROM in the same function right after I am doing EEPROM.put, I think that makes no difference. But you are right, It wont actually right to EEPROM without that, if the device restarts before the commit, the data wont be there.
Yes you are right. I am now unsure which method I should be using. I assume using packed struct would be more convenient since I know exactly where each data is placed based on the variables size. Because when I use EEPROM.get method to retrieve data from the EEPROM, I must know the exact length of the struct to be able to retrieve the correct data.
If the pack is not explicitly specified, optimise to merge the first two 1 byte of the struct (data is 8+8 bits and empty 16 bits) in an attempt to manage data in 32 bits.
Therefore, 32 bits(byte+byte+padding) + 32 bits(int) + 32 bits(int) occupy the memory.
If don't do this, the latter int is divided into two words and stored, which reduces performance.
Serial.print("struct after read = ");
Serial.println(configuration2.data1);
Serial.println(configuration2.data2);
Serial.println(configuration2.data3);
However, I am unsure about one more thing though. This is slightly out of topic but maybe you would know why do I must declare my struct as a reference in my function declaration?