Advertisement
Ilya_Bykonya

Untitled

Apr 15th, 2024
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.27 KB | Source Code | 0 0
  1. #include <EEPROM.h>
  2.  
  3.  
  4. template<typename Type>
  5. struct IProperty {
  6.   virtual ~IProperty() noexcept = default;
  7.   virtual void setValue(const Type&) = 0;
  8.   virtual const Type& value() const = 0;
  9. };
  10.  
  11. template<typename Type>
  12. class AbstractEepromProperty: public IProperty<Type> {
  13. protected:
  14.   EEPROMClass& m_eeprom;
  15.   const int m_address{};
  16.   Type m_value{};
  17. public:
  18.   AbstractEepromProperty(EEPROMClass& eeprom, int address)
  19.     :m_eeprom{ eeprom }, m_address{ address } {}
  20. public:
  21.   virtual void read_from_eeprom() = 0;
  22.   virtual void write_to_eeprom() = 0;
  23. public:
  24.   virtual const Type& value() const override final {
  25.     return m_value;
  26.   }
  27. };
  28.  
  29. template<size_t length>
  30. class ByteArrayEepromProperty: public AbstractEepromProperty<uint8_t[length]> {
  31. public:
  32.   ByteArrayEepromProperty(EEPROMClass& eeprom, int address)
  33.     :AbstractEepromProperty<uint8_t[length]>{ eeprom, address } {}
  34. public:
  35.   virtual void setValue(const uint8_t(&value)[length]) override {
  36.     memcpy(this->m_value, value, length);
  37.   }
  38.   virtual void read_from_eeprom() override final {
  39.     this->m_eeprom.readBytes(this->m_address, this->m_value, length);
  40.   }
  41.   virtual void write_to_eeprom() override final {
  42.     this->m_eeprom.writeBytes(this->m_address, this->m_value, length);
  43.   }
  44. };
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement