1 #ifndef PERSISTENT_STORAGE_H
2 #define PERSISTENT_STORAGE_H
4 #if defined(__SAMD21G18A__)
5 #include <FlashAsEEPROM.h>
11 class PersistentStorage
14 static unsigned reserveSpace(
unsigned numBytes)
16 static unsigned sSize;
17 unsigned offset = sSize;
18 if (sSize + numBytes >= EEPROM.length())
34 template <
typename T, u
int16_t kSignature>
40 fByteIndex = PersistentStorage::reserveSpace(
sizeof(T) +
sizeof(uint16_t));
45 return kSignature + (
sizeof(T) +
sizeof(uint16_t));
50 return (fByteIndex != ~0 && readSignature() ==
signature());
55 uint8_t *dst = (uint8_t*) &t;
61 uint8_t index = fByteIndex +
sizeof(uint16_t);
62 for (
unsigned count =
sizeof(T); count; count--)
63 *dst++ = EEPROM.read(index++);
70 memset(dst,
'\0',
sizeof(T));
75 const T &
put(
const T &t)
77 bool needsCommit =
false;
84 uint8_t index = fByteIndex;
87 if (EEPROM.read(index) != hisig)
89 EEPROM.write(index, hisig);
93 if (EEPROM.read(index) != losig)
95 EEPROM.write(index, losig);
100 const uint8_t *ptr = (
const uint8_t*) &t;
101 for (
int count =
sizeof(T); count; count--, index++, ptr++)
103 if (EEPROM.read(index) != *ptr)
105 EEPROM.write(index, *ptr);
115 #ifdef EEPROM_EMULATION_SIZE
127 uint16_t readSignature()
129 return ((uint16_t(EEPROM.read(fByteIndex)) << 8) | (EEPROM.read(fByteIndex+1)));
135 #define PERSIST(T) Persistent<T,WSID16(#T)>