2020-10-15 22:54:20 +02:00
|
|
|
#ifndef EEPROM_MANAGER
|
|
|
|
#define EEPROM_MANAGER
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
|
|
|
class EepromUnit;
|
|
|
|
|
|
|
|
class EepromManager final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit EepromManager();
|
|
|
|
|
|
|
|
friend class EepromUnit;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to register / allocate a part of the EEPROM
|
|
|
|
*
|
|
|
|
* This function ALWAYS has to be called in THE SAME ORDER for each unit.
|
|
|
|
* Otherwise THE DATA WILL BE LOST!!
|
|
|
|
*/
|
|
|
|
EepromUnit *registerEempromUnit(size_t size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int currentAddressEnding;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const T &writeToEeprom(int address, size_t size, const T &t)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (sizeof(T) > size) {
|
2020-10-17 01:08:43 +02:00
|
|
|
Serial.println("[Error][EepromManager] writing: Size should be: " + String(size) + " but was: " + String(sizeof(T)));
|
2020-10-15 22:54:20 +02:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T &res = EEPROM.put(address, t);
|
2020-10-17 01:08:43 +02:00
|
|
|
EEPROM.commit();
|
2020-10-15 22:54:20 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T &readFromEeprom(int address, size_t size, T &t)
|
|
|
|
{
|
|
|
|
if (sizeof(T) > size) {
|
2020-10-17 01:08:43 +02:00
|
|
|
Serial.println("[Error][EepromManager] reading: Size should be: " + String(size) + " but was: " + String(sizeof(T)));
|
2020-10-15 22:54:20 +02:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
T &res = EEPROM.get(address, t);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class EepromUnit final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
friend class EepromManager;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const T &write(const T &t)
|
|
|
|
{
|
|
|
|
return this->manager->writeToEeprom(this->address, this->size, t);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T &read(T &t)
|
|
|
|
{
|
|
|
|
return this->manager->readFromEeprom(this->address, this->size, t);
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit EepromUnit(EepromManager *manager, int address, size_t size);
|
|
|
|
|
|
|
|
EepromManager *manager;
|
|
|
|
int address;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // EEPROM_MANAGER
|