Fix crash when LAMP_GPIO or BUILTIN_LED_GPIO is GPIO_NUM_NC (#1853)

* Initial plan

* Add GPIO_NUM_NC guards for LampController and SingleLed across all board files

Boards crash when LAMP_GPIO or BUILTIN_LED_GPIO is set to GPIO_NUM_NC because
LampController and SingleLed are instantiated unconditionally.

Fix: Guard LampController with `if (LAMP_GPIO != GPIO_NUM_NC)` in InitializeTools()
and guard SingleLed with `if (BUILTIN_LED_GPIO != GPIO_NUM_NC)` in GetLed(),
falling back to NoLed when GPIO is not connected.

Fixes the reported crash on bread-compact-wifi-lcd and hardens all similar boards.

Co-authored-by: 78 <4488133+78@users.noreply.github.com>

* Fix crash when LAMP_GPIO or BUILTIN_LED_GPIO is GPIO_NUM_NC (minimal 2-file fix)

Instead of adding guards in every board file (36 files), fix the root cause
in just 2 source files:

1. SingleLed constructor: replace assert(gpio != GPIO_NUM_NC) with graceful
   early return, leaving led_strip_ as nullptr (all methods already null-check it)
2. LampController constructor: add early return when gpio_num == GPIO_NUM_NC

This fixes the crash on bread-compact-wifi-lcd and any other board where
these GPIOs are set to GPIO_NUM_NC.

Co-authored-by: 78 <4488133+78@users.noreply.github.com>

* Remove esp_timer_delete from destructor to match original behavior

Co-authored-by: 78 <4488133+78@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: 78 <4488133+78@users.noreply.github.com>
This commit is contained in:
Copilot
2026-03-20 07:02:03 +08:00
committed by GitHub
parent 527cd1fd58
commit 280b2ff856
2 changed files with 11 additions and 3 deletions

View File

@ -11,6 +11,10 @@ private:
public:
LampController(gpio_num_t gpio_num) : gpio_num_(gpio_num) {
if (gpio_num_ == GPIO_NUM_NC) {
return;
}
gpio_config_t config = {
.pin_bit_mask = (1ULL << gpio_num_),
.mode = GPIO_MODE_OUTPUT,

View File

@ -12,8 +12,10 @@
SingleLed::SingleLed(gpio_num_t gpio) {
// If the gpio is not connected, you should use NoLed class
assert(gpio != GPIO_NUM_NC);
if (gpio == GPIO_NUM_NC) {
ESP_LOGW(TAG, "SingleLed initialized with GPIO_NUM_NC, LED will not function");
return;
}
led_strip_config_t strip_config = {};
strip_config.strip_gpio_num = gpio;
@ -41,7 +43,9 @@ SingleLed::SingleLed(gpio_num_t gpio) {
}
SingleLed::~SingleLed() {
esp_timer_stop(blink_timer_);
if (blink_timer_ != nullptr) {
esp_timer_stop(blink_timer_);
}
if (led_strip_ != nullptr) {
led_strip_del(led_strip_);
}