I need to keep some minimal config data across power cycles... searching the forums and playground, i found the following which appears that it should do exactly what i need, essentially serialize a struct and stuff it in EEPROM for later (when it can be deserialized): Arduino Playground - EEPROMWriteAnything
However... I cannot figure out how to actually use it. Compiling the example as shown (pasted again below for posterity), i get:
test_eepromstruct:-1: error: expected ',' or '...' before '&' token
test_eepromstruct:-1: error: ISO C++ forbids declaration of 'T' with no type
test_eepromstruct:-1: error: 'T' has not been declared
The templating business is a little beyond me at the moment... i have not been able to figure out how to actually use the thing. Can someone please point me in the right direction?
code listing:
#include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
int i;
for (i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
return i;
}
template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
/* Once your sketch has these two functions defined, you can now save and load whole arrays or structures of variables in a single call. You provide the first EEPROM address to be written, and the functions return how many bytes were transferred.
*/
struct config_t
{
long alarm;
int mode;
} configuration;
void setup()
{
EEPROM_readAnything(0, configuration);
// ...
}
void loop()
{
// let the user adjust their alarm settings
// let the user adjust their mode settings
// ...
// if they push the "Save" button, save their configuration
if (digitalRead(13) == HIGH)
EEPROM_writeAnything(0, configuration);
}