/***
eeprom_put example.
This shows how to use the EEPROM.put() method.
Also, this sketch will pre-set the EEPROM data for the
example sketch eeprom_get.
Note, unlike the single byte version EEPROM.write(),
the put method will use update semantics. As in a byte
will only be written to the EEPROM if the data is actually
different.
Written by Christopher Andrews 2015
Released under MIT licence.
***/
#include <EEPROM.h>
struct MyObject{
float field1;
byte field2;
char name[10];
};
void setup(){
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
for ( unsigned int i = 0 ; i < EEPROM.length() ; i++ )
EEPROM.write(i, 0);
Serial.print("EEPROM Length:");
Serial.println(EEPROM.length());
//Print the result of calling eeprom_crc()
Serial.print( "CRC32 of EEPROM data: 0x" );
Serial.println( eeprom_crc(), HEX );
Serial.print( "\n\nDone!" );
for(int i = 0; i < 5096; i++){
float f = 123.456f; //Variable to store in EEPROM.
/** Put is designed for use with custom structures also. **/
//Data to store.
MyObject customVar = {
3.14f,
65,
"Working!"
};
unsigned int startAddress =EEPROM.length() - sizeof(float)-sizeof(customVar)*2;
unsigned int eeAddress = startAddress; //Location we want the data to be put.
//One simple call, with the address first and the object second.
EEPROM.put( eeAddress, f );
//Serial.println("\nWritten float data type!");
//Serial.print("\nSize of Float: ");
//Serial.println(sizeof(float));
//Serial.print("\nSize of CustomVar: ");
//Serial.println(sizeof(customVar));
//Serial.println();
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.put( eeAddress, customVar );
//Serial.print( "Written custom data type! \n\n" );
//Serial.print( "Data data type!\n" );
//Serial.println("Second write of Custom Var!");
eeAddress += sizeof(customVar); //Move address to the next byte after float 'f'.
EEPROM.put( eeAddress, customVar );
f = 0.00f; //Variable to store data read from EEPROM.
eeAddress = 0; //EEPROM address to start reading from
EEPROM.get( eeAddress, f );
//Serial.println( EEPROM.get( eeAddress, f ) );
secondTest(); //Run the next test.
Serial.printf("Iteration %d, CRC %X, Start Address %d\n", i, eeprom_crc(), startAddress);
}
exit(0);
}
void loop(){ /* Empty loop */ }
void secondTest(){
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
MyObject customVar; //Variable to store custom object read from EEPROM.
EEPROM.get( eeAddress, customVar );
//Serial.println( "Read custom object from EEPROM: " );
//Serial.println( customVar.field1 );
//Serial.println( customVar.field2 );
//Serial.println( customVar.name );
EEPROM.get( eeAddress, customVar );
//Serial.println( "Read custom object from EEPROM: " );
//Serial.println( customVar.field1 );
//Serial.println( customVar.field2 );
//Serial.println( customVar.name );
}
unsigned long eeprom_crc( void ){
const unsigned long crc_table[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
unsigned long crc = ~0L;
for( unsigned int index = 0 ; index < EEPROM.length() ; ++index ){
crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4);
crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc;
}