Button Debounce doesn't work correctly
See original GitHub issuehttps://github.com/dotnet/iot/pull/1720
There’s an issue with this implementation, when the button is held down for longer then the debounce timeout. When it is released now, there will be a “pressed” event due to the bounces happening during release, and the desired “released” event is fired, due to the debouncing getting started by “pressed”.
I solved this by using a timer for debounce that checks the current button state when finished. Something like below, same for HandleButtonReleased()
protected void HandleButtonPressed()
{
_stateForAfterDebounce = true; // keep track of the button state, even with bounces, so we can get the current state at the end of the debounce timeout
if (IsPressed) // already pressed
return;
if (_debounceTimer!=null) // currently debouncing
return;
IsPressed = true;
if (_debounceTime.TotalMilliseconds > 0)
{
_debounceTimer = new Timer((o) =>
{
_debounceTimer?.Dispose();
_debounceTimer = null;
if (_stateForAfterDebounce == false) // Button might have been been released while we were in the debounce. Check current state and handle
HandleButtonReleased();
}, null, _debounceTime, Timeout.InfiniteTimeSpan);
}
ButtonDown?.Invoke(this, new EventArgs());
if (IsHoldingEnabled)
{
_holdingTimer = new Timer(StartHoldingHandler, null, (int)_holdingMs, Timeout.Infinite);
}
}
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:11 (8 by maintainers)
Top Results From Across the Web
DEBOUNCE JS NOT WORKING with event onclick was ...
1 Answer 1 ... debounce is designed to create a debounced function. Every time you click you create a debounced function which you...
Read more >Button debouncing not working [closed]
It is perfectly possible that when you close the switch, at the time the ISR handler gets control, the switch contact is bouncing....
Read more >The simplest button debounce solution
Button debounce does not to be this complicate, in this article I will show you probably the simplest button debounce solution.
Read more >Debounce button and delays - Programming Questions
I try to find out why my debounced button gets hardly recognized after pushing. ... No I do not use delay within the...
Read more >How to debounce and throttle in React without losing your ...
Deep dive into debounce and throttle in React. What is debounce and throttle, how to use them in React properly, how to avoid...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@W1zzardTPU do you feel like sending a PR with a fix? I’m sure there will be more people hitting this. Perhaps we should be doing debounce on both pressing and releasing button (I didn’t look at the implementation so take what I’m saying with grain of salt)
Still no time, for a quick test, try running a loop of random presses/releases to simulate bounce.
both before and after the sleep