From efee048a08fabc0d37c9456fb64814a88ddc92ad Mon Sep 17 00:00:00 2001 From: Maakbaas <57539443+maakbaas@users.noreply.github.com> Date: Wed, 17 Feb 2021 14:29:54 +0100 Subject: [PATCH] Config save callback (#83) * Add config loop function * Add callback * Update docs Thanks to @arjena for the code --- docs/config-manager.md | 22 +++++++++++++--- .../configManager/configManagerExample.cpp | 8 +++++- examples/dashboard/dashboardExample.cpp | 1 + examples/fetch/fetchExample.cpp | 1 + examples/helloWorld/helloWorld.cpp | 1 + examples/timeSync/timeSyncExample.cpp | 1 + src/configManager.cpp | 25 ++++++++++++++++--- src/configManager.h | 4 +++ 8 files changed, 54 insertions(+), 9 deletions(-) diff --git a/docs/config-manager.md b/docs/config-manager.md index 9bfdbc2..54af299 100644 --- a/docs/config-manager.md +++ b/docs/config-manager.md @@ -20,28 +20,42 @@ This method must be called from the setup of the application. The optional argum ```c++ void save(); ``` -This method saves the configManager.data object to EEPROM. Use this if you write changes or updates directly to the RAM mirror +This method saves the configManager.data object to EEPROM. Use this if you write changes or updates directly to the RAM mirror. This method is blocking. #### saveExternal ```c++ void saveExternal(configData *extData); ``` -This method copies an external configData object into the configManager.data RAM mirror, and uploads it to EEPROM. +This method copies an external configData object into the configManager.data RAM mirror, and uploads it to EEPROM. This method is asynchronous. The actual save command will be performed in the `loop()` function. #### saveRaw ```c++ void saveRaw(uint8_t bytes[]); ``` -This method stores the input byte array into the EEPROM. This is an unsafe method, you must ensure externally that the input byte array is the correct length and structure. +This method stores the input byte array into the EEPROM. This is an unsafe method, you must ensure externally that the input byte array is the correct length and structure. This method is asynchronous. The actual save command will be performed in the `loop()` function. #### reset ```c++ void reset(); ``` -This method resets the EEPROM contents to the default values. +This method resets the EEPROM contents to the default values. This method is asynchronous. The actual save command will be performed in the `loop()` function. + +#### loop + +```c++ +void loop(); +``` +Performs the save function is a save is requested. Place this in your main loop function. + +#### setConfigSaveCallback + +```c++ +void setConfigSaveCallback( std::function func ); +``` +Use this function to add a callback that will be executed everytime new data is committed to EEPROM. See the configuration manager example for how to use it. ## Class Members diff --git a/examples/configManager/configManagerExample.cpp b/examples/configManager/configManagerExample.cpp index 8b7b09d..e362991 100644 --- a/examples/configManager/configManagerExample.cpp +++ b/examples/configManager/configManagerExample.cpp @@ -16,6 +16,10 @@ struct task task taskA = {.rate = 60000, .previous = 0}; +void saveCallback() { + Serial.println("EEPROM saved"); +} + void setup() { Serial.begin(115200); @@ -23,6 +27,7 @@ void setup() LittleFS.begin(); GUI.begin(); configManager.begin(); + configManager.setConfigSaveCallback(saveCallback); WiFiManager.begin(configManager.data.projectName); timeSync.begin(); } @@ -32,7 +37,8 @@ void loop() //software interrupts WiFiManager.loop(); updater.loop(); - + configManager.loop(); + //task A if (taskA.previous == 0 || (millis() - taskA.previous > taskA.rate)) { diff --git a/examples/dashboard/dashboardExample.cpp b/examples/dashboard/dashboardExample.cpp index 16a6aac..c19c7bc 100644 --- a/examples/dashboard/dashboardExample.cpp +++ b/examples/dashboard/dashboardExample.cpp @@ -33,6 +33,7 @@ void loop() //software interrupts WiFiManager.loop(); updater.loop(); + configManager.loop(); dash.loop(); //your code here diff --git a/examples/fetch/fetchExample.cpp b/examples/fetch/fetchExample.cpp index cad1cd1..5a2860a 100644 --- a/examples/fetch/fetchExample.cpp +++ b/examples/fetch/fetchExample.cpp @@ -32,6 +32,7 @@ void loop() //software interrupts WiFiManager.loop(); updater.loop(); + configManager.loop(); //task A if (taskA.previous==0 || (millis() - taskA.previous > taskA.rate )) diff --git a/examples/helloWorld/helloWorld.cpp b/examples/helloWorld/helloWorld.cpp index 76a176a..6226c8d 100644 --- a/examples/helloWorld/helloWorld.cpp +++ b/examples/helloWorld/helloWorld.cpp @@ -26,6 +26,7 @@ void loop() //software interrupts WiFiManager.loop(); updater.loop(); + configManager.loop(); //your code here } diff --git a/examples/timeSync/timeSyncExample.cpp b/examples/timeSync/timeSyncExample.cpp index 215c6e9..b19cee5 100644 --- a/examples/timeSync/timeSyncExample.cpp +++ b/examples/timeSync/timeSyncExample.cpp @@ -41,4 +41,5 @@ void loop() //software interrupts WiFiManager.loop(); updater.loop(); + configManager.loop(); } diff --git a/src/configManager.cpp b/src/configManager.cpp index 16274f0..c439017 100644 --- a/src/configManager.cpp +++ b/src/configManager.cpp @@ -33,7 +33,7 @@ bool config::begin(int numBytes) { Serial.println(PSTR("Internal data checksum mismatch")); internal = internalData(); - save(); + requestSave = true; returnValue = false; } @@ -43,19 +43,19 @@ bool config::begin(int numBytes) void config::reset() { memcpy_P(&data, &defaults, sizeof(data)); - save(); + requestSave = true; } void config::saveRaw(uint8_t bytes[]) { memcpy(&data,bytes,sizeof(data)); - save(); + requestSave = true; } void config::saveExternal(configData *extData) { memcpy(&data, extData, sizeof(data)); - save(); + requestSave = true; } void config::save() @@ -72,6 +72,23 @@ void config::save() EEPROM.put(SIZE_INTERNAL + 5 + sizeof(data), checksum(reinterpret_cast(&data), sizeof(data))); EEPROM.commit(); + + if ( _configsavecallback != NULL) { + _configsavecallback(); + } +} + +void config::setConfigSaveCallback( std::function func ) { + _configsavecallback = func; +} + +void config::loop() +{ + if (requestSave) + { + requestSave = false; + save(); + } } uint8_t config::checksum(uint8_t *byteArray, unsigned long length) diff --git a/src/configManager.h b/src/configManager.h index 3f340f5..fa97c60 100644 --- a/src/configManager.h +++ b/src/configManager.h @@ -27,9 +27,13 @@ class config void saveExternal(configData *extData); void save(); void reset(); + void loop(); + void setConfigSaveCallback( std::function func ); private: uint8_t checksum(uint8_t *byteArray, unsigned long length); + std::function _configsavecallback; + bool requestSave = false; }; extern config configManager;