#ifndef EEPROM_MANAGER #define EEPROM_MANAGER #include #include 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 const T &writeToEeprom(int address, size_t size, const T &t) { Serial.println("Writing at: " + String(address) + " size: " + String(sizeof(T))); if (sizeof(T) > size) { Serial.println("Error writing: Size should be: " + String(size)); return t; } const T &res = EEPROM.put(address, t); Serial.println("Eeprom commit returned: " + String(EEPROM.commit())); return res; } template T &readFromEeprom(int address, size_t size, T &t) { Serial.println("Reading at: " + String(address) + " size: " + String(sizeof(T))); if (sizeof(T) > size) { Serial.println("Error reading: Size should be: " + String(size)); return t; } T &res = EEPROM.get(address, t); return res; } }; class EepromUnit final { public: friend class EepromManager; template const T &write(const T &t) { return this->manager->writeToEeprom(this->address, this->size, t); }; template 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