Skip to content

Ethernet enabled breaks gpio button short/long/double click logic#1038

Open
penfold42 wants to merge 3 commits into
probonopd:mainfrom
penfold42:etherbuttonfix
Open

Ethernet enabled breaks gpio button short/long/double click logic#1038
penfold42 wants to merge 3 commits into
probonopd:mainfrom
penfold42:etherbuttonfix

Conversation

@penfold42

@penfold42 penfold42 commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #1012

Partially tested with my build on Pi2, Pi3 (and Pi1 with network enabled).

Will do more testing with generated build.

Summary by Sourcery

Throttle network updates and make button handling time-based on actual elapsed ticks to fix input timing issues when Ethernet is enabled.

Bug Fixes:

  • Fix GPIO button click, double-click, and long-press detection by basing timing on elapsed ticks instead of fixed update steps and handling threshold crossings robustly.
  • Prevent Ethernet-enabled network processing from starving other logic by rate-limiting network updates.

Enhancements:

  • Simplify network running-state detection by only applying WLAN-specific connectivity checks.
  • Minor formatting cleanup in kernel initialization code.

checking link status takes 12ms -> a killer in the main loop.
 only do it once a second
 I also think its ultimately doing it twice.

even with this, the main loop runs much slower.
 button logic is tweaked to use the # of 100uS that passed.
 it assumed it was called every 100uS
m_pNet->IsRunning(); also calls IsLinkUP()
@sourcery-ai

sourcery-ai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces timer-based throttling for network updates and makes button click/long/double-press detection interval-aware so UI timing remains correct when Ethernet/network activity is present; also cleans up network readiness logic and a minor USB initialization style issue.

Sequence diagram for throttled network updates in CMiniDexed::Process

sequenceDiagram
    participant CMiniDexed
    participant CTimer
    participant Net

    CMiniDexed->>CTimer: GetClockTicks()
    CTimer-->>CMiniDexed: currentTick
    CMiniDexed->>CMiniDexed: [currentTick - m_lastNetworkUpdate > NETWORK_UPDATE_NUM_TICKS]
    alt [currentTick - m_lastNetworkUpdate > NETWORK_UPDATE_NUM_TICKS]
        CMiniDexed->>CMiniDexed: m_lastNetworkUpdate = currentTick
        CMiniDexed->>Net: UpdateNetwork()
    else [interval not reached]
        CMiniDexed->>CMiniDexed: skip UpdateNetwork
    end
Loading

File-Level Changes

Change Details Files
Throttle CMiniDexed network updates to run periodically instead of every Process() loop iteration.
  • Add m_lastNetworkUpdate member to track last time network was updated
  • Define NETWORK_UPDATE_NUM_TICKS and only call UpdateNetwork() when enough clock ticks have elapsed
  • Remove unconditional UpdateNetwork() call and keep scheduler yielding after potential network update
src/minidexed.cpp
src/minidexed.h
Adjust network-running detection to ignore Ethernet link state and only gate WLAN by WPA supplicant connection.
  • Simplify bNetIsRunning calculation to only AND WLAN case with WPA supplicant connectivity
  • Stop masking Ethernet network state with link-up check
src/minidexed.cpp
Make button trigger detection use a variable interval rather than assuming fixed-tick increments, improving reliability of click/double-click/long-press logic under varying load.
  • Change CUIButton::ReadTrigger and Read to accept an interval parameter
  • Increment button timer by interval instead of 1 each update
  • Switch equality checks for double-click and long-press thresholds to >= comparisons
  • Have CUIButtons::Update compute elapsed ticks since last update and pass interval to each button
  • Store last interval in CUIButtons and add member field for it
src/uibuttons.cpp
src/uibuttons.h
Minor formatting/consistency fixes in kernel USB initialization block.
  • Normalize indentation and brace style around m_pUSB->Initialize() check
src/kernel.cpp

Assessment against linked issues

Issue Objective Addressed Explanation
#1012 Fix the bug where enabling Ethernet/networking causes the KY-040 rotary encoder (GPIO button) short/long/double press logic to misbehave (e.g., short press no longer loads performances, long press no longer goes 'home').
#1012 Ensure networking can remain enabled (Ethernet/WLAN) without regressing core functionality, by integrating network updates in a way that does not interfere with GPIO button timing.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The new tick-based throttling in Process and the interval logic in CUIButtons::Update both rely on unsigned GetClockTicks() subtraction without any wraparound handling; consider explicitly handling timer overflow so long-running devices don’t produce negative/huge effective intervals.
  • In CUIButtons::Update, m_interval can become 0 when currentTick - m_lastTick < BUTTONS_UPDATE_NUM_TICKS, which prevents m_timer from progressing for short periods; you may want to enforce a minimum interval of 1 or avoid the division so button timing remains robust at higher update rates.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new tick-based throttling in `Process` and the interval logic in `CUIButtons::Update` both rely on unsigned `GetClockTicks()` subtraction without any wraparound handling; consider explicitly handling timer overflow so long-running devices don’t produce negative/huge effective intervals.
- In `CUIButtons::Update`, `m_interval` can become 0 when `currentTick - m_lastTick < BUTTONS_UPDATE_NUM_TICKS`, which prevents `m_timer` from progressing for short periods; you may want to enforce a minimum interval of 1 or avoid the division so button timing remains robust at higher update rates.

## Individual Comments

### Comment 1
<location path="src/minidexed.cpp" line_range="2275-2276" />
<code_context>
-		bNetIsRunning &= m_pNetDevice->IsLinkUp();
-	else if (m_pNetDevice->GetType() == NetDeviceTypeWLAN)
+
+	if (m_pNetDevice->GetType() == NetDeviceTypeWLAN)
 		bNetIsRunning &= (m_WPASupplicant && m_WPASupplicant->IsConnected());

 	if (!m_bNetworkInit && bNetIsRunning)
</code_context>
<issue_to_address>
**question:** Re-check whether Ethernet link state should still gate bNetIsRunning.

This change removes the Ethernet `IsLinkUp()` check from `bNetIsRunning`, so an unplugged Ethernet cable may now be seen as a running network, unlike WLAN which still requires a supplicant connection. If this behavior is intentional, consider whether any minimal link-state check is still needed (e.g., platform-specific) to avoid cases where the stack is up but the physical link is down.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/minidexed.cpp
@github-actions

Copy link
Copy Markdown

Build for testing:
MiniDexed_1336_2026-06-21-f48dcd5_64bit
MiniDexed_1336_2026-06-21-f48dcd5_32bit
Use at your own risk.

As the code stands its unnecessary and I suspect the compiler optimises
it away.
It can't hurt and might save someone in the future.
@github-actions

Copy link
Copy Markdown

Build for testing:
MiniDexed_1337_2026-06-21-db9beb1_64bit
MiniDexed_1337_2026-06-21-db9beb1_32bit
Use at your own risk.

@penfold42

Copy link
Copy Markdown
Contributor Author

My testing with the generated builds with Pi2 and Pi3 is complete.

@probonopd

Copy link
Copy Markdown
Owner

Thank you very much @penfold42. Do you have a chance to test on more models?

@penfold42

Copy link
Copy Markdown
Contributor Author

Thank you very much @penfold42. Do you have a chance to test on more models?

I also tested on an old Pi1 (with my build to enable ethernet). I dont have a pi4 or pi5.

@probonopd

probonopd commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Would be great if someone could test 4/5. Anyone?

@penfold42

Copy link
Copy Markdown
Contributor Author

Would be great if someone could test 3/4/5. Anyone?

I've tested the Pi3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enabling network and rotary encoder goes crazy

2 participants