This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
This repository was archived by the owner on Feb 8, 2024. It is now read-only.
Add callback to config save #77
Copy link
Copy link
Closed
Description
Not an issue, but I'm not an experienced Github user, so I don't know where to put this. Maybe it is a pull-request, but I don't know how that works...
I needed a way to know if any of the config values has changed, so i can combine that with my MQTT calls. So I added a function to configManager.
My configManager.cpp now looks like this:
#include <EEPROM.h>
#include <Arduino.h>
#include "configManager.h"
//class functions
bool config::begin(int numBytes)
{
EEPROM.begin(numBytes);
uint32_t storedVersion;
uint8_t checksumData = 0;
uint8_t checksumInternal = 0;
EEPROM.get(0, internal);
EEPROM.get(SIZE_INTERNAL, checksumInternal);
EEPROM.get(SIZE_INTERNAL + 1, storedVersion);
EEPROM.get(SIZE_INTERNAL + 5, data);
EEPROM.get(SIZE_INTERNAL + 5 + sizeof(data), checksumData);
bool returnValue = true;
//reset configuration data if checksum mismatch
if (checksumData != checksum(reinterpret_cast<uint8_t*>(&data), sizeof(data)) || storedVersion != configVersion)
{
Serial.println(PSTR("Config data checksum mismatch"));
reset();
returnValue = false;
}
//reset internal data if checksum mismatch
if (checksumInternal != checksum(reinterpret_cast<uint8_t*>(&internal), sizeof(internal)))
{
Serial.println(PSTR("Internal data checksum mismatch"));
internal = internalData();
save();
returnValue = false;
}
return returnValue;
}
void config::reset()
{
memcpy_P(&data, &defaults, sizeof(data));
save();
}
void config::saveRaw(uint8_t bytes[])
{
memcpy(&data,bytes,sizeof(data));
save();
}
void config::saveExternal(configData *extData)
{
memcpy(&data, extData, sizeof(data));
save();
}
void config::setConfigSaveCallback( std::function<void()> func ) {
_configsavecallback = func;
}
void config::save()
{
EEPROM.put(0, internal);
//save checksum for internal data
EEPROM.put(SIZE_INTERNAL, checksum(reinterpret_cast<uint8_t*>(&internal), sizeof(internal)));
EEPROM.put(SIZE_INTERNAL + 1, configVersion);
EEPROM.put(SIZE_INTERNAL + 5, data);
//save checksum for configuration data
EEPROM.put(SIZE_INTERNAL + 5 + sizeof(data), checksum(reinterpret_cast<uint8_t*>(&data), sizeof(data)));
EEPROM.commit();
if ( _configsavecallback != NULL) {
_configsavecallback();
}
}
uint8_t config::checksum(uint8_t *byteArray, unsigned long length)
{
uint8_t value = 0;
unsigned long counter;
for (counter=0; counter<length; counter++)
{
value += *byteArray;
byteArray++;
}
return (uint8_t)(256-value);
}
config configManager;
And my configManager.h:
#ifndef CONFIGMGR_H
#define CONFIGMGR_H
#include "IPAddress.h"
#include "generated/config.h"
//data that needs to be persisted for other parts of the framework
#define SIZE_INTERNAL 32 //allocate 32 bytes to have room for future expansion
struct internalData
{
uint32_t ip;
uint32_t gw;
uint32_t sub;
uint32_t dns;
};
class config
{
public:
configData data;
internalData internal;
bool begin(int numBytes = 512);
void saveRaw(uint8_t bytes[]);
void saveExternal(configData *extData);
void save();
void reset();
void setConfigSaveCallback( std::function<void()> func );
private:
uint8_t checksum(uint8_t *byteArray, unsigned long length);
std::function<void()> _configsavecallback;
};
extern config configManager;
#endif
So now you can define a function to be called in setup() when the 'Save' button is clicked:
configManager.setConfigSaveCallback(saveCallback);
EisbaeeerEisbaeeer
Metadata
Metadata
Assignees
Labels
No labels