Repository navigation
Expand file tree
/
Copy pathpower_config_store.cpp
More file actions
56 lines (48 loc) · 1.99 KB
/
Copy pathpower_config_store.cpp
File metadata and controls
56 lines (48 loc) · 1.99 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
55
56
#include "power_config_store.h"
#include <Preferences.h>
namespace {
constexpr const char *kNamespace = "snips_pwr";
} // namespace
PowerConfig PowerConfigStore::load() {
PowerConfig config; // defaults if nothing has been saved yet
Preferences prefs;
// Not readOnly: avoids a misleading "nvs_open failed: NOT_FOUND" log
// on a device that's never saved this namespace yet — see
// calibration_store.cpp's load() for the full explanation.
if (!prefs.begin(kNamespace, /*readOnly=*/false)) {
return config;
}
config.dimTimeoutSec = prefs.getInt("dimSec", config.dimTimeoutSec);
config.offTimeoutSec = prefs.getInt("offSec", config.offTimeoutSec);
config.xbeeSleepTimeoutSec =
prefs.getInt("xbeeSec", config.xbeeSleepTimeoutSec);
config.autoPoweroffTimeoutSec =
prefs.getInt("offAllSec", config.autoPoweroffTimeoutSec);
// Saved before each stage had its own Never option; save() drops it.
if (prefs.isKey("mode")) {
applyLegacyPowerMode(prefs.getInt("mode", 0), &config);
}
// New keys for the percentage settings: the old "ledBright" held a
// Low/Medium/High index, which would read as a near-zero percentage.
config.ledBrightnessPercent = BrightnessLevel::sanitize(
prefs.getInt("ledPct", config.ledBrightnessPercent));
config.screenBrightnessPercent = BrightnessLevel::sanitize(
prefs.getInt("scrPct", config.screenBrightnessPercent));
prefs.end();
return config;
}
void PowerConfigStore::save(const PowerConfig &config) {
Preferences prefs;
if (!prefs.begin(kNamespace, /*readOnly=*/false)) {
return;
}
// The Never timeouts now carry what the old mode did (see load()).
prefs.remove("mode");
prefs.putInt("dimSec", config.dimTimeoutSec);
prefs.putInt("offSec", config.offTimeoutSec);
prefs.putInt("xbeeSec", config.xbeeSleepTimeoutSec);
prefs.putInt("offAllSec", config.autoPoweroffTimeoutSec);
prefs.putInt("ledPct", config.ledBrightnessPercent);
prefs.putInt("scrPct", config.screenBrightnessPercent);
prefs.end();
}