-
Notifications
You must be signed in to change notification settings - Fork 3
/
fredStructConfig.h
49 lines (40 loc) · 1.43 KB
/
fredStructConfig.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
49
#ifndef FREDSTRUCTCONFIG_H_
#define FREDSTRUCTCONFIG_H_
template<typename datatype> class StructConfig {
static const uint8_t configExistsOffset = 0;
static const uint8_t configDataOffset = 1;
const int baseAddress;
const datatype* defaultConfig;
datatype* config;
public:
StructConfig(const int baseAddress, const datatype* defaultConfig, datatype* config) :
baseAddress(baseAddress), defaultConfig(defaultConfig), config(config) {
}
//restore the default config
void restoreDefault() {
setConfigExists(false);
*config = *defaultConfig;
}
//check if the data in eeprom is valid
bool getConfigExists() {
return EEPROM.read(baseAddress + configExistsOffset);
}
//sets if the data in eeprom is valid
void setConfigExists(bool exists) {
EEPROM.update(baseAddress + configExistsOffset, exists ? 255 : 0);
}
//loads config from eeprom, or the default if it doesnt exist yet
void loadConfig() {
if (getConfigExists()) {
EEPROM.get(baseAddress + configDataOffset, *config);
} else { //if it doesnt exist, lets just load the default
*config = *defaultConfig;
}
}
//saves config to eeprom and sets exists to true
void saveConfig() {
EEPROM.put(baseAddress + configDataOffset, *config);
setConfigExists(true);
}
};
#endif /* LIBRARIES_FREDUTIL_FREDSTRUCTCONFIG_H_ */