I need to save some settings of my application.
I found this example Arduino Playground - HomePage, but I can't compile this code.
#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;
}
double testInt[12] = { -12.5, -10.00, -5.7, 0, 2.45, 2.90, 3.10, 4 , 5.6, 7.9, 5.5, 4};
byte noElem = 12;
unsigned int baseAddr = 0;
unsigned int n = 0;
void setup() {
Serial.begin(9600);
// write data to eeprom
for (int i=0; i <= noElem-1; i++){
n = EEPROM_writeAnything( (i*4)+baseAddr, testInt[i]);
}
// read data back
for (int i=0; i <= noElem-1; i++){
double val;
int addr = (i*4)+baseAddr;
n = EEPROM_readAnything( addr, val);
Serial.println(val);
}
}
void loop() {
}
Error is:
sketch_apr24a:-1: error: expected ',' or '...' before '&' token
sketch_apr24a:-1: error: ISO C++ forbids declaration of 'T' with no type
sketch_apr24a:0: error: 'T' has not been declared
There is some kind of bug with arduino IDE version 22 that prevents the templete from being compiled correctly. A work around is to put the templete in a tabbed window in the IDE and include it in your main sketch.
templete code in tabbed window named eetest.h :
#include <WProgram.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;
}
Main sketch code example:
#include <EEPROM.h>
#include "eetest.h"
struct config_t
{
long alarm;
int mode;
} configuration;
void setup()
{
Serial.begin(9600);
/*
configuration.alarm = 12345678; // comment out after inital eeprom written
configuration.mode = 1; // same
// Write the configuration struct to EEPROM
EEPROM_writeAnything(0, configuration); // comment out after first written
// above three code lines could be a function in main sketch to save something away.
*/
}
void loop()
{
// Read and print the contents of EEPROM
EEPROM_readAnything(0, configuration);
Serial.print("alarm = ");Serial.println(configuration.alarm);
Serial.print("mode = ");Serial.println(configuration.mode);
// A "do nothing loop" where your real working code would be
while(1)
;
}
template 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;
}
It shocks me that the EEPROM library only handles byte read/writes, when avr-libc provides a simple way to do the above code in ONE library call.
You can use the avr-libc call, just put the include in your sketch: #include <avr/eeprom.h>
But - both routines write 1 byte at a time, so keep this in mind. The Arduino's EE is only rated for 100,000 writes, you can use that up in a hurry if you aren't careful.