Add saved LED controls across supported boards

Add get leds and set leds on|off for Companion, repeater, room server and
sensor builds. Preserve the setting across reboots and cover GPIO, RGB,
Bluetooth, notification and expander indicators without changing radio
amplifier controls or screen backlights.

RAK Companions blink while advertising and keep the Bluetooth LED off once
connected, including late timer callbacks.

Adapted from meshcore-dev/MeshCore#3605 and #2694.
Validated with host regressions and firmware builds for SenseCAP Solar,
RAK4631, T096, Heltec V4, T-Beam, Xiao S3, Pico W and Wio E5 Mini.

Co-authored-by: R0ck <develop@hectospark.co.uk>
Co-authored-by: Nick Dunklee <nick@dunklee.org>
This commit is contained in:
committed 2026-10-20 08:59:39 -07:00
1 parent c48e679284
commit 9d52b1fd68
91 files changed
+1485 -65

No files matched your search

+3
View File
@@ -7,6 +7,7 @@
#include "helpers/BatteryChargeSupport.h"
#include "helpers/RegulatorMode.h"
#include "helpers/PowerSavingDiagnostics.h"
#include "helpers/LedPolicy.h"
#if defined(ARDUINO) && (defined(RP2040_PLATFORM) || defined(STM32_PLATFORM))
#include <Arduino.h>
#endif
@@ -145,6 +146,8 @@ public:
virtual const char* getManufacturerName() const = 0;
virtual void onBeforeTransmit() { }
virtual void onAfterTransmit() { }
// Update only indicator outputs; transmit hooks may also control RF hardware.
virtual void applyLedPolicy() { }
virtual void reboot() = 0;
MESH_REGULATOR_VIRTUAL uint8_t mcuRegulatorModes() const { return 0; }
MESH_REGULATOR_VIRTUAL power::RegulatorMode defaultMcuRegulatorMode() const {
+2
View File
@@ -1,4 +1,5 @@
#include <helpers/RegulatorSettings.h>
#include <helpers/LedSettings.h>
#include <Arduino.h>
#include "CarrierTools.h"
#include "PacketFileLoggingConfig.h"
@@ -2980,6 +2981,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
if (mesh::power::handleBatteryChargeCommand(*_board, command, reply, 160)) return;
#endif
if (mesh::power::handleRegulatorCommand(command, reply, 157)) return;
if (mesh::leds::handleCommand(command, reply, 157)) return;
if (mesh::handleUsbLoggingWatchdogCommand(command, reply, 160)) return;
if (mesh::powerdiag::handleCommand(command, reply, 157, millis())) return;
if (handleManagementCommand(command, reply)) return;
+13 -2
View File
@@ -271,9 +271,18 @@ public:
uint8_t getStartupReason() const override { return startup_reason; }
bool isUserGpioAvailable(uint8_t pin) const override;
void applyLedPolicy() override {
if (mesh::leds::enabled()) return;
#if defined(P_LORA_TX_LED)
digitalWrite(P_LORA_TX_LED, LOW);
#elif defined(P_LORA_TX_NEOPIXEL_LED)
neopixelWrite(P_LORA_TX_NEOPIXEL_LED, 0, 0, 0);
#endif
}
#if defined(P_LORA_TX_LED)
void onBeforeTransmit() override {
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on
if (mesh::leds::enabled()) digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on
}
void onAfterTransmit() override {
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off
@@ -282,7 +291,9 @@ public:
#define NEOPIXEL_BRIGHTNESS 64 // white brightness (max 255)
void onBeforeTransmit() override {
neopixelWrite(P_LORA_TX_NEOPIXEL_LED, NEOPIXEL_BRIGHTNESS, NEOPIXEL_BRIGHTNESS, NEOPIXEL_BRIGHTNESS); // turn TX neopixel on (White)
if (mesh::leds::enabled()) {
neopixelWrite(P_LORA_TX_NEOPIXEL_LED, NEOPIXEL_BRIGHTNESS, NEOPIXEL_BRIGHTNESS, NEOPIXEL_BRIGHTNESS); // turn TX neopixel on (White)
}
}
void onAfterTransmit() override {
neopixelWrite(P_LORA_TX_NEOPIXEL_LED, 0, 0, 0); // turn TX neopixel off
+24
View File
@@ -0,0 +1,24 @@
#include "LedPolicy.h"
#include <Arduino.h>
#include <target.h>
namespace mesh { namespace leds {
void applyStatusPolicy() {
if (enabled()) return;
#if defined(PIN_STATUS_LED_R) && defined(PIN_STATUS_LED_G) && defined(PIN_STATUS_LED_B)
analogWrite(PIN_STATUS_LED_R, 0);
analogWrite(PIN_STATUS_LED_G, 0);
analogWrite(PIN_STATUS_LED_B, 0);
#elif defined(PIN_STATUS_LED)
if (PIN_STATUS_LED < 0) return;
#ifdef LED_STATE_ON
digitalWrite(PIN_STATUS_LED, !LED_STATE_ON);
#else
digitalWrite(PIN_STATUS_LED, LOW);
#endif
pinMode(PIN_STATUS_LED, OUTPUT);
#endif
}
}} // namespace mesh::leds
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <atomic>
namespace mesh { namespace leds {
inline std::atomic<bool>& policy() {
static std::atomic<bool> value{true};
return value;
}
inline bool enabled() { return policy().load(std::memory_order_relaxed); }
inline void setEnabled(bool value) { policy().store(value, std::memory_order_relaxed); }
void applyStatusPolicy();
}} // namespace mesh::leds
+107
View File
@@ -0,0 +1,107 @@
#include "LedSettings.h"
#include <MeshCore.h>
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#include <helpers/AtomicFileWriter.h>
#else
#include <helpers/ContactFileTransaction.h>
#endif
#include <stdio.h>
#include <string.h>
namespace mesh { namespace leds {
namespace {
MainBoard* main_board = nullptr;
FILESYSTEM* settings_fs = nullptr;
const char* const path = "/leds_v1";
const char* const backup = "/leds_v1.bak";
void encode(bool on, uint8_t (&data)[8]) {
const uint8_t value = on ? 1 : 0;
const uint8_t image[] = {'L', 'E', 'D', 1, value,
static_cast<uint8_t>(value ^ 0xff), 0, 0};
memcpy(data, image, sizeof(data));
}
bool readSetting(const char* name, bool& on) {
if (!settings_fs) return false;
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
File file(*settings_fs);
if (!file.open(name, FILE_O_READ)) return false;
#else
File file = settings_fs->open(name, "r");
if (!file) return false;
#endif
uint8_t data[8], expected[8];
const bool complete = file.size() == sizeof(data)
&& file.read(data, sizeof(data)) == sizeof(data);
file.close();
if (!complete || data[4] > 1) return false;
encode(data[4] != 0, expected);
if (memcmp(data, expected, sizeof(data)) != 0) return false;
on = data[4] != 0;
return true;
}
bool saveSetting(bool on) {
if (!settings_fs) return false;
uint8_t data[8];
encode(on, data);
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
AtomicFileWriter file(settings_fs, path);
#else
ContactFileTransaction file(settings_fs, path);
#endif
if (!file || file.write(data, sizeof(data)) != sizeof(data) || !file.commit())
return false;
bool checked = !on;
return readSetting(path, checked) && checked == on;
}
} // namespace
void beginSettings(MainBoard& board, FILESYSTEM* fs) {
main_board = &board;
settings_fs = fs;
bool on = true;
if (!readSetting(path, on) && readSetting(backup, on)) {
// Recover the rename gap without discarding the last complete preference.
if ((!fs->exists(path) || fs->remove(path)) && fs->rename(backup, path)) {
bool checked = !on;
if (!readSetting(path, checked) || checked != on) settings_fs = nullptr;
} else {
settings_fs = nullptr;
}
}
setEnabled(on);
board.applyLedPolicy();
applyStatusPolicy();
}
bool handleCommand(const char* command, char* reply, size_t capacity) {
if (!command || !reply || capacity == 0) return false;
const bool get = strncmp(command, "get ", 4) == 0;
const bool set = strncmp(command, "set ", 4) == 0;
if ((!get && !set) || strncmp(command + 4, "leds", 4) != 0) return false;
const char* value = command + 8;
if (*value && *value != ' ' && *value != '\t') return false;
if (get) {
if (*value) snprintf(reply, capacity, "Error: usage get leds");
else snprintf(reply, capacity, "> %s", enabled() ? "on" : "off");
return true;
}
while (*value == ' ' || *value == '\t') ++value;
if (strcmp(value, "on") != 0 && strcmp(value, "off") != 0) {
snprintf(reply, capacity, "Error: usage set leds on|off");
return true;
}
const bool on = strcmp(value, "on") == 0;
if (!main_board || !saveSetting(on)) {
snprintf(reply, capacity, "Error: LED setting save unconfirmed; reboot may differ");
return true;
}
setEnabled(on);
main_board->applyLedPolicy();
applyStatusPolicy();
snprintf(reply, capacity, "OK");
return true;
}
} } // namespace mesh::leds
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <helpers/IdentityStore.h>
#include <stddef.h>
namespace mesh {
class MainBoard;
namespace leds {
void beginSettings(MainBoard& board, FILESYSTEM* fs);
bool handleCommand(const char* command, char* reply, size_t capacity);
} // namespace leds
} // namespace mesh
+7
View File
@@ -6,6 +6,7 @@
#include "nrf52/SoftDeviceState.h"
#include "nrf52/SleepCycleState.h"
#include "nrf52/ShutdownLeds.h"
#include "nrf52/BluetoothLed.h"
#include <target.h>
#include "UserGpioPinPolicy.h"
#include "PowerSavingDiagnostics.h"
@@ -263,6 +264,10 @@ bool NRF52Board::applyMcuRegulator(mesh::power::RegulatorMode mode) {
return true;
}
void NRF52Board::applyLedPolicy() {
mesh_nrf52::applyBluetoothLedPolicy();
}
void NRF52Board::loop() {
#ifdef NRF52_POWER_MANAGEMENT
if (!mesh::power::voltagePolicySupported()) return;
@@ -959,10 +964,12 @@ bool NRF52Board::startOTAUpdate(const char *id, char reply[], bool force_ap) {
// Even a failed begin can leave SoftDevice enabled partway through SDK
// startup. Require a fresh authoritative query before the next sleep.
mesh_nrf52::sleepCycleState().invalidate();
mesh_nrf52::applyBluetoothLedPolicy();
if (!Bluefruit.begin(1, 0)) {
return false;
}
ota_ble_started = true;
mesh_nrf52::bluetoothLedStarted();
// To be consistent OTA DFU should be added first if it exists
bledfu.begin();
+1
View File
@@ -61,6 +61,7 @@ public:
{}
virtual void begin();
void loop() override;
void applyLedPolicy() override;
// Feed the hardware main-loop watchdog. The first call starts it, so normal
// setup (including filesystem mount and radio initialization) is not timed.
// A loop blocked forever in SoftDevice-backed flash I/O will stop feeding it
+15 -1
View File
@@ -139,9 +139,23 @@ public:
return charge_target_restore_failed;
}
void applyLedPolicy() override {
#ifndef TBEAM_SUPREME_SX1262
if (!mesh::leds::enabled()) digitalWrite(P_LORA_TX_LED, HIGH);
#else
ESP32Board::applyLedPolicy();
#endif
#if !defined(PORTABLE_MQTT_OBSERVER) || defined(TBEAM_SUPREME_SX1262)
if (charge_pmu_ready && PMU) {
PMU->setChargingLedMode(mesh::leds::enabled()
? XPOWERS_CHG_LED_CTRL_CHG : XPOWERS_CHG_LED_OFF);
}
#endif
}
#ifndef TBEAM_SUPREME_SX1262
void onBeforeTransmit() override{
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on - invert pin for SX1276
if (mesh::leds::enabled()) digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on - invert pin for SX1276
}
void onAfterTransmit() override{
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off - invert pin for SX1276
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include "../LedPolicy.h"
#include "ShutdownLeds.h"
#include <bluefruit.h>
namespace mesh_nrf52 {
inline bool& bluetoothLedReady() {
static bool ready = false;
return ready;
}
inline void applyBluetoothLedPolicy() {
#ifdef LED_BLUE
if (LED_BLUE < 0 || static_cast<uint32_t>(LED_BLUE) >= PINS_COUNT
|| digitalPinToPinName(LED_BLUE) == 0xff) {
Bluefruit.autoConnLed(false);
return;
}
#else
Bluefruit.autoConnLed(false);
return;
#endif
#ifdef DISABLE_BLE_LED
const bool enable = false;
#else
const bool enable = mesh::leds::enabled();
#endif
Bluefruit.autoConnLed(enable);
if (!enable) {
#ifdef LED_BLUE
turnOffShutdownLed(LED_BLUE);
#endif
} else if (bluetoothLedReady()) {
#if defined(RAK_BOARD) && defined(BLE_PIN_CODE)
Bluefruit._setConnLed(false);
#else
Bluefruit._setConnLed(Bluefruit.connected() != 0);
#endif
if (Bluefruit.Advertising.isRunning()) Bluefruit._startConnLed();
}
}
inline void bluetoothLedStarted() {
bluetoothLedReady() = true;
applyBluetoothLedPolicy();
}
} // namespace mesh_nrf52
+3 -5
View File
@@ -4,6 +4,7 @@
#include "SoftDeviceState.h"
#include "SleepCycleState.h"
#include "BleDebugTrace.h"
#include "BluetoothLed.h"
#include <stdio.h>
#include <string.h>
#include "ble_gap.h"
@@ -836,11 +837,7 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
char charpin[20];
snprintf(charpin, sizeof(charpin), "%lu", (unsigned long)pin_code);
#ifdef DISABLE_BLE_LED
// Opt out of Bluefruit's automatic LED control without changing the normal
// board/library default or the application's own LED handling.
Bluefruit.autoConnLed(false);
#endif
mesh_nrf52::applyBluetoothLedPolicy();
// Configure individual connection values rather than the all-or-nothing
// Bluefruit preset. Defaults are identical to BANDWIDTH_MAX; constrained
// board profiles can lower event/buffer reservations without shrinking the
@@ -875,6 +872,7 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
return false;
}
mesh_nrf52::bluetoothLedStarted();
if (clear_bonds) Bluefruit.Periph.clearBonds();
if (custom_address != nullptr) {
+5 -1
View File
@@ -36,8 +36,12 @@ public:
}
#if defined(P_LORA_TX_LED)
void applyLedPolicy() override {
if (!mesh::leds::enabled()) digitalWrite(P_LORA_TX_LED, HIGH);
}
void onBeforeTransmit() override {
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on
if (mesh::leds::enabled()) digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on
}
void onAfterTransmit() override {
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off