Skip to content

fix: dynamically assign button pin to prevent false interrupts#10742

Open
LN4CY wants to merge 13 commits into
meshtastic:developfrom
LN4CY:fix-dynamic-button-pin
Open

fix: dynamically assign button pin to prevent false interrupts#10742
LN4CY wants to merge 13 commits into
meshtastic:developfrom
LN4CY:fix-dynamic-button-pin

Conversation

@LN4CY

@LN4CY LN4CY commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

This PR resolves the false interrupt and random wake issues caused by floating button pins on variants where the button is not physically wired (such as rak3401_1watt), while fully restoring button functionality when a GPIO is explicitly assigned via the Meshtastic configuration.

Changes Made:

  • Removed hardcoded #ifdef BUTTON_PIN guards in InputBroker.cpp, PowerFSM.cpp, sleep.cpp, and main-esp32.cpp in favor of #if HAS_BUTTON.
  • Refactored logic to dynamically resolve _btnPin using the following priority:
    1. config.device.button_gpio (if explicitly assigned at runtime)
    2. USERPREFS_BUTTON_PIN (if defined in userprefs)
    3. BUTTON_PIN (if defined in the variant header)
  • If no valid pin is resolved (evaluates to 0xFF), the firmware skips configuring the button pull-ups, skips initializing ButtonThread, and ignores digital reads during sleep, avoiding any false positives on floating pins.

?? Attestations

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other: RAK3401 1-Watt
    • Other: RAK WisMesh Tap

Fixes #10574

Summary by CodeRabbit

  • Bug Fixes
    • Improved button detection after waking from both light and deep sleep.
    • Button wake/pull-up configuration now correctly uses the effective configured button GPIO at runtime.
    • Avoids enabling wake or treating a press when no valid button pin is configured.
    • Fixed pull-up and wake GPIO handling to better match ESP32/ESP32S3 requirements.
    • Improved compatibility across button configuration variants.

@github-actions github-actions Bot added the bugfix Pull request that fixes bugs label Jun 18, 2026
@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

⚡ Try this PR in the Web Flasher

Flash this PR in the Web Flasher

firmware commit boards expires

Warning

This is an automated, unreviewed CI test build. Back up your device configuration
before flashing, and only flash devices you are able to recover.

Supported boards built by this PR (30)
Device Board Platform
Crowpanel Adv 3.5 TFT elecrow-adv-35-tft esp32-s3
Heltec HT62 heltec-ht62-esp32c3-sx1262 esp32-c3
Heltec Mesh Node 096 heltec-mesh-node-t096 nrf52840
Heltec Mesh Node T1 heltec-mesh-node-t1 nrf52840
Heltec Mesh Node T114 heltec-mesh-node-t114 nrf52840
Heltec V3 heltec-v3 esp32-s3
Heltec V4 heltec-v4 esp32-s3
Meshnology W10 meshnology_w10 esp32-s3
Raspberry Pi Pico pico rp2040
Raspberry Pi Pico W picow rp2040
RAK WisMesh Pocket V3 rak_wismesh_pocket nrf52840
RAK WisMesh Pod rak_wismesh_pod nrf52840
RAK WisMesh Repeater Mini V2 rak_wismesh_repeater_mini nrf52840
RAK WisMesh Tag rak_wismeshtag nrf52840
RAK WisBlock 11200 rak11200 esp32
RAK WisBlock 11310 rak11310 rp2040
RAK3312 rak3312 esp32-s3
RAK WisBlock 4631 rak4631 nrf52840
Seeed SenseCAP Mesh-Tracker-X1 seeed_mesh_tracker_X1 nrf52840
Seeed Wio Tracker L1 seeed_wio_tracker_L1 nrf52840
Seeed Xiao NRF52840 Kit seeed_xiao_nrf52840_kit nrf52840
Seeed Xiao ESP32-S3 seeed-xiao-s3 esp32-s3
Station G2 station-g2 esp32-s3
Station G3 station-g3 esp32-s3
LILYGO T-Deck t-deck-tft esp32-s3
LILYGO T-Echo t-echo nrf52840
LILYGO T-Echo Plus t-echo-plus nrf52840
LILYGO T-Impulse Plus t-impulse-plus nrf52840
LilyGo T3-C6 tlora-c6 esp32-c6
Seeed SenseCAP T1000-E tracker-t1000-e nrf52840

Build artifacts expire on 2026-08-18. Updated for f80c04d.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Button GPIO selection now uses a shared resolver with compile-time and device-configuration sources, validates ESP32 pins, and applies the result across input initialization, sleep-wake configuration, and idle wake-event detection.

