mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-05 00:56:02 +00:00
long press cancels multi click
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "MomentaryButton.h"
|
||||
|
||||
#define MULTI_CLICK_WINDOW_MS 280
|
||||
|
||||
MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, bool reverse, bool pulldownup) {
|
||||
_pin = pin;
|
||||
_reverse = reverse;
|
||||
@@ -11,7 +13,7 @@ MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, bool reverse
|
||||
_threshold = 0;
|
||||
_click_count = 0;
|
||||
_last_click_time = 0;
|
||||
_multi_click_window = 260;
|
||||
_multi_click_window = MULTI_CLICK_WINDOW_MS;
|
||||
_pending_click = false;
|
||||
}
|
||||
|
||||
@@ -26,7 +28,7 @@ MomentaryButton::MomentaryButton(int8_t pin, int long_press_millis, int analog_t
|
||||
_threshold = analog_threshold;
|
||||
_click_count = 0;
|
||||
_last_click_time = 0;
|
||||
_multi_click_window = 260;
|
||||
_multi_click_window = MULTI_CLICK_WINDOW_MS;
|
||||
_pending_click = false;
|
||||
}
|
||||
|
||||
@@ -43,7 +45,9 @@ bool MomentaryButton::isPressed() const {
|
||||
|
||||
void MomentaryButton::cancelClick() {
|
||||
cancel = 1;
|
||||
down_at = 0;
|
||||
_click_count = 0;
|
||||
_last_click_time = 0;
|
||||
_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) {
|
||||
event = BUTTON_EVENT_LONG_PRESS;
|
||||
down_at = 0;
|
||||
_click_count = 0;
|
||||
_last_click_time = 0;
|
||||
_pending_click = false;
|
||||
if (_pending_click) {
|
||||
// long press during multi-click detection - cancel pending clicks
|
||||
cancelClick();
|
||||
} else {
|
||||
event = BUTTON_EVENT_LONG_PRESS;
|
||||
down_at = 0;
|
||||
_click_count = 0;
|
||||
_last_click_time = 0;
|
||||
_pending_click = false;
|
||||
}
|
||||
}
|
||||
if (down_at > 0 && repeat_click) {
|
||||
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 (down_at > 0) {
|
||||
// still pressed - wait for button release before processing clicks
|
||||
return event;
|
||||
}
|
||||
switch (_click_count) {
|
||||
case 1:
|
||||
event = BUTTON_EVENT_CLICK;
|
||||
|
||||
Reference in New Issue
Block a user