|
25 | 25 |
|
26 | 26 | /***
|
27 | 27 | EERef class.
|
28 |
| - |
| 28 | +
|
29 | 29 | This object references an EEPROM cell.
|
30 | 30 | Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
|
31 | 31 | This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
|
32 | 32 | ***/
|
33 | 33 |
|
34 |
| -struct EERef{ |
| 34 | +struct EERef |
| 35 | +{ |
| 36 | + |
| 37 | + EERef(const int index) |
| 38 | + : index(index) {} |
35 | 39 |
|
36 |
| - EERef( const int index ) |
37 |
| - : index( index ) {} |
38 |
| - |
39 | 40 | //Access/read members.
|
40 |
| - uint8_t operator*() const { return eeprom_read_byte( /*(uint8_t*)*/ index ); } |
41 |
| - operator const uint8_t() const { return **this; } |
42 |
| - |
| 41 | + uint8_t operator*() const { return eeprom_read_byte(/*(uint8_t*)*/ index); } |
| 42 | + operator uint8_t() const { return **this; } |
| 43 | + |
43 | 44 | //Assignment/write members.
|
44 |
| - EERef &operator=( const EERef &ref ) { return *this = *ref; } |
45 |
| - EERef &operator=( uint8_t in ) { return eeprom_write_byte( /*(uint8_t*)*/ index, in ), *this; } |
46 |
| - EERef &operator +=( uint8_t in ) { return *this = **this + in; } |
47 |
| - EERef &operator -=( uint8_t in ) { return *this = **this - in; } |
48 |
| - EERef &operator *=( uint8_t in ) { return *this = **this * in; } |
49 |
| - EERef &operator /=( uint8_t in ) { return *this = **this / in; } |
50 |
| - EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } |
51 |
| - EERef &operator %=( uint8_t in ) { return *this = **this % in; } |
52 |
| - EERef &operator &=( uint8_t in ) { return *this = **this & in; } |
53 |
| - EERef &operator |=( uint8_t in ) { return *this = **this | in; } |
54 |
| - EERef &operator <<=( uint8_t in ) { return *this = **this << in; } |
55 |
| - EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } |
56 |
| - |
57 |
| - EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } |
58 |
| - |
| 45 | + EERef &operator=(const EERef &ref) { return *this = *ref; } |
| 46 | + EERef &operator=(uint8_t in) { return eeprom_write_byte(/*(uint8_t*)*/ index, in), *this; } |
| 47 | + EERef &operator+=(uint8_t in) { return *this = **this + in; } |
| 48 | + EERef &operator-=(uint8_t in) { return *this = **this - in; } |
| 49 | + EERef &operator*=(uint8_t in) { return *this = **this * in; } |
| 50 | + EERef &operator/=(uint8_t in) { return *this = **this / in; } |
| 51 | + EERef &operator^=(uint8_t in) { return *this = **this ^ in; } |
| 52 | + EERef &operator%=(uint8_t in) { return *this = **this % in; } |
| 53 | + EERef &operator&=(uint8_t in) { return *this = **this & in; } |
| 54 | + EERef &operator|=(uint8_t in) { return *this = **this | in; } |
| 55 | + EERef &operator<<=(uint8_t in) { return *this = **this << in; } |
| 56 | + EERef &operator>>=(uint8_t in) { return *this = **this >> in; } |
| 57 | + |
| 58 | + EERef &update(uint8_t in) { return in != *this ? *this = in : *this; } |
| 59 | + |
59 | 60 | /** Prefix increment/decrement **/
|
60 |
| - EERef& operator++() { return *this += 1; } |
61 |
| - EERef& operator--() { return *this -= 1; } |
62 |
| - |
| 61 | + EERef &operator++() { return *this += 1; } |
| 62 | + EERef &operator--() { return *this -= 1; } |
| 63 | + |
63 | 64 | /** Postfix increment/decrement **/
|
64 |
| - uint8_t operator++ (int){ |
| 65 | + uint8_t operator++(int) |
| 66 | + { |
65 | 67 | uint8_t ret = **this;
|
66 | 68 | return ++(*this), ret;
|
67 | 69 | }
|
68 | 70 |
|
69 |
| - uint8_t operator-- (int){ |
| 71 | + uint8_t operator--(int) |
| 72 | + { |
70 | 73 | uint8_t ret = **this;
|
71 | 74 | return --(*this), ret;
|
72 | 75 | }
|
73 |
| - |
| 76 | + |
74 | 77 | int index; //Index of current EEPROM cell.
|
75 | 78 | };
|
76 | 79 |
|
77 | 80 | /***
|
78 | 81 | EEPtr class.
|
79 |
| - |
| 82 | +
|
80 | 83 | This object is a bidirectional pointer to EEPROM cells represented by EERef objects.
|
81 |
| - Just like a normal pointer type, this can be dereferenced and repositioned using |
| 84 | + Just like a normal pointer type, this can be dereferenced and repositioned using |
82 | 85 | increment/decrement operators.
|
83 | 86 | ***/
|
84 | 87 |
|
85 |
| -struct EEPtr{ |
| 88 | +struct EEPtr |
| 89 | +{ |
| 90 | + |
| 91 | + EEPtr(const int index) |
| 92 | + : index(index) {} |
| 93 | + |
| 94 | + operator int() const { return index; } |
| 95 | + EEPtr &operator=(int in) { return index = in, *this; } |
86 | 96 |
|
87 |
| - EEPtr( const int index ) |
88 |
| - : index( index ) {} |
89 |
| - |
90 |
| - operator const int() const { return index; } |
91 |
| - EEPtr &operator=( int in ) { return index = in, *this; } |
92 |
| - |
93 | 97 | //Iterator functionality.
|
94 |
| - bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } |
95 |
| - EERef operator*() { return index; } |
96 |
| - |
| 98 | + bool operator!=(const EEPtr &ptr) { return index != ptr.index; } |
| 99 | + EERef operator*() { return index; } |
| 100 | + |
97 | 101 | /** Prefix & Postfix increment/decrement **/
|
98 |
| - EEPtr& operator++() { return ++index, *this; } |
99 |
| - EEPtr& operator--() { return --index, *this; } |
100 |
| - EEPtr operator++ (int) { return index++; } |
101 |
| - EEPtr operator-- (int) { return index--; } |
| 102 | + EEPtr &operator++() { return ++index, *this; } |
| 103 | + EEPtr &operator--() { return --index, *this; } |
| 104 | + EEPtr operator++(int) { return index++; } |
| 105 | + EEPtr operator--(int) { return index--; } |
102 | 106 |
|
103 | 107 | int index; //Index of current EEPROM cell.
|
104 | 108 | };
|
105 | 109 |
|
106 | 110 | /***
|
107 | 111 | EEPROMClass class.
|
108 |
| - |
| 112 | +
|
109 | 113 | This object represents the entire EEPROM space.
|
110 | 114 | It wraps the functionality of EEPtr and EERef into a basic interface.
|
111 | 115 | This class is also 100% backwards compatible with earlier Arduino core releases.
|
112 | 116 | ***/
|
113 | 117 |
|
114 |
| -struct EEPROMClass{ |
| 118 | +struct EEPROMClass |
| 119 | +{ |
115 | 120 |
|
116 | 121 | //Basic user access methods.
|
117 |
| - EERef operator[]( const int idx ) { return idx; } |
118 |
| - uint8_t read( int idx ) { return EERef( idx ); } |
119 |
| - void write( int idx, uint8_t val ) { (EERef( idx )) = val; } |
120 |
| - void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } |
121 |
| - |
| 122 | + EERef operator[](const int idx) { return idx; } |
| 123 | + uint8_t read(int idx) { return EERef(idx); } |
| 124 | + void write(int idx, uint8_t val) { (EERef(idx)) = val; } |
| 125 | + void update(int idx, uint8_t val) { EERef(idx).update(val); } |
| 126 | + |
122 | 127 | //STL and C++11 iteration capability.
|
123 |
| - EEPtr begin() { return 0x00; } |
124 |
| - EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. |
125 |
| - uint16_t length() { return E2END; } |
126 |
| - |
| 128 | + EEPtr begin() { return 0x00; } |
| 129 | + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. |
| 130 | + uint16_t length() { return E2END; } |
| 131 | + |
127 | 132 | //Functionality to 'get' and 'put' objects to and from EEPROM.
|
128 |
| - template< typename T > T &get( int idx, T &t ){ |
| 133 | + template <typename T> |
| 134 | + T &get(int idx, T &t) |
| 135 | + { |
129 | 136 | EEPtr e = idx;
|
130 |
| - uint8_t *ptr = (uint8_t*) &t; |
131 |
| - for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; |
| 137 | + uint8_t *ptr = (uint8_t *)&t; |
| 138 | + for (int count = sizeof(T); count; --count, ++e) |
| 139 | + *ptr++ = *e; |
132 | 140 | return t;
|
133 | 141 | }
|
134 |
| - |
135 |
| - template< typename T > const T &put( int idx, const T &t ){ |
| 142 | + |
| 143 | + template <typename T> |
| 144 | + const T &put(int idx, const T &t) |
| 145 | + { |
136 | 146 | EEPtr e = idx;
|
137 |
| - const uint8_t *ptr = (const uint8_t*) &t; |
138 |
| - for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); |
| 147 | + const uint8_t *ptr = (const uint8_t *)&t; |
| 148 | + for (int count = sizeof(T); count; --count, ++e) |
| 149 | + (*e).update(*ptr++); |
139 | 150 | return t;
|
140 | 151 | }
|
141 | 152 | };
|
142 | 153 |
|
143 |
| -static EEPROMClass EEPROM; |
| 154 | +EEPROMClass EEPROM; |
144 | 155 | #endif
|
0 commit comments