Skip to content

Commit e9ec113

Browse files
committed
Fix EEPROM get/put and add example.
Former-commit-id: 08aad7b
1 parent 9499ae3 commit e9ec113

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* n-able EEPROM demo
3+
*/
4+
#include <Arduino.h>
5+
#include <EEPROM.h>
6+
7+
void setup() {
8+
Serial.begin(115200);
9+
EEPROM.begin();
10+
11+
uint32_t item = 0;
12+
uint32_t count = 0;
13+
14+
Serial.println("EEPROM Demo started, press a key to see the bootcount.");
15+
16+
// wait for user input
17+
while(!Serial.available()) {
18+
delay(10);
19+
}
20+
21+
EEPROM.get(item, count);
22+
Serial.printf("Bootcount = %u\n", count);
23+
24+
count++;
25+
EEPROM.put(item, count);
26+
27+
systemRestart();
28+
}
29+
30+
void loop() {
31+
}

libraries/EEPROM/src/EEPROM.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ class EEPROMClass {
8585
//Functionality to 'get' and 'put' objects to and from EEPROM.
8686
template< typename T > T &get( int id, T &t ){
8787
static_assert(std::is_trivially_copyable<T>::value,"You can not use this type with EEPROM.get" ); // the code below only makes sense if you can "memcpy" T
88-
_store.read(id, (uint8_t*)t, sizeof(T));
88+
_store.read(id, (uint8_t*)&t, sizeof(T));
8989
return t;
9090
}
9191

9292
template< typename T > const T &put( int id, const T &t ){
9393
static_assert(std::is_trivially_copyable<T>::value, "You can not use this type with EEPROM.put"); // the code below only makes sense if you can "memcpy" T
94-
if(!_store.write(id, (uint8_t*)t, sizeof(T))) {
94+
if(!_store.write(id, (uint8_t*)&t, sizeof(T))) {
9595
_store.defrag();
96-
_store.write(id, (uint8_t*)t, sizeof(T));
96+
_store.write(id, (uint8_t*)&t, sizeof(T));
9797
}
9898
return t;
9999
}

0 commit comments

Comments
 (0)