Skip to content

Commit 2f67caf

Browse files
committed
Added extended iteration functionality to EEPROM library.
Now arbitrary ranges can be iterated. Also EEMEM variables can be serialized with this feature.
1 parent 6a8e3b9 commit 2f67caf

File tree

1 file changed

+55
-32
lines changed
  • hardware/arduino/avr/libraries/EEPROM/src

1 file changed

+55
-32
lines changed

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

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -206,38 +206,61 @@ inline EEPtr EERef::operator&() const { return index; } //Deferred definition ti
206206
This class is also 100% backwards compatible with earlier Arduino core releases.
207207
***/
208208

209-
struct EEPROMClass{
210-
211-
//Basic user access methods.
212-
EERef operator[]( EERef ref ) { return ref; }
213-
EERef read( EERef ref ) { return ref; }
214-
void write( EERef ref, uint8_t val ) { ref = val; }
215-
void update( EERef ref, uint8_t val ) { ref.update( val ); }
216-
217-
//STL and C++11 iteration capability.
218-
EEPtr begin() { return 0x00; }
219-
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
220-
uint16_t length() { return E2END + 1; }
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-
226-
//A helper function for the builtin eeprom_is_ready macro.
227-
bool ready() { return eeprom_is_ready(); }
228-
229-
//Functionality to 'get' and 'put' objects to and from EEPROM.
230-
template< typename T > T &get( EEPtr ptr, T &t ){
231-
uint8_t *dest = (uint8_t*) &t;
232-
for( int count = sizeof(T) ; count ; --count, ++ptr ) *dest++ = *ptr;
233-
return t;
234-
}
235-
236-
template< typename T > const T &put( EEPtr ptr, const T &t ){
237-
const uint8_t *src = (const uint8_t*) &t;
238-
for( int count = sizeof(T) ; count ; --count, ++ptr ) (*ptr).update( *src++ );
239-
return t;
240-
}
209+
class EEPROMClass{
210+
protected:
211+
212+
/***
213+
EEIterator interface.
214+
This interface allows creating customized ranges within
215+
the EEPROM. Essentially intended for use with ranged for
216+
loops, or STL style iteration on subsections of the EEPROM.
217+
***/
218+
219+
struct EEIterator{
220+
EEIterator( EEPtr _start, int _length ) : start(_start), length(_length) {}
221+
EEPtr begin() { return start; }
222+
EEPtr end() { return start + length; }
223+
EEPtr start;
224+
int length;
225+
};
226+
227+
public:
228+
229+
//Basic user access methods.
230+
EERef operator[]( EERef ref ) { return ref; }
231+
EERef read( EERef ref ) { return ref; }
232+
void write( EERef ref, uint8_t val ) { ref = val; }
233+
void update( EERef ref, uint8_t val ) { ref.update( val ); }
234+
235+
//STL and C++11 iteration capability.
236+
EEPtr begin() { return 0x00; }
237+
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
238+
uint16_t length() { return E2END + 1; }
239+
240+
//Extended iteration functionality (arbitrary regions).
241+
//These can make serialized reading/writing easy.
242+
template< typename T > EEIterator iterate( T *t ) { return EEIterator( t, sizeof(T) ); }
243+
EEIterator iterate( EEPtr ptr, int length ) { return EEIterator( ptr, length ); }
244+
245+
//Bit access methods.
246+
EEBit readBit( EERef ref, uint8_t bidx ) { return ref[ bidx ]; }
247+
void writeBit( EERef ref, uint8_t bidx, const bool val ) { ref[ bidx ] = val; }
248+
249+
//A helper function for the builtin eeprom_is_ready macro.
250+
bool ready() { return eeprom_is_ready(); }
251+
252+
//Functionality to 'get' and 'put' objects to and from EEPROM.
253+
template< typename T > T &get( EEPtr ptr, T &t ){
254+
uint8_t *dest = (uint8_t*) &t;
255+
for( int count = sizeof(T) ; count ; --count, ++ptr ) *dest++ = *ptr;
256+
return t;
257+
}
258+
259+
template< typename T > const T &put( EEPtr ptr, const T &t ){
260+
const uint8_t *src = (const uint8_t*) &t;
261+
for( int count = sizeof(T) ; count ; --count, ++ptr ) (*ptr).update( *src++ );
262+
return t;
263+
}
241264
};
242265

243266
static EEPROMClass EEPROM;

0 commit comments

Comments
 (0)