Pages: [1]   Go Down
Author Topic: How to save structure in EEPROM  (Read 579 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all

I need to save some settings of my application.
I found this example http://arduino.cc/playground/Code/EEPROMWriteAnything, but I can't compile this code.
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:
Quote
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

Can you give some advices to make this code work?

Happy debugging,
Johhik
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

.
I found this example http://arduino.cc/playground/Code/EEPROMWriteAnything, but I can't compile this code.

...Can you give some advices to make this code work?


Some explanation here:
http://arduino.cc/forum/index.php/topic,56627.msg406522.html#msg406508


Regards,

Dave
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

Code:
#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:

Code:
#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)
     ;
}
 

Lefty
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 8
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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;
}

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.

Code:
eeprom_write_block((const void*)&value,(void*)ee,sizeof(value));
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

William
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The Arduino's EE is only rated guaranteed for 100,000 writes

The guarantee includes a retention time.  Atmel makes no mention of "only rated".
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 8
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Right, so actually eeprom_update_block is even better.
Logged


Pages: [1]   Go Up
Jump to: