-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathExample2_Structures.ino
66 lines (52 loc) · 1.7 KB
/
Example2_Structures.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
// This file is subject to the terms and conditions defined in
// file 'LICENSE.md', which is part of this source code package.
*/
#include "EEPROM.h"
#define EEPROM_PREFS_IDX (0)
#define EEPROM_VALID_CODE (0xAB)
#define DEFAULT_VER_MAJOR (0)
#define DEFAULT_VER_MINOR (0)
#define DEFAULT_VER_PATCH (0)
typedef struct {
uint8_t valid = EEPROM_VALID_CODE;
uint8_t ver_major = DEFAULT_VER_MAJOR;
uint8_t ver_minor = DEFAULT_VER_MINOR;
uint8_t ver_patch = DEFAULT_VER_PATCH;
} preferences_t;
preferences_t prefs;
void setup() {
Serial.begin(115200);
Serial.println("EEPROM Example2_Structures");
EEPROM.init();
// use EEPROM.get(int index, T type) to retrieve
// an arbitrary type from flash memory
prefs.valid = 0x00;
EEPROM.get(EEPROM_PREFS_IDX, prefs);
if(prefs.valid != EEPROM_VALID_CODE){
Serial.println("EEPROM was invalid");
// use EEPROM to store the default structure
preferences_t default_prefs;
EEPROM.put(EEPROM_PREFS_IDX, default_prefs);
Serial.println("EEPROM initialized");
}
// verify that the prefs are valid
EEPROM.get(EEPROM_PREFS_IDX, prefs);
if(prefs.valid != EEPROM_VALID_CODE){
Serial.println("ERROR");
while(1){};
}
Serial.println("EEPROM is valid");
Serial.printf("version: %d.%d.%d\n", prefs.ver_major, prefs.ver_minor, prefs.ver_patch);
Serial.printf("\nany characters received over SERIAL will increment the patch version and be stored after power-down\n");
}
void loop() {
if(Serial.available()){
while(Serial.available()){
Serial.read();
prefs.ver_patch++;
}
EEPROM.put(EEPROM_PREFS_IDX, prefs);
Serial.printf("version: %d.%d.%d\n", prefs.ver_major, prefs.ver_minor, prefs.ver_patch);
}
}