I am trying to use this EEPROM emulation library (GitHub - cmaglie/FlashStorage: A convenient way to store data into Flash memory on the ATSAMD21 and ATSAMD51 processor family and how to get an EEPROM like functionality ? - Arduino Zero - Arduino Forum) into my own library but it doesn't work.
I started with this small class:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <Arduino.h>
#include "FlashStorage.h"
typedef struct {
char id[12];
long up;
} A;
typedef struct {
boolean valid;
A as[12];
} AS;
FlashStorage(flashStore, AS);
class MyClass {
public:
MyClass();
void clean();
void print();
};
#endif
But I get this compilation error:
sketch/ZeroEEPROMTest.ino.cpp.o: In function `loop':
ZeroEEPROMTest.ino:68: multiple definition of `flashStore'
sketch/MyClass.cpp.o:sketch/FlashStorage.h:43: first defined here
collect2: error: ld returned 1 exit status
exit status 1
Then I moved to this:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <Arduino.h>
#include "FlashStorage.h"
typedef struct {
char id[12];
long up;
} A;
typedef struct {
boolean valid;
A as[12];
} AS;
class MyClass {
private:
FlashStorage(flashStore, AS);
public:
MyClass();
void clean();
void print();
};
#endif
but I get this compilation error:
In file included from sketch/MyClass.h:6:0,
from sketch/MyClass.cpp:1:
FlashStorage.h:34: error: 'constexpr' needed for in-class initialization of static data member 'const uint8_t MyClass::_dataflashStore [256]' of non-integral type [-fpermissive]
static const uint8_t PPCAT(_data,name)[(sizeof(T)+255)/256*256] = { }; \
Eventually I moved to this class that compiles fine but doesn't work at runtime.
#ifndef MYCLASS_H
#define MYCLASS_H
#include <Arduino.h>
#include "FlashStorage.h"
typedef struct {
char id[12];
long up;
} A;
typedef struct {
boolean valid;
A as[12];
} AS;
class MyClass {
private:
public:
MyClass();
void clean();
void print();
};
#endif
and
#include "MyClass.h"
MyClass::MyClass() {
FlashStorage(flashStore, AS);
AS as = flashStore.read();
if (as.valid == false) {
for(int i=0;i<5;i++) {
as.as[i].id[0] = 48+i;
as.as[i].id[1] = '\0';
as.as[i].up = i;
}
as.valid = true;
flashStore.write(as);
}
else {
for(int i=0;i<5;i++) {
Serial.println(as.as[i].id);
Serial.println(as.as[i].up);
}
}
}
void MyClass::clean() {
Serial.println("Cleaning");
FlashStorage(flashStore, AS);
AS as = flashStore.read();
for(int i=0;i<5;i++) {
as.as[i].id[0] = 49+i;
as.as[i].id[1] = '\0';
as.as[i].up = i+1;
}
as.valid = true;
flashStore.write(as);
}
void MyClass::print() {
Serial.println("Print Flash");
FlashStorage(flashStore, AS);
AS as = flashStore.read();
Serial.println("--- Flash content ---");
for(int i=0;i<5;i++) {
Serial.println(as.as[i].id);
Serial.println(as.as[i].up);
}
Serial.println("--------------------");
}
Using this simple test sketch:
#include "FlashStorage.h"
#include "MyClass.h"
void setup() {
Serial.begin(9600);
Serial.println("Begin");
MyClass mc = MyClass();
Serial.println("Waiting ....");
delay(5000);
mc.print();
Serial.println("Completed");
while(1)
;
}
void loop() {
}
Unfortunately, resetting the Zero, the print function doesn't print the previous saved values.
Can anyone help me to make it working?