Repository navigation
Expand file tree
/
Copy pathconfig_store.cpp
More file actions
54 lines (48 loc) · 1.42 KB
/
Copy pathconfig_store.cpp
File metadata and controls
54 lines (48 loc) · 1.42 KB
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
#include "config_store.h"
#include <Preferences.h>
void loadConfig(Config& c) {
Preferences p;
p.begin("ocellus", true);
String s = p.getString("cfg", "");
p.end();
if (s.length()) configFromJson(std::string(s.c_str()), c);
}
bool saveConfig(const Config& c) {
Preferences p;
p.begin("ocellus", false);
// putString returns bytes written; 0 = nvs_set_str failed (its 4000 B string cap, or a full
// partition) with the OLD value left intact -- the unit then runs the new config from RAM and
// silently reverts at reboot. The caller must surface a false, not echo success.
size_t wrote = p.putString("cfg", configToJson(c).c_str());
p.end();
return wrote != 0;
}
uint32_t treatsLoad() {
Preferences p;
p.begin("ocellus", true);
uint32_t t = p.getUInt("treats", 0);
p.end();
return t;
}
void treatsSave(uint32_t treats) {
Preferences p;
p.begin("ocellus", false);
p.putUInt("treats", treats);
p.end();
}
uint8_t resumeIdLoad() {
Preferences p;
p.begin("ocellus", true);
uint8_t id = p.getUChar("lastid", 0xFF);
p.end();
return id;
}
// Its own one-byte key rather than a Config field: saving through gConfig would rewrite the whole
// config JSON (~400-700 B) on every pick, and drag the codec, the protocol and config.html along
// for a value the user never sets by hand.
void resumeIdSave(uint8_t id) {
Preferences p;
p.begin("ocellus", false);
p.putUChar("lastid", id);
p.end();
}