Skip to content

Split EEBit into a pointer and reference type #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: EEPROM_2.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,41 @@ struct EERef{
struct EEBit{

//Constructor, use by passing in index of EEPROM byte, then index of bit to read.
EEBit( int index, uint8_t bidx )
: ref( index ), mask( 0x01 << bidx ) {}
EEBit( EEBitPtr ptr)
: ptr( ptr ) {}

//Modifier functions.
EEBit &setIndex( uint8_t bidx ) { return mask = (0x01 << bidx), *this; }
EEBit &set() { return *this = true; }
EEBit &clear() { return *this = false; }

//Read/write functions.
operator bool() const { return ref & mask; }
EEBit &operator =( const EEBit &copy ) { return *this = ( const bool ) copy; }
operator bool() const { return *ptr.ref & ptr.mask; }
EEBit &operator =( const EEBit &copy ) { return *this = ( bool ) copy; }

EEBit &operator =( const bool &copy ){
if( copy ) ref |= mask;
else ref &= ~mask;
EERef cell = *ptr.ref;
if( copy ) cell |= ptr.mask;
else cell &= ~mask;
return *this;
}

const EEBitPtr ptr;
}

struct EEBitPtr {
//Constructor, use by passing in index of EEPROM byte, then index of bit to read.
EEBitPtr( int index, uint8_t bidx )
: ptr( index ), mask( 0x01 << bidx ) {}

//Iterator functionality.
EEBit& operator*() { return *this; }
bool operator==( const EEBit &bit ) { return (mask == bit.mask) && (ref.index == bit.ref.index); }
bool operator!=( const EEBit &bit ) { return !(*this == bit); }
EEBit operator*() const { return EEBit(*this); }
bool operator==( const EEBitPtr &bit ) const { return (mask == bit.mask) && (ptr == bit.ptr); }
bool operator!=( const EEBitPtr &bit ) const { return !(*this == bit); }

//Prefix & Postfix increment/decrement
EEBit& operator++(){
if( mask & 0x80 ){
++ref.index;
++ptr;
mask = 0x01;
}else{
mask <<= 1;
Expand All @@ -137,7 +145,7 @@ struct EEBit{

EEBit& operator--(){
if( mask & 0x01 ){
--ref.index;
--ptr;
mask = 0x80;
}else{
mask >>= 1;
Expand All @@ -155,14 +163,17 @@ struct EEBit{
return --(*this), cpy;
}

EERef ref; //Reference to EEPROM cell.
EEPtr ptr; //Reference to EEPROM cell.
uint8_t mask; //Mask of bit to read/write.
};

//Deferred definition till EEBitPtr becomes available.
inline EEBitPtr EEBit::operator&() const { return ptr; }

//Deferred definition till EEBit becomes available.
inline EEBit EERef::operator[]( const int bidx ) { return EEBit( index, bidx ); }
inline EEBit EERef::begin() { return EEBit( index, 0 ); }
inline EEBit EERef::end() { return EEBit( index + 1, 0 ); }
inline EEBit EERef::operator[]( const int bidx ) { return *EEBitPtr( index, bidx ); }
inline EEBitPtr EERef::begin() { return EEBitPtr( index, 0 ); }
inline EEBitPtr EERef::end() { return EEBitPtr( index + 1, 0 ); }


/***
Expand Down