From 280b2ff856afcfd8cd88a69e7511bfa9c88c0ad6 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 20 Mar 2026 07:02:03 +0800 Subject: [PATCH] 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> --- main/boards/common/lamp_controller.h | 4 ++++ main/led/single_led.cc | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/main/boards/common/lamp_controller.h b/main/boards/common/lamp_controller.h index 1ed142c..6015447 100644 --- a/main/boards/common/lamp_controller.h +++ b/main/boards/common/lamp_controller.h @@ -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, diff --git a/main/led/single_led.cc b/main/led/single_led.cc index 338af0a..d107619 100644 --- a/main/led/single_led.cc +++ b/main/led/single_led.cc @@ -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_); }