Changes

Button GPIO handling

Layer / File(s) Summary
Resolve and initialize the button GPIO
src/input/ButtonHelper.h, src/input/InputBroker.cpp
A shared helper resolves and validates the effective GPIO; input setup configures valid pins and creates UserButtonThread only when a pin is available.
Apply the selected GPIO to sleep-wake paths
src/sleep.cpp, src/platform/esp32/main-esp32.cpp
Deep and light sleep configure holds, pull-ups, GPIO wake, and external wake masks using the resolved GPIO.
Map wake input to FSM events
src/PowerFSM.cpp
Idle wake handling reads the resolved GPIO and emits EVENT_PRESS or EVENT_WAKE_TIMER when no valid pin is available.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant InputBroker
  participant SleepManager
  participant ButtonHelper
  participant ESP32Sleep
  participant PowerFSM
  InputBroker->>ButtonHelper: resolve and validate button GPIO
  InputBroker->>InputBroker: initialize valid button input
  SleepManager->>ButtonHelper: resolve button GPIO
  SleepManager->>ESP32Sleep: configure sleep wake sources
  ESP32Sleep-->>PowerFSM: return from wake
  PowerFSM->>ButtonHelper: resolve button GPIO
  PowerFSM->>PowerFSM: emit EVENT_PRESS or EVENT_WAKE_TIMER
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: dynamic button pin resolution to prevent false interrupts.
Description check ✅ Passed The description is mostly complete, with a clear summary, issue reference, and attestations using the repository template.
Linked Issues check ✅ Passed The changes address #10574 by restoring user-button support via runtime GPIO resolution, including explicit device-setting assignment.
Out of Scope Changes check ✅ Passed The PR stays focused on button-pin resolution and related sleep/init handling, with no obvious unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/input/InputBroker.cpp`:
- Around line 170-178: Duplicate button GPIO resolution should be centralized in
an inline helper such as getResolvedButtonPin(), preserving the
USERPREFS_BUTTON_PIN, BUTTON_PIN, and config.device.button_gpio precedence. Add
the helper in the shared configuration or utility location, then replace the
duplicated blocks in src/input/InputBroker.cpp lines 170-178 and 329-337,
src/sleep.cpp lines 313-321, 440-448, and 485-493,
src/platform/esp32/main-esp32.cpp lines 355-363, and src/PowerFSM.cpp lines
180-188 with calls to it.

In `@src/platform/esp32/main-esp32.cpp`:
- Around line 367-369: Validate the resolved button GPIO before use, preferably
in the shared pin-resolution helper, using the platform’s GPIO validity check or
an equivalent bounds check. Apply this to the wake-mask shift in
main-esp32.cpp#L367-L369, the digitalRead path in PowerFSM.cpp#L189, and
OneButton instantiation in InputBroker.cpp#L348-L349; invalid pins must be
excluded so they cannot cause undefined shifts or phantom button events.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 32fa657f-7831-4f92-ba75-b84721911335

📥 Commits

Reviewing files that changed from the base of the PR and between de0380c and 75a378d.

📒 Files selected for processing (4)
  • src/PowerFSM.cpp
  • src/input/InputBroker.cpp
  • src/platform/esp32/main-esp32.cpp
  • src/sleep.cpp

Comment thread src/input/InputBroker.cpp Outdated
Comment thread src/platform/esp32/main-esp32.cpp
LN4CY added 2 commits July 18, 2026 13:46
This addresses the CodeRabbit PR comments by moving the duplicate GPIO resolution and validation logic into ButtonHelper.h and updating the call sites.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/input/ButtonHelper.h`:
- Around line 17-19: Update the button GPIO assignment logic around
config.device.button_gpio so GPIO 0 can be distinguished from an unset value,
using the protobuf presence flag such as has_button_gpio when available.
Preserve the hardware-defined BUTTON_PIN fallback when no dynamic GPIO is
configured, while allowing an explicitly configured GPIO 0 to be assigned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 1ac485af-d1ad-42cc-b566-800a557c4d71

📥 Commits

Reviewing files that changed from the base of the PR and between 75a378d and 900c4e5.

📒 Files selected for processing (5)
  • src/PowerFSM.cpp
  • src/input/ButtonHelper.h
  • src/input/InputBroker.cpp
  • src/platform/esp32/main-esp32.cpp
  • src/sleep.cpp
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/sleep.cpp
  • src/input/InputBroker.cpp
  • src/PowerFSM.cpp
  • src/platform/esp32/main-esp32.cpp

Comment thread src/input/ButtonHelper.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Add user button for Rak 1 Watt radio

2 participants