From cac249f349995a1bb68068833ca7f791dc8f942e Mon Sep 17 00:00:00 2001 From: "lorenzo.tacconi1967@gmail.com" Date: Sun, 4 Sep 2016 21:25:58 +0200 Subject: [PATCH 1/2] EEPROM Library: Improved put function, compare data and only in case are different set _dirty flag, copy the data --- libraries/EEPROM/EEPROM.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 09e582be73..a54e4fda71 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -53,8 +53,11 @@ class EEPROMClass { if (address < 0 || address + sizeof(T) > _size) return t; - memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); - _dirty = true; + _dirty = (memcmp(_data + address, (const uint8_t*) &t, sizeof(T)) !=0); + + if (_dirty) + memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); + return t; } From 68ea5e8f91e66dc5cef9649bc9ffe8ef6ee6aadc Mon Sep 17 00:00:00 2001 From: "lorenzo.tacconi1967@gmail.com" Date: Tue, 27 Sep 2016 21:06:21 +0200 Subject: [PATCH 2/2] It will grant that the _dirty flag is reset only at EEPROM.commit() and no changes are lost --- libraries/EEPROM/EEPROM.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index a54e4fda71..3a26d02e64 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -52,11 +52,10 @@ class EEPROMClass { const T &put(int address, const T &t) { if (address < 0 || address + sizeof(T) > _size) return t; - - _dirty = (memcmp(_data + address, (const uint8_t*) &t, sizeof(T)) !=0); - - if (_dirty) - memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); + if (memcmp(_data + address, (const uint8_t*)&t, sizeof(T)) != 0) { + _dirty = true; + memcpy(_data + address, (const uint8_t*)&t, sizeof(T)); + } return t; }