Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Config save callback (#83)
Browse files Browse the repository at this point in the history
* Add config loop function

* Add callback

* Update docs

Thanks to @arjena for the code
  • Loading branch information
maakbaas authored Feb 17, 2021
1 parent 01c226c commit efee048
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 9 deletions.
22 changes: 18 additions & 4 deletions docs/config-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> 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
Expand Down
8 changes: 7 additions & 1 deletion examples/configManager/configManagerExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ struct task

task taskA = {.rate = 60000, .previous = 0};

void saveCallback() {
Serial.println("EEPROM saved");
}

void setup()
{
Serial.begin(115200);

LittleFS.begin();
GUI.begin();
configManager.begin();
configManager.setConfigSaveCallback(saveCallback);
WiFiManager.begin(configManager.data.projectName);
timeSync.begin();
}
Expand All @@ -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))
{
Expand Down
1 change: 1 addition & 0 deletions examples/dashboard/dashboardExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void loop()
//software interrupts
WiFiManager.loop();
updater.loop();
configManager.loop();
dash.loop();

//your code here
Expand Down
1 change: 1 addition & 0 deletions examples/fetch/fetchExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ))
Expand Down
1 change: 1 addition & 0 deletions examples/helloWorld/helloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void loop()
//software interrupts
WiFiManager.loop();
updater.loop();
configManager.loop();

//your code here
}
1 change: 1 addition & 0 deletions examples/timeSync/timeSyncExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ void loop()
//software interrupts
WiFiManager.loop();
updater.loop();
configManager.loop();
}
25 changes: 21 additions & 4 deletions src/configManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool config::begin(int numBytes)
{
Serial.println(PSTR("Internal data checksum mismatch"));
internal = internalData();
save();
requestSave = true;
returnValue = false;
}

Expand All @@ -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()
Expand All @@ -72,6 +72,23 @@ void config::save()
EEPROM.put(SIZE_INTERNAL + 5 + sizeof(data), checksum(reinterpret_cast<uint8_t*>(&data), sizeof(data)));

EEPROM.commit();

if ( _configsavecallback != NULL) {
_configsavecallback();
}
}

void config::setConfigSaveCallback( std::function<void()> func ) {
_configsavecallback = func;
}

void config::loop()
{
if (requestSave)
{
requestSave = false;
save();
}
}

uint8_t config::checksum(uint8_t *byteArray, unsigned long length)
Expand Down
4 changes: 4 additions & 0 deletions src/configManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ class config
void saveExternal(configData *extData);
void save();
void reset();
void loop();
void setConfigSaveCallback( std::function<void()> func );

private:
uint8_t checksum(uint8_t *byteArray, unsigned long length);
std::function<void()> _configsavecallback;
bool requestSave = false;
};

extern config configManager;
Expand Down

0 comments on commit efee048

Please sign in to comment.