|
| 1 | +/* |
| 2 | + EEPROM.h - EEPROM library |
| 3 | + Original Copyright (c) 2006 David A. Mellis. All right reserved. |
| 4 | + New version by Christopher Andrews 2015. |
| 5 | +
|
| 6 | + Copy of https://github.com/arduino/ArduinoCore-megaavr/blob/c8a1dd996c783777ec46167cfd8ad3fd2e6df185/libraries/EEPROM/src/EEPROM.h |
| 7 | + modified by James Foster in 2020 to work with Arduino CI. |
| 8 | +
|
| 9 | + This library is free software; you can redistribute it and/or |
| 10 | + modify it under the terms of the GNU Lesser General Public |
| 11 | + License as published by the Free Software Foundation; either |
| 12 | + version 2.1 of the License, or (at your option) any later version. |
| 13 | +
|
| 14 | + This library is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + Lesser General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU Lesser General Public |
| 20 | + License along with this library; if not, write to the Free Software |
| 21 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | +*/ |
| 23 | + |
| 24 | +#ifndef EEPROM_h |
| 25 | +#define EEPROM_h |
| 26 | + |
| 27 | +#include <inttypes.h> |
| 28 | +#include <avr/io.h> |
| 29 | + |
| 30 | +// different EEPROM implementations have different macros that leak out |
| 31 | +#if !defined(EEPROM_SIZE) && defined(E2END) && (E2END) |
| 32 | +#define EEPROM_SIZE (E2END + 1) |
| 33 | +#endif |
| 34 | + |
| 35 | +// Does the current board have EEPROM? |
| 36 | +#ifndef EEPROM_SIZE |
| 37 | +// In lieu of an "EEPROM.h not found" error for unsupported boards |
| 38 | +#error "EEPROM library not available for your board" |
| 39 | +#endif |
| 40 | + |
| 41 | +// On a real device this would be in hardware, but we have a mock board! |
| 42 | +static uint8_t eeprom[EEPROM_SIZE]; |
| 43 | +inline uint8_t eeprom_read_byte( uint8_t* index ) { return eeprom[(unsigned long) index % EEPROM_SIZE]; } |
| 44 | +inline void eeprom_write_byte( uint8_t* index, uint8_t value ) { eeprom[(unsigned long) index % EEPROM_SIZE] = value; } |
| 45 | + |
| 46 | +// Everything following is from the original (referenced above) |
| 47 | + |
| 48 | +/*** |
| 49 | + EERef class. |
| 50 | + |
| 51 | + This object references an EEPROM cell. |
| 52 | + Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. |
| 53 | + This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. |
| 54 | +***/ |
| 55 | + |
| 56 | +struct EERef{ |
| 57 | + |
| 58 | + EERef( const int index ) |
| 59 | + : index( index ) {} |
| 60 | + |
| 61 | + //Access/read members. |
| 62 | + uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); } |
| 63 | + operator uint8_t() const { return **this; } |
| 64 | + |
| 65 | + //Assignment/write members. |
| 66 | + EERef &operator=( const EERef &ref ) { return *this = *ref; } |
| 67 | + EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; } |
| 68 | + EERef &operator +=( uint8_t in ) { return *this = **this + in; } |
| 69 | + EERef &operator -=( uint8_t in ) { return *this = **this - in; } |
| 70 | + EERef &operator *=( uint8_t in ) { return *this = **this * in; } |
| 71 | + EERef &operator /=( uint8_t in ) { return *this = **this / in; } |
| 72 | + EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } |
| 73 | + EERef &operator %=( uint8_t in ) { return *this = **this % in; } |
| 74 | + EERef &operator &=( uint8_t in ) { return *this = **this & in; } |
| 75 | + EERef &operator |=( uint8_t in ) { return *this = **this | in; } |
| 76 | + EERef &operator <<=( uint8_t in ) { return *this = **this << in; } |
| 77 | + EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } |
| 78 | + |
| 79 | + EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } |
| 80 | + |
| 81 | + /** Prefix increment/decrement **/ |
| 82 | + EERef& operator++() { return *this += 1; } |
| 83 | + EERef& operator--() { return *this -= 1; } |
| 84 | + |
| 85 | + /** Postfix increment/decrement **/ |
| 86 | + uint8_t operator++ (int){ |
| 87 | + uint8_t ret = **this; |
| 88 | + return ++(*this), ret; |
| 89 | + } |
| 90 | + |
| 91 | + uint8_t operator-- (int){ |
| 92 | + uint8_t ret = **this; |
| 93 | + return --(*this), ret; |
| 94 | + } |
| 95 | + |
| 96 | + int index; //Index of current EEPROM cell. |
| 97 | +}; |
| 98 | + |
| 99 | +/*** |
| 100 | + EEPtr class. |
| 101 | + |
| 102 | + This object is a bidirectional pointer to EEPROM cells represented by EERef objects. |
| 103 | + Just like a normal pointer type, this can be dereferenced and repositioned using |
| 104 | + increment/decrement operators. |
| 105 | +***/ |
| 106 | + |
| 107 | +struct EEPtr{ |
| 108 | + |
| 109 | + EEPtr( const int index ) |
| 110 | + : index( index ) {} |
| 111 | + |
| 112 | + operator int() const { return index; } |
| 113 | + EEPtr &operator=( int in ) { return index = in, *this; } |
| 114 | + |
| 115 | + //Iterator functionality. |
| 116 | + bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } |
| 117 | + EERef operator*() { return index; } |
| 118 | + |
| 119 | + /** Prefix & Postfix increment/decrement **/ |
| 120 | + EEPtr& operator++() { return ++index, *this; } |
| 121 | + EEPtr& operator--() { return --index, *this; } |
| 122 | + EEPtr operator++ (int) { return index++; } |
| 123 | + EEPtr operator-- (int) { return index--; } |
| 124 | + |
| 125 | + int index; //Index of current EEPROM cell. |
| 126 | +}; |
| 127 | + |
| 128 | +/*** |
| 129 | + EEPROMClass class. |
| 130 | + |
| 131 | + This object represents the entire EEPROM space. |
| 132 | + It wraps the functionality of EEPtr and EERef into a basic interface. |
| 133 | + This class is also 100% backwards compatible with earlier Arduino core releases. |
| 134 | +***/ |
| 135 | + |
| 136 | +struct EEPROMClass{ |
| 137 | + |
| 138 | + //Basic user access methods. |
| 139 | + EERef operator[]( const int idx ) { return idx; } |
| 140 | + uint8_t read( int idx ) { return EERef( idx ); } |
| 141 | + void write( int idx, uint8_t val ) { (EERef( idx )) = val; } |
| 142 | + void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } |
| 143 | + |
| 144 | + //STL and C++11 iteration capability. |
| 145 | + EEPtr begin() { return 0x00; } |
| 146 | + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. |
| 147 | + uint16_t length() { return EEPROM_SIZE; } |
| 148 | + |
| 149 | + //Functionality to 'get' and 'put' objects to and from EEPROM. |
| 150 | + template< typename T > T &get( int idx, T &t ){ |
| 151 | + EEPtr e = idx; |
| 152 | + uint8_t *ptr = (uint8_t*) &t; |
| 153 | + for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; |
| 154 | + return t; |
| 155 | + } |
| 156 | + |
| 157 | + template< typename T > const T &put( int idx, const T &t ){ |
| 158 | + EEPtr e = idx; |
| 159 | + const uint8_t *ptr = (const uint8_t*) &t; |
| 160 | + for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); |
| 161 | + return t; |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +static EEPROMClass EEPROM; |
| 166 | +#endif |
0 commit comments