Repository navigation
Expand file tree
/
Copy pathsettings.cpp
More file actions
302 lines (258 loc) · 9.89 KB
/
Copy pathsettings.cpp
File metadata and controls
302 lines (258 loc) · 9.89 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "settings.h"
#include <Arduino.h>
#include <Preferences.h>
#include "config.h"
namespace settings {
namespace {
// Namespace and keys are unchanged from the first firmware so units that are
// already in the field keep their stored thresholds across an update.
constexpr char kNamespace[] = "traffic";
constexpr char kKeyDbMin[] = "dB_min";
constexpr char kKeyDbMax[] = "dB_max";
constexpr char kKeyBright[] = "bright";
constexpr char kKeyScreen[] = "screen_bri";
constexpr char kKeyGroup[] = "group";
constexpr char kKeyUnit[] = "unit";
constexpr char kKeyGrpLevel[] = "grp_level";
constexpr char kKeyZones[] = "zones";
constexpr char kKeyCombine[] = "combine";
constexpr char kKeyInactive[] = "inactive";
constexpr char kKeySsid[] = "wifi_ssid";
constexpr char kKeyPass[] = "wifi_pass";
// Quiet period after the last change before anything is written to flash.
constexpr uint32_t kFlushDelayMs = 3000;
Preferences prefs;
// Sized to the 802.11 maxima: 32-character SSID, 63-character WPA2 passphrase.
char wifiSsidBuf[33] = {0};
char wifiPassBuf[64] = {0};
// Kept out of Settings so the flush can compare it with a strcmp rather than
// the field-by-field equality the numeric settings use.
char groupNameBuf[17] = {0};
char groupNameStored[17] = {0};
char unitNameBuf[GROUP_NAME_MAX + 1] = {0};
char unitNameStored[GROUP_NAME_MAX + 1] = {0};
Settings current;
Settings stored;
bool dirty = false;
uint32_t dirtySinceMs = 0;
int clampInt(int value, int low, int high) {
if (value < low) return low;
if (value > high) return high;
return value;
}
void markDirty() {
dirty = true;
dirtySinceMs = millis();
}
// Only fields that actually differ are written, so a burst of edits that ends
// where it started costs nothing.
void writeNow() {
if (current.dbMin != stored.dbMin) prefs.putUInt(kKeyDbMin, current.dbMin);
if (current.dbMax != stored.dbMax) prefs.putUInt(kKeyDbMax, current.dbMax);
if (current.brightness != stored.brightness) prefs.putUInt(kKeyBright, current.brightness);
if (current.displayBrightness != stored.displayBrightness)
prefs.putUInt(kKeyScreen, current.displayBrightness);
if (current.groupLevel != stored.groupLevel)
prefs.putUInt(kKeyGrpLevel, current.groupLevel ? 1 : 0);
if (current.zones != stored.zones) prefs.putUInt(kKeyZones, current.zones);
if (current.combine != stored.combine) prefs.putUInt(kKeyCombine, current.combine);
if (current.inactiveLevel != stored.inactiveLevel)
prefs.putUInt(kKeyInactive, current.inactiveLevel);
if (strcmp(groupNameBuf, groupNameStored) != 0) {
prefs.putString(kKeyGroup, groupNameBuf);
strncpy(groupNameStored, groupNameBuf, sizeof(groupNameStored) - 1);
}
if (strcmp(unitNameBuf, unitNameStored) != 0) {
prefs.putString(kKeyUnit, unitNameBuf);
strncpy(unitNameStored, unitNameBuf, sizeof(unitNameStored) - 1);
}
stored = current;
dirty = false;
}
} // namespace
void begin() {
// Establish a known state rather than relying on static initialisation, so
// begin() is a real reset point. The credential buffers matter in
// particular: getString leaves them untouched when the key is absent, so
// without clearing them first a missing key would read as whatever was
// there before.
dirty = false;
dirtySinceMs = 0;
wifiSsidBuf[0] = '\0';
wifiPassBuf[0] = '\0';
groupNameBuf[0] = '\0';
groupNameStored[0] = '\0';
unitNameBuf[0] = '\0';
unitNameStored[0] = '\0';
// Opened once and left open; closing and reopening per access costs several
// milliseconds and gains nothing.
if (!prefs.begin(kNamespace, /*readOnly=*/false)) {
Serial.println(F("settings: NVS unavailable, using defaults for this session"));
current = {DB_MIN_DEFAULT, DB_MAX_DEFAULT,
LED_BRIGHTNESS_DEFAULT, DISPLAY_BRIGHTNESS_DEFAULT,
false, ZONE_MASK_ALL,
COMBINE_DEFAULT, ZONE_INACTIVE_LEVEL_DEFAULT};
stored = current;
dirty = false;
return;
}
current.dbMin = clampInt(prefs.getUInt(kKeyDbMin, DB_MIN_DEFAULT), DB_LIMIT_LOW, DB_LIMIT_HIGH);
current.dbMax = clampInt(prefs.getUInt(kKeyDbMax, DB_MAX_DEFAULT), DB_LIMIT_LOW, DB_LIMIT_HIGH);
current.brightness = clampInt(prefs.getUInt(kKeyBright, LED_BRIGHTNESS_DEFAULT),
LED_BRIGHTNESS_MIN, LED_BRIGHTNESS_MAX);
current.displayBrightness =
clampInt(prefs.getUInt(kKeyScreen, DISPLAY_BRIGHTNESS_DEFAULT),
DISPLAY_BRIGHTNESS_MIN, DISPLAY_BRIGHTNESS_MAX);
// A unit flashed with an older build may hold a pair that violates the span.
if (current.dbMax < current.dbMin + DB_MIN_SPAN) {
current.dbMax = clampInt(current.dbMin + DB_MIN_SPAN, DB_LIMIT_LOW, DB_LIMIT_HIGH);
current.dbMin = clampInt(current.dbMax - DB_MIN_SPAN, DB_LIMIT_LOW, DB_LIMIT_HIGH);
}
current.groupLevel = prefs.getUInt(kKeyGrpLevel, 0) != 0;
current.zones = clampInt(prefs.getUInt(kKeyZones, ZONE_MASK_ALL), 0, ZONE_MASK_ALL);
current.combine = clampInt(prefs.getUInt(kKeyCombine, COMBINE_DEFAULT),
COMBINE_LOUDEST, COMBINE_AVERAGE);
current.inactiveLevel = clampInt(prefs.getUInt(kKeyInactive, ZONE_INACTIVE_LEVEL_DEFAULT),
0, LED_BRIGHTNESS_MAX);
// A unit whose mask ended up empty would never light at all, which reads as
// a dead unit rather than a configuration mistake.
if (current.zones == 0) current.zones = ZONE_MASK_ALL;
prefs.getString(kKeySsid, wifiSsidBuf, sizeof(wifiSsidBuf));
prefs.getString(kKeyPass, wifiPassBuf, sizeof(wifiPassBuf));
prefs.getString(kKeyGroup, groupNameBuf, sizeof(groupNameBuf));
strncpy(groupNameStored, groupNameBuf, sizeof(groupNameStored) - 1);
prefs.getString(kKeyUnit, unitNameBuf, sizeof(unitNameBuf));
strncpy(unitNameStored, unitNameBuf, sizeof(unitNameStored) - 1);
stored = current;
dirty = false;
}
const Settings& get() { return current; }
void setDbMin(int value) {
const uint8_t next = clampInt(value, DB_LIMIT_LOW, current.dbMax - DB_MIN_SPAN);
if (next == current.dbMin) return;
current.dbMin = next;
markDirty();
}
void setDbMax(int value) {
const uint8_t next = clampInt(value, current.dbMin + DB_MIN_SPAN, DB_LIMIT_HIGH);
if (next == current.dbMax) return;
current.dbMax = next;
markDirty();
}
void applyPreset(Preset preset) {
uint8_t targetMin = 0, targetMax = 0;
switch (preset) {
case Preset::Exam:
targetMin = PRESET_EXAM_MIN;
targetMax = PRESET_EXAM_MAX;
break;
case Preset::QuietWork:
targetMin = PRESET_QUIET_MIN;
targetMax = PRESET_QUIET_MAX;
break;
case Preset::GroupWork:
targetMin = PRESET_GROUP_MIN;
targetMax = PRESET_GROUP_MAX;
break;
default:
return;
}
if (targetMin >= current.dbMin) {
setDbMax(targetMax);
setDbMin(targetMin);
} else {
setDbMin(targetMin);
setDbMax(targetMax);
}
}
Preset activePreset() {
if (current.dbMin == PRESET_EXAM_MIN && current.dbMax == PRESET_EXAM_MAX) {
return Preset::Exam;
}
if (current.dbMin == PRESET_QUIET_MIN && current.dbMax == PRESET_QUIET_MAX) {
return Preset::QuietWork;
}
if (current.dbMin == PRESET_GROUP_MIN && current.dbMax == PRESET_GROUP_MAX) {
return Preset::GroupWork;
}
return Preset::Custom;
}
const char* presetName(Preset preset) {
switch (preset) {
case Preset::Exam: return "exam";
case Preset::QuietWork: return "quiet";
case Preset::GroupWork: return "group";
default: return "custom";
}
}
void setBrightness(int value) {
const uint8_t next = clampInt(value, LED_BRIGHTNESS_MIN, LED_BRIGHTNESS_MAX);
if (next == current.brightness) return;
current.brightness = next;
markDirty();
}
void setDisplayBrightness(int value) {
const uint8_t next = clampInt(value, DISPLAY_BRIGHTNESS_MIN, DISPLAY_BRIGHTNESS_MAX);
if (next == current.displayBrightness) return;
current.displayBrightness = next;
markDirty();
}
void setGroupLevel(bool enabled) {
if (enabled == current.groupLevel) return;
current.groupLevel = enabled;
markDirty();
}
void setZones(int mask) {
const uint8_t next = clampInt(mask, 0, ZONE_MASK_ALL);
// Refuse to leave a unit with nothing to light; that looks like a fault.
if (next == 0 || next == current.zones) return;
current.zones = next;
markDirty();
}
void setCombine(int mode) {
const uint8_t next = clampInt(mode, COMBINE_LOUDEST, COMBINE_AVERAGE);
if (next == current.combine) return;
current.combine = next;
markDirty();
}
void setInactiveLevel(int value) {
const uint8_t next = clampInt(value, 0, LED_BRIGHTNESS_MAX);
if (next == current.inactiveLevel) return;
current.inactiveLevel = next;
markDirty();
}
const char* groupName() { return groupNameBuf; }
const char* unitName() { return unitNameBuf; }
void setUnitName(const char* name) {
strncpy(unitNameBuf, name ? name : "", sizeof(unitNameBuf) - 1);
unitNameBuf[sizeof(unitNameBuf) - 1] = '\0';
markDirty();
}
void setGroupName(const char* name) {
strncpy(groupNameBuf, name ? name : "", sizeof(groupNameBuf) - 1);
groupNameBuf[sizeof(groupNameBuf) - 1] = '\0';
markDirty();
}
void adjustDbMin(int delta) { setDbMin(current.dbMin + delta); }
void adjustDbMax(int delta) { setDbMax(current.dbMax + delta); }
void adjustBrightness(int delta) { setBrightness(current.brightness + delta); }
const char* wifiSsid() { return wifiSsidBuf; }
const char* wifiPassword() { return wifiPassBuf; }
void setWifiCredentials(const char* ssid, const char* password) {
strncpy(wifiSsidBuf, ssid, sizeof(wifiSsidBuf) - 1);
wifiSsidBuf[sizeof(wifiSsidBuf) - 1] = 0;
strncpy(wifiPassBuf, password, sizeof(wifiPassBuf) - 1);
wifiPassBuf[sizeof(wifiPassBuf) - 1] = 0;
prefs.putString(kKeySsid, wifiSsidBuf);
prefs.putString(kKeyPass, wifiPassBuf);
}
void flush() {
if (dirty) writeNow();
}
void tick() {
if (!dirty) return;
// Unsigned arithmetic, so this stays correct across the millis() rollover.
if (millis() - dirtySinceMs < kFlushDelayMs) return;
writeNow();
}
} // namespace settings