Skip to content

Commit f4f5333

Browse files
committed
Added String handling to EEPROM put() & get() functions.
Also added another overload for EEMEM get() overload.
1 parent ff78b24 commit f4f5333

File tree

1 file changed

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

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,48 @@ class EEPROMClass{
249249
//A helper function for the builtin eeprom_is_ready macro.
250250
bool ready() { return eeprom_is_ready(); }
251251

252-
//Functionality to 'get' and 'put' objects to and from EEPROM.
252+
253+
/*
254+
Functionality to 'get' and 'put' objects to and from EEPROM.
255+
All put() functions use the update() method of writing to the EEPROM
256+
*/
257+
258+
//Generic get() function, for any type of data.
253259
template< typename T > T &get( EEPtr ptr, T &t ){
254260
uint8_t *dest = (uint8_t*) &t;
255261
for( int count = sizeof(T) ; count ; --count, ++ptr ) *dest++ = *ptr;
256262
return t;
257263
}
258264

259-
//EEMEM helper: This function retrieves an object which uses the same type as the provided pointer.
265+
//EEMEM helper: This function retrieves an object which uses the same type as the provided object.
266+
template< typename T > T get( T &t ){ return get(&t); }
260267
template< typename T > T get( T *t ){
261268
T result;
262269
return get( t, result );
263270
}
271+
272+
//Overload get() function to deal with the String class.
273+
String &get( EEPtr ptr, String &t ){
274+
for( auto el : iterate(ptr, length() - ptr)){
275+
if(el) t += char(el);
276+
else break;
277+
}
278+
return t;
279+
}
264280

281+
//Generic put() function, for any type of data.
265282
template< typename T > const T &put( EEPtr ptr, const T &t ){
266283
const uint8_t *src = (const uint8_t*) &t;
267284
for( int count = sizeof(T) ; count ; --count, ++ptr ) (*ptr).update( *src++ );
268285
return t;
269286
}
287+
288+
//Overload of put() function to deal with the String class.
289+
const String &put( EEPtr ptr, const String &t ){
290+
uint16_t idx = 0;
291+
for(auto el : iterate(ptr, t.length() + 1)) el.update(t[idx++]); //Read past length() as String::operator[] returns 0 on an out of bounds read (for null terminator).
292+
return t;
293+
}
270294
};
271295

272296
static EEPROMClass EEPROM;

0 commit comments

Comments
 (0)