1
0

EEPROMStruct.h 595 B

123456789101112131415161718192021
  1. #include <avr/eeprom.h>
  2. #include <EEPROM.h>
  3. #include <Arduino.h> // for type definitions
  4. template <class T> int EEPROM_writeStruct(int address, const T& value)
  5. {
  6. const byte* p = (const byte*)(const void*)&value;
  7. unsigned int i;
  8. for (i = 0; i < sizeof(value); i++)
  9. eeprom_write_byte((unsigned char *)address++, *p++);
  10. return i;
  11. }
  12. template <class T> int EEPROM_readStruct(int address, T& value)
  13. {
  14. byte* p = (byte*)(void*)&value;
  15. unsigned int i;
  16. for (i = 0; i < sizeof(value); i++)
  17. *p++ = eeprom_read_byte((unsigned char *)address++);
  18. return i;
  19. }