From 7cfc4ebb48c7f658b3f348bd8c08d45a4bef0d6e Mon Sep 17 00:00:00 2001 From: Andrew Yong Date: Fri, 17 Apr 2026 13:15:22 +0800 Subject: [PATCH] fix(Power): refactor EXT_CHRG_DETECT to compile-time macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirror the EXT_PWR_DETECT pattern: replace runtime static variables (ext_chrg_detect_mode, ext_chrg_detect_value) with compile-time macros. Auto-infer EXT_CHRG_DETECT_VALUE from EXT_CHRG_DETECT_MODE when the mode is INPUT_PULLUP (→ LOW) or INPUT_PULLDOWN (→ HIGH); default to HIGH. This fixes inverted polarity on variants that define EXT_CHRG_DETECT_MODE INPUT_PULLUP without an explicit EXT_CHRG_DETECT_VALUE (e.g. russell): previously the runtime default of HIGH caused isCharging() to return the opposite of the correct value. With auto-inference the correct LOW active level is now derived at compile time. Remove the now-redundant EXT_CHRG_DETECT_VALUE HIGH from ELECROW-ThinkNode-M4 variant.h since HIGH is the inferred default. Signed-off-by: Andrew Yong Assisted-by: Claude Sonnet 4.6 --- src/Power.cpp | 25 ++++++++++--------- .../nrf52840/ELECROW-ThinkNode-M4/variant.h | 1 - 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Power.cpp b/src/Power.cpp index 26b9615254e..7fb9b8b7d93 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -96,14 +96,15 @@ static const adc_atten_t atten = ADC_ATTENUATION; #ifdef EXT_CHRG_DETECT #ifndef EXT_CHRG_DETECT_MODE -static const uint8_t ext_chrg_detect_mode = INPUT; -#else -static const uint8_t ext_chrg_detect_mode = EXT_CHRG_DETECT_MODE; +#define EXT_CHRG_DETECT_MODE INPUT +// If using internal pull resistors, we can infer EXT_CHRG_DETECT_VALUE +#elif EXT_CHRG_DETECT_MODE == INPUT_PULLUP +#define EXT_CHRG_DETECT_VALUE LOW +#elif EXT_CHRG_DETECT_MODE == INPUT_PULLDOWN +#define EXT_CHRG_DETECT_VALUE HIGH #endif #ifndef EXT_CHRG_DETECT_VALUE -static const uint8_t ext_chrg_detect_value = HIGH; -#else -static const uint8_t ext_chrg_detect_value = EXT_CHRG_DETECT_VALUE; +#define EXT_CHRG_DETECT_VALUE HIGH #endif #endif @@ -511,9 +512,9 @@ class AnalogBatteryLevel : public HasBatteryLevel } #endif #if defined(ELECROW_ThinkNode_M6) - return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value || isVbusIn(); + return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE || isVbusIn(); #elif EXT_CHRG_DETECT - return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value; + return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE; #elif defined(BATTERY_CHARGING_INV) return !digitalRead(BATTERY_CHARGING_INV); #else @@ -653,7 +654,7 @@ bool Power::analogInit() #endif #endif #ifdef EXT_CHRG_DETECT - pinMode(EXT_CHRG_DETECT, ext_chrg_detect_mode); + pinMode(EXT_CHRG_DETECT, EXT_CHRG_DETECT_MODE); #endif #ifdef BATTERY_PIN @@ -1806,7 +1807,7 @@ class SerialBatteryLevel : public HasBatteryLevel { #if defined(EXT_CHRG_DETECT) - return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value; + return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE; #endif return false; @@ -1815,7 +1816,7 @@ class SerialBatteryLevel : public HasBatteryLevel virtual bool isCharging() override { #ifdef EXT_CHRG_DETECT - return digitalRead(EXT_CHRG_DETECT) == ext_chrg_detect_value; + return digitalRead(EXT_CHRG_DETECT) == EXT_CHRG_DETECT_VALUE; #endif // by default, we check the battery voltage only @@ -1840,7 +1841,7 @@ bool Power::serialBatteryInit() pinMode(EXT_PWR_DETECT, INPUT); #endif #ifdef EXT_CHRG_DETECT - pinMode(EXT_CHRG_DETECT, ext_chrg_detect_mode); + pinMode(EXT_CHRG_DETECT, EXT_CHRG_DETECT_MODE); #endif bool result = serialBatteryLevel.runOnce(); diff --git a/variants/nrf52840/ELECROW-ThinkNode-M4/variant.h b/variants/nrf52840/ELECROW-ThinkNode-M4/variant.h index 2cfe948e3c4..2164bcedc74 100644 --- a/variants/nrf52840/ELECROW-ThinkNode-M4/variant.h +++ b/variants/nrf52840/ELECROW-ThinkNode-M4/variant.h @@ -85,7 +85,6 @@ static const uint8_t A0 = PIN_A0; // charger status #define EXT_CHRG_DETECT (32 + 6) -#define EXT_CHRG_DETECT_VALUE HIGH // SPI #define SPI_INTERFACES_COUNT 1