-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.h
48 lines (38 loc) · 1.13 KB
/
eeprom.h
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
#include <EEPROM.h>
#include "definitions.h"
#define COMMA ,
#define DECL_VARS \
int* state_variables[] = { \
STORABLE_STATE_VARIABLES(&state.,COMMA) \
}; \
static_assert(sizeof(state_variables) / sizeof(int*) \
== sizeof(eeprom_default_state_vals) / sizeof(int), \
"State variable count doesnt match eeprom state variable count");
#define STATE_SIZE sizeof(eeprom_default_state_vals) / sizeof(int)
void read_state_from_eeprom(State& state)
{
DECL_VARS;
int read_state[STATE_SIZE];
EEPROM.get(STATE_ADDR, read_state);
for (int i = 0; i < STATE_SIZE; i++) {
*state_variables[i] = read_state[i];
}
}
void write_state_to_eeprom(State state)
{
DECL_VARS;
int writable_state[STATE_SIZE];
for (int i = 0; i < STATE_SIZE; i++) {
writable_state[i] = *state_variables[i];
}
EEPROM.put(STATE_ADDR, writable_state);
}
void initialize_default_state_to_eeprom()
{
EEPROM.put(STATE_ADDR, eeprom_default_state_vals);
EEPROM.put(MAGIC_NUMBER_ADDR, MAGIC_NUMBER);
}
bool eeprom_is_initialized()
{
return EEPROM.read(MAGIC_NUMBER_ADDR) == MAGIC_NUMBER;
}