Skip to content

Commit 6a8e3b9

Browse files
committed
Added bit handling to EEPROM library.
Now reading and writing of individual bits is possible.
1 parent db80e32 commit 6a8e3b9

File tree

1 file changed

+86
-2
lines changed
  • hardware/arduino/avr/libraries/EEPROM/src

1 file changed

+86
-2
lines changed

hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <avr/io.h>
2727

2828
struct EEPtr; //Forward declaration for EERef::opreator&
29+
struct EEBit; //Forward declaration for EERef::opreator[]
2930

3031
/***
3132
EERef class.
@@ -45,6 +46,11 @@ struct EERef{
4546
operator uint8_t() const { return **this; }
4647

4748
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();
4854

4955
//Assignment/write members.
5056
EERef &operator=( const EERef &ref ) { return *this = *ref; }
@@ -62,11 +68,11 @@ struct EERef{
6268

6369
EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; }
6470

65-
/** Prefix increment/decrement **/
71+
// Prefix increment/decrement
6672
EERef& operator++() { return *this += 1; }
6773
EERef& operator--() { return *this -= 1; }
6874

69-
/** Postfix increment/decrement **/
75+
// Postfix increment/decrement
7076
uint8_t operator++ (int){
7177
uint8_t ret = **this;
7278
return ++(*this), ret;
@@ -80,6 +86,80 @@ struct EERef{
8086
int index; //Index of current EEPROM cell.
8187
};
8288

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 &copy ) { return *this = ( const bool ) copy; }
110+
111+
EEBit &operator =( const bool &copy ){
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+
83163
/***
84164
EEPtr class.
85165
@@ -139,6 +219,10 @@ struct EEPROMClass{
139219
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
140220
uint16_t length() { return E2END + 1; }
141221

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+
142226
//A helper function for the builtin eeprom_is_ready macro.
143227
bool ready() { return eeprom_is_ready(); }
144228

0 commit comments

Comments
 (0)