4 #include <MutilaDebug.h> 8 extern uint16_t PersistentSettingOffset;
102 PersistentSetting(T minimum, T maximum, T defaultValue, uint8_t copies=1, int32_t eepromOffset=-1) :
103 _eepromOffset(eepromOffset),
107 _defaultValue(defaultValue)
109 if (eepromOffset < 0) {
110 _eepromOffset = PersistentSettingOffset;
112 _eepromOffset = eepromOffset;
114 PersistentSettingOffset = _eepromOffset +
size();
125 return v >= _minimum && v <= _maximum;
133 _value = _defaultValue;
144 #if defined(ESP8266) or defined(ESP32) 145 #warning ESP EEPROM functionality not yet implemented 148 for (uint8_t i=0; i<
sizeof(T); i++) {
149 *((uint8_t*)&value+i) = EEPROM.read(address+i);
160 return _eepromOffset + ((uint16_t)idx * (
sizeof(T) +
sizeof(uint8_t)));
163 uint16_t dataOffset(uint8_t idx) {
167 uint8_t getCurrentIdx() {
168 #if defined(ESP8266) or defined(ESP32) 169 #warning ESP EEPROM functionality not yet implemented 175 uint8_t nextCount = EEPROM.read(
counterOffset((i+1)%_copies));
176 if ((uint8_t)(nextCount - thisCount) != 1) {
179 thisCount = nextCount;
191 uint8_t idx = getCurrentIdx();
194 _value = _defaultValue;
206 #if defined(ESP8266) or defined(ESP32) 207 #warning ESP EEPROM functionality not yet implemented 209 uint8_t idx = getCurrentIdx();
217 address = _eepromOffset;
221 idx = (idx + 1) % _copies;
224 EEPROM.update(address++, counter);
229 for (uint8_t i=0; i<
sizeof(T); i++) {
230 EEPROM.update(address++, *((uint8_t*)&_value+i));
237 T
get() {
return _value; }
266 bool set(T v,
bool saveIt=
false) {
286 return (
sizeof(uint8_t) +
sizeof(T) ) * _copies;
294 DB(_eepromOffset, HEX);
306 uint16_t _eepromOffset;
324 NamedPersistentSetting(T minimum, T maximum, T defaultValue,
const char* name, uint8_t copies=1, int32_t eepromOffset=-1) :
329 const String& getName() {
return _name; }
bool set(T v, bool saveIt=false)
EEPROM-backed variables with optional wear levelling functionality.
PersistentSetting(T minimum, T maximum, T defaultValue, uint8_t copies=1, int32_t eepromOffset=-1)
void reset(bool saveIt=false)
uint16_t counterOffset(uint8_t idx)
T getValueAt(uint16_t address)