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

Commit efee048

Browse files
authored
Config save callback (#83)
* Add config loop function * Add callback * Update docs Thanks to @arjena for the code
1 parent 01c226c commit efee048

File tree

8 files changed

+54
-9
lines changed

8 files changed

+54
-9
lines changed

docs/config-manager.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,42 @@ This method must be called from the setup of the application. The optional argum
2020
```c++
2121
void save();
2222
```
23-
This method saves the configManager.data object to EEPROM. Use this if you write changes or updates directly to the RAM mirror
23+
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.
2424

2525
#### saveExternal
2626

2727
```c++
2828
void saveExternal(configData *extData);
2929
```
30-
This method copies an external configData object into the configManager.data RAM mirror, and uploads it to EEPROM.
30+
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.
3131
3232
#### saveRaw
3333
3434
```c++
3535
void saveRaw(uint8_t bytes[]);
3636
```
37-
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.
37+
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.
3838

3939
#### reset
4040

4141
```c++
4242
void reset();
4343
```
44-
This method resets the EEPROM contents to the default values.
44+
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.
45+
46+
#### loop
47+
48+
```c++
49+
void loop();
50+
```
51+
Performs the save function is a save is requested. Place this in your main loop function.
52+
53+
#### setConfigSaveCallback
54+
55+
```c++
56+
void setConfigSaveCallback( std::function<void()> func );
57+
```
58+
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.
4559
4660
## Class Members
4761

examples/configManager/configManagerExample.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ struct task
1616

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

19+
void saveCallback() {
20+
Serial.println("EEPROM saved");
21+
}
22+
1923
void setup()
2024
{
2125
Serial.begin(115200);
2226

2327
LittleFS.begin();
2428
GUI.begin();
2529
configManager.begin();
30+
configManager.setConfigSaveCallback(saveCallback);
2631
WiFiManager.begin(configManager.data.projectName);
2732
timeSync.begin();
2833
}
@@ -32,7 +37,8 @@ void loop()
3237
//software interrupts
3338
WiFiManager.loop();
3439
updater.loop();
35-
40+
configManager.loop();
41+
3642
//task A
3743
if (taskA.previous == 0 || (millis() - taskA.previous > taskA.rate))
3844
{

examples/dashboard/dashboardExample.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void loop()
3333
//software interrupts
3434
WiFiManager.loop();
3535
updater.loop();
36+
configManager.loop();
3637
dash.loop();
3738

3839
//your code here

examples/fetch/fetchExample.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void loop()
3232
//software interrupts
3333
WiFiManager.loop();
3434
updater.loop();
35+
configManager.loop();
3536

3637
//task A
3738
if (taskA.previous==0 || (millis() - taskA.previous > taskA.rate ))

examples/helloWorld/helloWorld.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void loop()
2626
//software interrupts
2727
WiFiManager.loop();
2828
updater.loop();
29+
configManager.loop();
2930

3031
//your code here
3132
}

examples/timeSync/timeSyncExample.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ void loop()
4141
//software interrupts
4242
WiFiManager.loop();
4343
updater.loop();
44+
configManager.loop();
4445
}

src/configManager.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool config::begin(int numBytes)
3333
{
3434
Serial.println(PSTR("Internal data checksum mismatch"));
3535
internal = internalData();
36-
save();
36+
requestSave = true;
3737
returnValue = false;
3838
}
3939

@@ -43,19 +43,19 @@ bool config::begin(int numBytes)
4343
void config::reset()
4444
{
4545
memcpy_P(&data, &defaults, sizeof(data));
46-
save();
46+
requestSave = true;
4747
}
4848

4949
void config::saveRaw(uint8_t bytes[])
5050
{
5151
memcpy(&data,bytes,sizeof(data));
52-
save();
52+
requestSave = true;
5353
}
5454

5555
void config::saveExternal(configData *extData)
5656
{
5757
memcpy(&data, extData, sizeof(data));
58-
save();
58+
requestSave = true;
5959
}
6060

6161
void config::save()
@@ -72,6 +72,23 @@ void config::save()
7272
EEPROM.put(SIZE_INTERNAL + 5 + sizeof(data), checksum(reinterpret_cast<uint8_t*>(&data), sizeof(data)));
7373

7474
EEPROM.commit();
75+
76+
if ( _configsavecallback != NULL) {
77+
_configsavecallback();
78+
}
79+
}
80+
81+
void config::setConfigSaveCallback( std::function<void()> func ) {
82+
_configsavecallback = func;
83+
}
84+
85+
void config::loop()
86+
{
87+
if (requestSave)
88+
{
89+
requestSave = false;
90+
save();
91+
}
7592
}
7693

7794
uint8_t config::checksum(uint8_t *byteArray, unsigned long length)

src/configManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ class config
2727
void saveExternal(configData *extData);
2828
void save();
2929
void reset();
30+
void loop();
31+
void setConfigSaveCallback( std::function<void()> func );
3032

3133
private:
3234
uint8_t checksum(uint8_t *byteArray, unsigned long length);
35+
std::function<void()> _configsavecallback;
36+
bool requestSave = false;
3337
};
3438

3539
extern config configManager;

0 commit comments

Comments
 (0)