From 69158c293c70abcc3af41724b7530ccbd65a1bad Mon Sep 17 00:00:00 2001 From: "Bundit J." Date: Sat, 18 Apr 2015 15:33:45 +0700 Subject: [PATCH 1/3] Add get and put functions to EEPROM As available in http://www.arduino.cc/en/Reference/EEPROM --- .../esp8266/libraries/EEPROM/EEPROM.h | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h b/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h index 0c7068d1cd..c51f759b70 100644 --- a/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h +++ b/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h @@ -35,6 +35,27 @@ class EEPROMClass void commit(); void end(); + template T &get(int address, T &t) + { + if (address < 0 || address >= _size) + return t; + + uint8_t *ptr = (uint8_t*) &t; + for(int count = 0; count < sizeof(T); ++count) *ptr++ = _data[address + count]; + return t; + } + + template const T &put(int address, const T &t) + { + if (address < 0 || address >= _size) + return t; + + const uint8_t *ptr = (const uint8_t*) &t; + for(int count = 0; count < sizeof(T); ++count) _data[address + count] = *ptr++; + _dirty = true; + return t; + } + protected: uint8_t* _data; size_t _size; From 7795695c2e1f7bbf9c20df437f52c6bc258e5e21 Mon Sep 17 00:00:00 2001 From: "Bundit J." Date: Sat, 18 Apr 2015 16:08:36 +0700 Subject: [PATCH 2/3] Add get and put functions to EEPROM As available in http://www.arduino.cc/en/Reference/EEPROM --- hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h b/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h index c51f759b70..961ff8b082 100644 --- a/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h +++ b/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h @@ -37,7 +37,7 @@ class EEPROMClass template T &get(int address, T &t) { - if (address < 0 || address >= _size) + if (address < 0 || address + sizeof(T) > _size) return t; uint8_t *ptr = (uint8_t*) &t; @@ -47,7 +47,7 @@ class EEPROMClass template const T &put(int address, const T &t) { - if (address < 0 || address >= _size) + if (address < 0 || address + sizeof(T) > _size) return t; const uint8_t *ptr = (const uint8_t*) &t; From 33f735c15d8bbf0448d941ed48d1091edfd0063d Mon Sep 17 00:00:00 2001 From: "Bundit J." Date: Sat, 18 Apr 2015 22:48:04 +0700 Subject: [PATCH 3/3] Use memcpy instead of loop --- hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h b/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h index 961ff8b082..52de839091 100644 --- a/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h +++ b/hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h @@ -24,6 +24,7 @@ #include #include +#include class EEPROMClass { @@ -41,7 +42,7 @@ class EEPROMClass return t; uint8_t *ptr = (uint8_t*) &t; - for(int count = 0; count < sizeof(T); ++count) *ptr++ = _data[address + count]; + memcpy(ptr, _data + address, sizeof(T)); return t; } @@ -51,7 +52,7 @@ class EEPROMClass return t; const uint8_t *ptr = (const uint8_t*) &t; - for(int count = 0; count < sizeof(T); ++count) _data[address + count] = *ptr++; + memcpy(_data + address, ptr, sizeof(T)); _dirty = true; return t; }