1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-26 20:54:22 +01:00

Small tweaks to EEPROM lib and examples.

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

View File

@ -92,28 +92,16 @@ struct EEPtr{
operator const int() const { return index; }
EEPtr &operator=( int in ) { return index = in, *this; }
//Iterator functionality.
bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
EERef operator*() { return( this->index ); }
EERef operator*() { return index; }
/** Prefix increment/decrement **/
/** Prefix & Postfix increment/decrement **/
EEPtr& operator++() { return ++index, *this; }
EEPtr& operator--() { return --index, *this; }
/** Postfix increment/decrement **/
EEPtr operator++ (int){
int ret = index;
return ++index, ret;
}
EEPtr operator++ (int) { return index++; }
EEPtr operator-- (int) { return index--; }
EEPtr operator-- (int){
int ret = index;
return --index, ret;
}
int index; //Index of current EEPROM cell.
};
@ -128,15 +116,15 @@ struct EEPtr{
struct EEPROMClass{
//Basic user access methods.
EERef operator[]( const int index ) { return( index ); }
uint8_t read( int idx ) { return (EERef( idx )); }
EERef operator[]( const int idx ) { return idx; }
uint8_t read( int idx ) { return EERef( idx ); }
void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
//STL and C++11 iteration capability.
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.
uint16_t length() { return E2END + 1; }
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.
uint16_t length() { return E2END + 1; }
//Functionality to 'get' and 'put' objects to and from EEPROM.
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.
***/
int idx = 0;
int idx = 0; //Used 'idx' to avoid name conflict with 'index' above.
do{
//Add one to each cell in the EEPROM
EEPROM[ index ] += 1;
index++;
}while( index < EEPROM.length() );
EEPROM[ idx ] += 1;
idx++;
}while( idx < EEPROM.length() );
/***
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.
***/
for( EEPtr ptr = EEPROM.begin() ; item != EEPROM.end() ; ++item ){
for( EEPtr ptr = EEPROM.begin() ; ptr != EEPROM.end() ; ++ptr ){
Serial.print( *ptr, HEX );
Serial.print( ", " );
}