26
26
#include < avr/io.h>
27
27
28
28
struct EEPtr ; // Forward declaration for EERef::opreator&
29
+ struct EEBit ; // Forward declaration for EERef::opreator[]
29
30
30
31
/* **
31
32
EERef class.
@@ -45,6 +46,11 @@ struct EERef{
45
46
operator uint8_t () const { return **this ; }
46
47
47
48
EEPtr operator &() const ; // Defined below EEPtr
49
+
50
+ // Bit access members, defined below EEBit declaration.
51
+ EEBit operator []( const int bidx );
52
+ EEBit begin ();
53
+ EEBit end ();
48
54
49
55
// Assignment/write members.
50
56
EERef &operator =( const EERef &ref ) { return *this = *ref; }
@@ -62,11 +68,11 @@ struct EERef{
62
68
63
69
EERef &update ( uint8_t in ) { return in != *this ? *this = in : *this ; }
64
70
65
- /* * Prefix increment/decrement * */
71
+ // Prefix increment/decrement
66
72
EERef& operator ++() { return *this += 1 ; }
67
73
EERef& operator --() { return *this -= 1 ; }
68
74
69
- /* * Postfix increment/decrement * */
75
+ // Postfix increment/decrement
70
76
uint8_t operator ++ (int ){
71
77
uint8_t ret = **this ;
72
78
return ++(*this ), ret;
@@ -80,6 +86,80 @@ struct EERef{
80
86
int index; // Index of current EEPROM cell.
81
87
};
82
88
89
+
90
+ /* **
91
+ EEBit class.
92
+ This object is a reference object similar to EERef, however it references
93
+ only a single bit. Its function mimics a bool type.
94
+ ***/
95
+
96
+ struct EEBit {
97
+
98
+ // Constructor, use by passing in index of EEPROM byte, then index of bit to read.
99
+ EEBit ( int index, uint8_t bidx )
100
+ : ref( index ), mask( 0x01 << bidx ) {}
101
+
102
+ // Modifier functions.
103
+ EEBit &setIndex ( uint8_t bidx ) { return mask = (0x01 << bidx), *this ; }
104
+ EEBit &set () { return *this = true ; }
105
+ EEBit &clear () { return *this = false ; }
106
+
107
+ // Read/write functions.
108
+ operator bool () const { return ref & mask; }
109
+ EEBit &operator =( const EEBit © ) { return *this = ( const bool ) copy; }
110
+
111
+ EEBit &operator =( const bool © ){
112
+ if ( copy ) ref |= mask;
113
+ else ref &= ~mask;
114
+ return *this ;
115
+ }
116
+
117
+ // Iterator functionality.
118
+ EEBit& operator *() { return *this ; }
119
+ bool operator ==( const EEBit &bit ) { return (mask == bit.mask ) && (ref.index == bit.ref .index ); }
120
+ bool operator !=( const EEBit &bit ) { return !(*this == bit); }
121
+
122
+ // Prefix & Postfix increment/decrement
123
+ EEBit& operator ++(){
124
+ if ( mask & 0x80 ){
125
+ ++ref.index ;
126
+ mask = 0x01 ;
127
+ }else {
128
+ mask <<= 1 ;
129
+ }
130
+ return *this ;
131
+ }
132
+
133
+ EEBit& operator --(){
134
+ if ( mask & 0x01 ){
135
+ --ref.index ;
136
+ mask = 0x80 ;
137
+ }else {
138
+ mask >>= 1 ;
139
+ }
140
+ return *this ;
141
+ }
142
+
143
+ EEBit operator ++ (int ) {
144
+ EEBit cpy = *this ;
145
+ return ++(*this ), cpy;
146
+ }
147
+
148
+ EEBit operator -- (int ) {
149
+ EEBit cpy = *this ;
150
+ return --(*this ), cpy;
151
+ }
152
+
153
+ EERef ref; // Reference to EEPROM cell.
154
+ uint8_t mask; // Mask of bit to read/write.
155
+ };
156
+
157
+ // Deferred definition till EEBit becomes available.
158
+ inline EEBit EERef::operator []( const int bidx ) { return EEBit ( index, bidx ); }
159
+ inline EEBit EERef::begin () { return EEBit ( index, 0 ); }
160
+ inline EEBit EERef::end () { return EEBit ( index + 1 , 0 ); }
161
+
162
+
83
163
/* **
84
164
EEPtr class.
85
165
@@ -139,6 +219,10 @@ struct EEPROMClass{
139
219
EEPtr end () { return length (); } // Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
140
220
uint16_t length () { return E2END + 1 ; }
141
221
222
+ // Bit access methods.
223
+ EEBit readBit ( EERef ref, uint8_t bidx ) { return ref[ bidx ]; }
224
+ void writeBit ( EERef ref, uint8_t bidx, const bool val ) { ref[ bidx ] = val; }
225
+
142
226
// A helper function for the builtin eeprom_is_ready macro.
143
227
bool ready () { return eeprom_is_ready (); }
144
228
0 commit comments