Skip to content

Commit

Permalink
Clear dirty flag in begin and reallocate only if necessary, add getCo…
Browse files Browse the repository at this point in the history
…nstDataPtr method (esp8266#2217) (esp8266#3849)
  • Loading branch information
devyte authored Nov 20, 2017
1 parent 117bc87 commit 1b76e9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions libraries/EEPROM/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t*>(_data), _size);
interrupts();

_dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time
}

void EEPROMClass::end() {
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions libraries/EEPROM/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class EEPROMClass {
void end();

uint8_t * getDataPtr();
uint8_t const * getConstDataPtr();

template<typename T>
T &get(int address, T &t) {
Expand Down

0 comments on commit 1b76e9b

Please sign in to comment.