long press cancels multi click
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
#include "MomentaryButton.h"
|
#include "MomentaryButton.h"
|
||||||
|
|
||||||
|
#define MULTI_CLICK_WINDOW_MS 280
|
||||||
|
|
||||||
MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, bool reverse, bool pulldownup) {
|
MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, bool reverse, bool pulldownup) {
|
||||||
_pin = pin;
|
_pin = pin;
|
||||||
_reverse = reverse;
|
_reverse = reverse;
|
||||||
@@ -11,7 +13,7 @@ MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, bool reverse
|
|||||||
_threshold = 0;
|
_threshold = 0;
|
||||||
_click_count = 0;
|
_click_count = 0;
|
||||||
_last_click_time = 0;
|
_last_click_time = 0;
|
||||||
_multi_click_window = 260;
|
_multi_click_window = MULTI_CLICK_WINDOW_MS;
|
||||||
_pending_click = false;
|
_pending_click = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +28,7 @@ MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, int analog_t
|
|||||||
_threshold = analog_threshold;
|
_threshold = analog_threshold;
|
||||||
_click_count = 0;
|
_click_count = 0;
|
||||||
_last_click_time = 0;
|
_last_click_time = 0;
|
||||||
_multi_click_window = 260;
|
_multi_click_window = MULTI_CLICK_WINDOW_MS;
|
||||||
_pending_click = false;
|
_pending_click = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +45,9 @@ bool MomentaryButton::isPressed() const {
|
|||||||
|
|
||||||
void MomentaryButton::cancelClick() {
|
void MomentaryButton::cancelClick() {
|
||||||
cancel = 1;
|
cancel = 1;
|
||||||
|
down_at = 0;
|
||||||
_click_count = 0;
|
_click_count = 0;
|
||||||
|
_last_click_time = 0;
|
||||||
_pending_click = false;
|
_pending_click = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,11 +98,16 @@ int MomentaryButton::check(bool repeat_click) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (_long_millis > 0 && down_at > 0 && (unsigned long)(millis() - down_at) >= _long_millis) {
|
if (_long_millis > 0 && down_at > 0 && (unsigned long)(millis() - down_at) >= _long_millis) {
|
||||||
event = BUTTON_EVENT_LONG_PRESS;
|
if (_pending_click) {
|
||||||
down_at = 0;
|
// long press during multi-click detection - cancel pending clicks
|
||||||
_click_count = 0;
|
cancelClick();
|
||||||
_last_click_time = 0;
|
} else {
|
||||||
_pending_click = false;
|
event = BUTTON_EVENT_LONG_PRESS;
|
||||||
|
down_at = 0;
|
||||||
|
_click_count = 0;
|
||||||
|
_last_click_time = 0;
|
||||||
|
_pending_click = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (down_at > 0 && repeat_click) {
|
if (down_at > 0 && repeat_click) {
|
||||||
unsigned long diff = (unsigned long)(millis() - down_at);
|
unsigned long diff = (unsigned long)(millis() - down_at);
|
||||||
@@ -108,6 +117,10 @@ int MomentaryButton::check(bool repeat_click) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (_pending_click && (millis() - _last_click_time) >= _multi_click_window) {
|
if (_pending_click && (millis() - _last_click_time) >= _multi_click_window) {
|
||||||
|
if (down_at > 0) {
|
||||||
|
// still pressed - wait for button release before processing clicks
|
||||||
|
return event;
|
||||||
|
}
|
||||||
switch (_click_count) {
|
switch (_click_count) {
|
||||||
case 1:
|
case 1:
|
||||||
event = BUTTON_EVENT_CLICK;
|
event = BUTTON_EVENT_CLICK;
|
||||||
|
|||||||
Reference in New Issue
Block a user