From 1b76e9bf72c05933ed912d2603bfa2d057db800e Mon Sep 17 00:00:00 2001 From: Develo Date: Mon, 20 Nov 2017 14:39:27 -0300 Subject: [PATCH] Clear dirty flag in begin and reallocate only if necessary, add getConstDataPtr method (#2217) (#3849) --- libraries/EEPROM/EEPROM.cpp | 13 +++++++++++-- libraries/EEPROM/EEPROM.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index b7909d0c3..b5b8bfc15 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -56,16 +56,21 @@ void EEPROMClass::begin(size_t size) { size = (size + 3) & (~3); - if (_data) { + //In case begin() is called a 2nd+ time, don't reallocate if size is the same + if(_data && size != _size) { delete[] _data; + _data = new uint8_t[size]; + } else if(!_data) { + _data = new uint8_t[size]; } - _data = new uint8_t[size]; _size = size; noInterrupts(); spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast(_data), _size); interrupts(); + + _dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time } void EEPROMClass::end() { @@ -131,6 +136,10 @@ uint8_t * EEPROMClass::getDataPtr() { return &_data[0]; } +uint8_t const * EEPROMClass::getConstDataPtr() { + return &_data[0]; +} + #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) EEPROMClass EEPROM; #endif diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 3a26d02e6..934a995a0 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -38,6 +38,7 @@ public: void end(); uint8_t * getDataPtr(); + uint8_t const * getConstDataPtr(); template T &get(int address, T &t) {