|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | +#include <inttypes.h> |
| 5 | +#include <Godmode.h> |
| 6 | + |
| 7 | +// Does the current board have EEPROM? |
| 8 | +#ifndef EEPROM_SIZE |
| 9 | + // In lieu of an "EEPROM.h not found" error for unsupported boards |
| 10 | + #error "EEPROM library not available for your board" |
| 11 | +#endif |
| 12 | + |
| 13 | +class EEPROMClass { |
| 14 | +private: |
| 15 | + GodmodeState* state; |
| 16 | +public: |
| 17 | + // constructor |
| 18 | + EEPROMClass() { |
| 19 | + state = GODMODE(); |
| 20 | + } |
| 21 | + // array subscript operator |
| 22 | + uint8_t &operator[](const int index) { |
| 23 | + assert(index < EEPROM_SIZE); |
| 24 | + return state->eeprom[index]; |
| 25 | + } |
| 26 | + |
| 27 | + uint8_t read(const int index) { |
| 28 | + assert(index < EEPROM_SIZE); |
| 29 | + return state->eeprom[index]; |
| 30 | + } |
| 31 | + |
| 32 | + void write(const int index, const uint8_t value) { |
| 33 | + assert(index < EEPROM_SIZE); |
| 34 | + state->eeprom[index] = value; |
| 35 | + } |
| 36 | + |
| 37 | + void update(const int index, const uint8_t value) { |
| 38 | + assert(index < EEPROM_SIZE); |
| 39 | + state->eeprom[index] = value; |
| 40 | + } |
| 41 | + |
| 42 | + uint16_t length() { return EEPROM_SIZE; } |
| 43 | + |
| 44 | + // read any object |
| 45 | + template <typename T> T &get(const int index, T &object) { |
| 46 | + uint8_t *ptr = (uint8_t *)&object; |
| 47 | + for (int i = 0; i < sizeof(T); ++i) { |
| 48 | + *ptr++ = read(index + i); |
| 49 | + } |
| 50 | + return object; |
| 51 | + } |
| 52 | + |
| 53 | + // write any object |
| 54 | + template <typename T> const T &put(const int index, T &object) { |
| 55 | + const uint8_t *ptr = (const uint8_t *)&object; |
| 56 | + for (int i = 0; i < sizeof(T); ++i) { |
| 57 | + write(index + i, *ptr++); |
| 58 | + } |
| 59 | + return object; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +// global available in Godmode.cpp |
| 64 | +extern EEPROMClass EEPROM; |
0 commit comments