mathertel/OneButton

Press logic not completely right

Opened this issue · 3 comments

First of all, I want to say this is a great project.

I have a button where I want to implement single, double, single hold, and double hold. All are implemented out of the box except for double hold. Implementing double hold is easy, because in the attachPress call, I just check if the last press was less than XXX milliseconds ago. If it was, set doublePress = true and then inside of attachLongPressStart (or attachDuringLongPress I simply check if doublePress is false. Then I set it to false inside of attachLongPressStop.

However, there is a problem. attachPress only gets triggered once, so when I go for the second press, it won't register it. Here's a chart showing what I mean:

press button and hold button (aka long press) triggers the following: 
"attachPress" -> "attachLongPressStart" -> "attachDuringLongPress"

press button, release button, press button and hold button (aka double click long press) triggers the following: 
"attachPress" -> "attachLongPressStart" -> "attachDuringLongPress"

What I would expect is this:

press button, release button, press button and hold button (aka double click long press) triggers the following: 
"attachPress" -> "attachPress" -> "attachLongPressStart" -> "attachDuringLongPress"

If I set the setClickMs to something extremely low like 100ms, I get what I expect:

press button, release button, press button and hold button (aka double click long press) triggers the following: 
"attachPress" -> "attachSingle" -> "attachPress" -> "attachLongPressStart" -> "attachDuringLongPress"

However, this is a problem, because now it triggers attachSingle and I see unexpected behavior.

Press should always be triggered when a button is pressed, regardless of the intervals set. Or no? Are there more events I can attach my button to, that will show exactly when the input was high and low?

If I am wrong, feel free to let me know, how you would implement double hold logic. By double hold I mean press, release, press, and hold. In the end I would like one button to be able to toggle lights (single click), set brightness to 100% or 1% (double click), and change brightness up (press and hold) and down (double press and hold). Obviously my application need to keep track of the toggle states with booleans, but I got that sorted.

This is a functional enrichment.
Can you provide a implementation for this and create a pull request ?

@mathertel I wouldn't call it functional enrichment, when "press" is not being registered correctly. I would expect to see "press" every time my switch is pressed, regardless of the delays set.

I see why you need it and your definition of "press" is meaningful but different than the current definition and will be a breaking change therefore.
I also to see n-click + press patterns being supported.