From 1736f7874b8f651ae42a21f48b6a15d90f30974a Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 29 Apr 2024 13:53:11 +1000 Subject: Use double-clicks instead of long-presses for wheel shortcuts --- src/input/input_trigger.cpp | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'src/input/input_trigger.cpp') diff --git a/src/input/input_trigger.cpp b/src/input/input_trigger.cpp index 9485ecb4..00d4a32d 100644 --- a/src/input/input_trigger.cpp +++ b/src/input/input_trigger.cpp @@ -8,34 +8,50 @@ #include #include -#include "esp_log.h" #include "esp_timer.h" namespace input { -Trigger::Trigger() : touch_time_ms_(), times_fired_(0) {} +Trigger::Trigger() + : touch_time_ms_(), + was_pressed_(false), + was_double_click_(false), + times_long_pressed_(0) {} auto Trigger::update(bool is_pressed) -> State { // Bail out early if we're in a steady-state of not pressed. - if (!is_pressed && !touch_time_ms_) { + if (!is_pressed && !was_pressed_) { + was_double_click_ = false; + times_long_pressed_ = 0; return State::kNone; } uint64_t now_ms = esp_timer_get_time() / 1000; - // Initial press of this key: record the current time, and report that we - // haven't triggered yet. - if (is_pressed && !touch_time_ms_) { + // This key wasn't being pressed, but now it is. + if (is_pressed && !was_pressed_) { + // Is this a double click? + if (now_ms - *touch_time_ms_ < kDoubleClickDelayMs) { + // Don't update touch_time_ms_, since we don't want triple clicks to + // register as double clicks. + was_double_click_ = true; + was_pressed_ = true; + return State::kDoubleClick; + } + // Not a double click; update our accounting info and wait for the next + // call. touch_time_ms_ = now_ms; - times_fired_ = 0; + was_double_click_ = false; + times_long_pressed_ = 0; + was_pressed_ = true; return State::kNone; } // The key was released. If there were no long-press events fired during the // press, then this was a standard click. - if (!is_pressed && touch_time_ms_) { - touch_time_ms_.reset(); - if (times_fired_ == 0) { + if (!is_pressed && was_pressed_) { + was_pressed_ = false; + if (!was_double_click_ && times_long_pressed_ == 0) { return State::kClick; } else { return State::kNone; @@ -43,10 +59,10 @@ auto Trigger::update(bool is_pressed) -> State { } // Now the more complicated case: the user is continuing to press the button. - if (times_fired_ == 0) { + if (times_long_pressed_ == 0) { // We haven't fired yet, so we wait for the long-press event. if (now_ms - *touch_time_ms_ >= kLongPressDelayMs) { - times_fired_++; + times_long_pressed_++; return State::kLongPress; } } else { @@ -60,8 +76,8 @@ auto Trigger::update(bool is_pressed) -> State { // kRepeatDelayMs since the long-press event. uint16_t expected_times_fired = 1 + (time_since_long_press / kRepeatDelayMs); - if (times_fired_ < expected_times_fired) { - times_fired_++; + if (times_long_pressed_ < expected_times_fired) { + times_long_pressed_++; return State::kRepeatPress; } } -- cgit v1.2.3 From 1573a8c4cde1cd9528b422b2dcc598e37ffe94a7 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 19:12:26 +1000 Subject: WIP merge cyclically dependent components into one big component --- src/input/input_trigger.cpp | 88 --------------------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 src/input/input_trigger.cpp (limited to 'src/input/input_trigger.cpp') diff --git a/src/input/input_trigger.cpp b/src/input/input_trigger.cpp deleted file mode 100644 index 00d4a32d..00000000 --- a/src/input/input_trigger.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2024 jacqueline - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#include "input_trigger.hpp" -#include - -#include -#include "esp_timer.h" - -namespace input { - -Trigger::Trigger() - : touch_time_ms_(), - was_pressed_(false), - was_double_click_(false), - times_long_pressed_(0) {} - -auto Trigger::update(bool is_pressed) -> State { - // Bail out early if we're in a steady-state of not pressed. - if (!is_pressed && !was_pressed_) { - was_double_click_ = false; - times_long_pressed_ = 0; - return State::kNone; - } - - uint64_t now_ms = esp_timer_get_time() / 1000; - - // This key wasn't being pressed, but now it is. - if (is_pressed && !was_pressed_) { - // Is this a double click? - if (now_ms - *touch_time_ms_ < kDoubleClickDelayMs) { - // Don't update touch_time_ms_, since we don't want triple clicks to - // register as double clicks. - was_double_click_ = true; - was_pressed_ = true; - return State::kDoubleClick; - } - // Not a double click; update our accounting info and wait for the next - // call. - touch_time_ms_ = now_ms; - was_double_click_ = false; - times_long_pressed_ = 0; - was_pressed_ = true; - return State::kNone; - } - - // The key was released. If there were no long-press events fired during the - // press, then this was a standard click. - if (!is_pressed && was_pressed_) { - was_pressed_ = false; - if (!was_double_click_ && times_long_pressed_ == 0) { - return State::kClick; - } else { - return State::kNone; - } - } - - // Now the more complicated case: the user is continuing to press the button. - if (times_long_pressed_ == 0) { - // We haven't fired yet, so we wait for the long-press event. - if (now_ms - *touch_time_ms_ >= kLongPressDelayMs) { - times_long_pressed_++; - return State::kLongPress; - } - } else { - // We've already fired at least once. How long has the user been holding - // the key for? - uint64_t time_since_long_press = - now_ms - (*touch_time_ms_ + kLongPressDelayMs); - - // How many times should we have fired? - // 1 initial fire (for the long-press), plus one additional fire every - // kRepeatDelayMs since the long-press event. - uint16_t expected_times_fired = - 1 + (time_since_long_press / kRepeatDelayMs); - if (times_long_pressed_ < expected_times_fired) { - times_long_pressed_++; - return State::kRepeatPress; - } - } - - return State::kNone; -} - -} // namespace input -- cgit v1.2.3