From 7c4e7fa90c8c2a58e479a28c7843bd1b12696785 Mon Sep 17 00:00:00 2001 From: maakbaas Date: Wed, 17 Feb 2021 14:08:58 +0100 Subject: [PATCH 1/3] Add config loop function --- docs/config-manager.md | 15 +++++++++++---- examples/configManager/configManagerExample.cpp | 3 ++- examples/dashboard/dashboardExample.cpp | 1 + examples/fetch/fetchExample.cpp | 1 + examples/helloWorld/helloWorld.cpp | 1 + examples/timeSync/timeSyncExample.cpp | 1 + src/configManager.cpp | 17 +++++++++++++---- src/configManager.h | 2 ++ 8 files changed, 32 insertions(+), 9 deletions(-) diff --git a/docs/config-manager.md b/docs/config-manager.md index 9bfdbc2..1b06cfc 100644 --- a/docs/config-manager.md +++ b/docs/config-manager.md @@ -20,28 +20,35 @@ 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. ## Class Members diff --git a/examples/configManager/configManagerExample.cpp b/examples/configManager/configManagerExample.cpp index 8b7b09d..0434e17 100644 --- a/examples/configManager/configManagerExample.cpp +++ b/examples/configManager/configManagerExample.cpp @@ -32,7 +32,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..c43b02b 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() @@ -74,6 +74,15 @@ void config::save() EEPROM.commit(); } +void config::loop() +{ + if (requestSave) + { + requestSave = false; + save(); + } +} + uint8_t config::checksum(uint8_t *byteArray, unsigned long length) { uint8_t value = 0; diff --git a/src/configManager.h b/src/configManager.h index 3f340f5..6142adf 100644 --- a/src/configManager.h +++ b/src/configManager.h @@ -27,9 +27,11 @@ class config void saveExternal(configData *extData); void save(); void reset(); + void loop(); private: uint8_t checksum(uint8_t *byteArray, unsigned long length); + bool requestSave = false; }; extern config configManager; From 3a32afe682ee73a97e578a134cec4bdf27e96bf2 Mon Sep 17 00:00:00 2001 From: maakbaas Date: Wed, 17 Feb 2021 14:23:52 +0100 Subject: [PATCH 2/3] Add callback --- examples/configManager/configManagerExample.cpp | 5 +++++ src/configManager.cpp | 8 ++++++++ src/configManager.h | 2 ++ 3 files changed, 15 insertions(+) diff --git a/examples/configManager/configManagerExample.cpp b/examples/configManager/configManagerExample.cpp index 0434e17..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(); } diff --git a/src/configManager.cpp b/src/configManager.cpp index c43b02b..c439017 100644 --- a/src/configManager.cpp +++ b/src/configManager.cpp @@ -72,6 +72,14 @@ 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() diff --git a/src/configManager.h b/src/configManager.h index 6142adf..fa97c60 100644 --- a/src/configManager.h +++ b/src/configManager.h @@ -28,9 +28,11 @@ class config 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; }; From cb8c368393db460b3769a7b4391e4fa20b297694 Mon Sep 17 00:00:00 2001 From: maakbaas Date: Wed, 17 Feb 2021 14:26:09 +0100 Subject: [PATCH 3/3] Update docs --- docs/config-manager.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/config-manager.md b/docs/config-manager.md index 1b06cfc..54af299 100644 --- a/docs/config-manager.md +++ b/docs/config-manager.md @@ -50,6 +50,13 @@ 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 #### data