leds: persist brightness across reboots and firmware updates

leds.brightness (PWM-capable boards) was RAM-only since it was added: it
always reset to ZEPHCORE_LED_DEFAULT_BRIGHTNESS_PCT (10%) on every boot,
regardless of what a user had last set it to.

New field led_brightness on NodePrefs, following the exact same
append-only pattern as leds_radio_mode/leds_hb_mode added just before it:
offset 311 in RepeaterDataStore.cpp (repeater/room-server/observer,
shared file), offset 174 in ZephyrDataStore.cpp (companion). Both are
backward compatible by construction: a prefs file written before this
field existed simply leaves it at the initNodePrefs() default (10%),
which is exactly the behaviour every already-deployed node has today --
no special-casing needed to keep "10% on a new/not-yet-upgraded node" as
the effective default.

CommonCLI.cpp's "set leds.brightness" handler now writes the prefs field
and calls savePrefs(), same as the leds.radio/leds.hb handlers right
above it; "get leds.brightness" now reads from prefs instead of the RAM
getter directly, for the same reason those two already do (always in
sync, prefs is the source of truth). The four boot call sites
(main_companion.cpp, main_repeater.cpp, main_room_server.cpp,
main_observer.cpp) each gained a zephcore_led_set_brightness_pct() call
seeding the RAM cache from the persisted value, mirroring the existing
leds_radio_mode/leds_hb_mode lines right next to them.

Verified building for heltec_t096/nrf52840 (companion) and rak3401_1watt
(repeater), the two roles/datastores touched by this change.
This commit is contained in:
ZephCore T096
2026-10-08 18:27:00 +02:00
parent 4905191b1c
commit ec2a07dfa9
6 changed files with 29 additions and 4 deletions
+13 -4
View File
@@ -995,8 +995,11 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, const char* command, cha
} else if (memcmp(config, "leds.brightness", 15) == 0) {
/* Separate from "leds" on purpose: keeps the on/off switch a
* pure word command and the dimmer a pure number command,
* instead of one command parsing both — see led_gate.h. */
snprintf(reply, CLI_REPLY_SIZE, "> %u%%", (unsigned)zephcore_led_brightness_pct());
* instead of one command parsing both — see led_gate.h. Read
* from _prefs, same as leds.radio/leds.hb above, not from the
* RAM getter directly -- the two are always in sync, the "set"
* handler below writes both together. */
snprintf(reply, CLI_REPLY_SIZE, "> %u%%", (unsigned)_prefs->led_brightness);
} else if (memcmp(config, "leds", 4) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "> %s", _prefs->leds_disabled ? "off" : "on");
#ifndef ZEPHCORE_REPEATER
@@ -1271,8 +1274,12 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, const char* command, cha
} else if (memcmp(config, "leds.brightness ", 16) == 0) {
/* Separate from "leds" on purpose: keeps the on/off switch a pure
* word command and the dimmer a pure number command, instead of
* one command parsing both. RAM-only, see led_gate.h — no
* savePrefs() call here. Checked before "leds " below so this
* one command parsing both. Persisted (since 1.17.4): a fresh
* node still starts at ZEPHCORE_LED_DEFAULT_BRIGHTNESS_PCT (the
* append-only prefs format leaves this byte at the
* initNodePrefs() default on any file that predates this field),
* but once set it survives reboots and firmware updates like
* leds.radio/leds.hb above. Checked before "leds " below so this
* longer, more specific prefix is never shadowed by it. */
const char *val = &config[16];
char *endptr = (char *)val;
@@ -1282,7 +1289,9 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, const char* command, cha
pct < 0 || pct > 100) {
strcpy(reply, "Error: must be 0-100");
} else {
_prefs->led_brightness = (uint8_t)pct;
zephcore_led_set_brightness_pct((uint8_t)pct);
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - leds.brightness=%ld%%", pct);
}
} else if (memcmp(config, "leds ", 5) == 0) {