Small tweaks to EEPROM lib and examples.

This commit is contained in:
Chris--A 2015-03-19 17:13:32 +10:00
parent c9ec4eabda
commit fd4323f360
3 changed files with 14 additions and 26 deletions

View File

@ -92,28 +92,16 @@ struct EEPtr{
operator const int() const { return index; } operator const int() const { return index; }
EEPtr &operator=( int in ) { return index = in, *this; } EEPtr &operator=( int in ) { return index = in, *this; }
//Iterator functionality. //Iterator functionality.
bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
EERef operator*() { return( this->index ); } EERef operator*() { return index; }
/** Prefix & Postfix increment/decrement **/
/** Prefix increment/decrement **/
EEPtr& operator++() { return ++index, *this; } EEPtr& operator++() { return ++index, *this; }
EEPtr& operator--() { return --index, *this; } EEPtr& operator--() { return --index, *this; }
EEPtr operator++ (int) { return index++; }
EEPtr operator-- (int) { return index--; }
/** Postfix increment/decrement **/
EEPtr operator++ (int){
int ret = index;
return ++index, ret;
}
EEPtr operator-- (int){
int ret = index;
return --index, ret;
}
int index; //Index of current EEPROM cell. int index; //Index of current EEPROM cell.
}; };
@ -128,15 +116,15 @@ struct EEPtr{
struct EEPROMClass{ struct EEPROMClass{
//Basic user access methods. //Basic user access methods.
EERef operator[]( const int index ) { return( index ); } EERef operator[]( const int idx ) { return idx; }
uint8_t read( int idx ) { return (EERef( idx )); } uint8_t read( int idx ) { return EERef( idx ); }
void write( int idx, uint8_t val ) { (EERef( idx )) = val; } void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
//STL and C++11 iteration capability. //STL and C++11 iteration capability.
EEPtr begin() { return( 0x00 ); } EEPtr begin() { return 0x00; }
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
uint16_t length() { return E2END + 1; } uint16_t length() { return E2END + 1; }
//Functionality to 'get' and 'put' objects to and from EEPROM. //Functionality to 'get' and 'put' objects to and from EEPROM.
template< typename T > T &get( int idx, T &t ){ template< typename T > T &get( int idx, T &t ){

View File

@ -42,14 +42,14 @@ void setup() {
Iterate the EEPROM using a do-while loop. Iterate the EEPROM using a do-while loop.
***/ ***/
int idx = 0; int idx = 0; //Used 'idx' to avoid name conflict with 'index' above.
do{ do{
//Add one to each cell in the EEPROM //Add one to each cell in the EEPROM
EEPROM[ index ] += 1; EEPROM[ idx ] += 1;
index++; idx++;
}while( index < EEPROM.length() ); }while( idx < EEPROM.length() );
/*** /***
Iterate the EEPROM using a C++11 ranged for loop. Iterate the EEPROM using a C++11 ranged for loop.

View File

@ -54,7 +54,7 @@ void setup() {
of the C++11 ranged for loop. of the C++11 ranged for loop.
***/ ***/
for( EEPtr ptr = EEPROM.begin() ; item != EEPROM.end() ; ++item ){ for( EEPtr ptr = EEPROM.begin() ; ptr != EEPROM.end() ; ++ptr ){
Serial.print( *ptr, HEX ); Serial.print( *ptr, HEX );
Serial.print( ", " ); Serial.print( ", " );
} }