From 14162fb1a44d8ea5cc33434d91e85e95684fb2ab Mon Sep 17 00:00:00 2001 From: OutOfSync1 Date: Wed, 14 Apr 2021 23:46:03 +0200 Subject: [PATCH] fix userdata saverestore (#133) * * fix for save/restore of userdata * change declaration of restore() in knx_facade.h to "const uint8_t* restore(const uint8_t* buffer)" to avoid calling default implementation in save_restore.h * change typedefs to separate SaveCallback and RestoreCallback * fix BME60 example, knx.setRestoreCallback() needs to use const uint8_t* as well * fix commitToEeprom for ESP32 * trigger dirty flag for ESP32 to make sure data is committed --- examples/knx-bme680/knx-bme680.ino | 4 ++-- src/esp32_platform.cpp | 1 + src/knx_facade.h | 13 +++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/examples/knx-bme680/knx-bme680.ino b/examples/knx-bme680/knx-bme680.ino index b6deb6d..f10a723 100644 --- a/examples/knx-bme680/knx-bme680.ino +++ b/examples/knx-bme680/knx-bme680.ino @@ -25,7 +25,7 @@ void checkIaqSensorStatus(void); void errLeds(void); uint8_t* saveBme680State(uint8_t* buffer); -uint8_t* loadBme680State(uint8_t* buffer); +const uint8_t* loadBme680State(const uint8_t* buffer); void triggerCallback(GroupObject& go); void updateState(); @@ -214,7 +214,7 @@ void errLeds(void) delay(100); } -uint8_t* loadBme680State(uint8_t* buffer) +const uint8_t* loadBme680State(const uint8_t* buffer) { // Existing state in EEPROM Serial.println("Reading state from EEPROM"); diff --git a/src/esp32_platform.cpp b/src/esp32_platform.cpp index 585464f..5c28ee1 100644 --- a/src/esp32_platform.cpp +++ b/src/esp32_platform.cpp @@ -104,6 +104,7 @@ uint8_t * Esp32Platform::getEepromBuffer(uint16_t size) void Esp32Platform::commitToEeprom() { + EEPROM.getDataPtr(); // trigger dirty flag in EEPROM lib to make sure data will be written to flash EEPROM.commit(); } diff --git a/src/knx_facade.h b/src/knx_facade.h index e858440..e0cecab 100644 --- a/src/knx_facade.h +++ b/src/knx_facade.h @@ -40,7 +40,8 @@ #endif #endif -typedef uint8_t* (*SaveRestoreCallback)(uint8_t* buffer); +typedef const uint8_t* (*RestoreCallback)(const uint8_t* buffer); +typedef uint8_t* (*SaveCallback)(uint8_t* buffer); typedef void (*IsrFunctionPtr)(); template class KnxFacade : private SaveRestore @@ -266,12 +267,12 @@ template class KnxFacade : private SaveRestore _progButtonISRFuncPtr = progButtonISRFuncPtr; } - void setSaveCallback(SaveRestoreCallback func) + void setSaveCallback(SaveCallback func) { _saveCallback = func; } - void setRestoreCallback(SaveRestoreCallback func) + void setRestoreCallback(RestoreCallback func) { _restoreCallback = func; } @@ -336,8 +337,8 @@ template class KnxFacade : private SaveRestore uint32_t _ledPin = LED_BUILTIN; uint32_t _buttonPinInterruptOn = RISING; uint32_t _buttonPin = 0; - SaveRestoreCallback _saveCallback = 0; - SaveRestoreCallback _restoreCallback = 0; + SaveCallback _saveCallback = 0; + RestoreCallback _restoreCallback = 0; volatile bool _toggleProgMode = false; bool _progLedState = false; uint16_t _saveSize = 0; @@ -351,7 +352,7 @@ template class KnxFacade : private SaveRestore return buffer; } - uint8_t* restore(uint8_t* buffer) + const uint8_t* restore(const uint8_t* buffer) { if (_restoreCallback != 0) return _restoreCallback(buffer);