From 6d6f42d242ff39cdce17850bb0bface0fac1e548 Mon Sep 17 00:00:00 2001 From: nyanpasu64 Date: Sun, 24 Jul 2022 15:12:27 -0700 Subject: [PATCH 1/3] Cleanup getButtonState() --- source/Core/Drivers/Buttons.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/source/Core/Drivers/Buttons.cpp b/source/Core/Drivers/Buttons.cpp index 3875ed96..b7111373 100644 --- a/source/Core/Drivers/Buttons.cpp +++ b/source/Core/Drivers/Buttons.cpp @@ -50,14 +50,10 @@ ButtonState getButtonState() { ButtonState retVal = BUTTON_NONE; if (currentState) { // User has pressed a button down (nothing done on down) - if (currentState != previousState) { - // There has been a change in the button states - // If there is a rising edge on one of the buttons from double press we - // want to mask that out As users are having issues with not release - // both at once - if (previousState == 0x03) - currentState = 0x03; - } + // If there is a rising edge on one of the buttons from double press we + // want to mask that out As users are having issues with not release + // both at once + previousState |= currentState; } else { // User has released buttons // If they previously had the buttons down we want to check if they were < @@ -73,8 +69,8 @@ ButtonState getButtonState() { else retVal = BUTTON_BOTH; // Both being held case } + previousState = 0; } - previousState = currentState; previousStateChange = xTaskGetTickCount(); return retVal; } From c855f4cffea349d887bd08afb1a9e2290038f0fe Mon Sep 17 00:00:00 2001 From: nyanpasu64 Date: Sun, 24 Jul 2022 15:16:11 -0700 Subject: [PATCH 2/3] Don't send short-presses after releasing long-presses --- source/Core/Drivers/Buttons.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/Core/Drivers/Buttons.cpp b/source/Core/Drivers/Buttons.cpp index b7111373..885a0b50 100644 --- a/source/Core/Drivers/Buttons.cpp +++ b/source/Core/Drivers/Buttons.cpp @@ -23,6 +23,7 @@ ButtonState getButtonState() { * (downtime>filter) */ static uint8_t previousState = 0; + static bool longPressed = false; static uint32_t previousStateChange = 0; const uint16_t timeout = TICKS_100MS * 4; uint8_t currentState; @@ -34,9 +35,10 @@ ButtonState getButtonState() { if (currentState == previousState) { if (currentState == 0) return BUTTON_NONE; - if ((xTaskGetTickCount() - previousStateChange) > timeout) { + if ((xTaskGetTickCount() - previousStateChange) >= timeout) { // User has been holding the button down // We want to send a button is held message + longPressed = true; if (currentState == 0x01) return BUTTON_F_LONG; else if (currentState == 0x02) @@ -58,7 +60,7 @@ ButtonState getButtonState() { // User has released buttons // If they previously had the buttons down we want to check if they were < // long hold and trigger a press - if ((xTaskGetTickCount() - previousStateChange) < timeout) { + if ((xTaskGetTickCount() - previousStateChange) < timeout && !longPressed) { // The user didn't hold the button for long // So we send button press @@ -70,6 +72,7 @@ ButtonState getButtonState() { retVal = BUTTON_BOTH; // Both being held case } previousState = 0; + longPressed = false; } previousStateChange = xTaskGetTickCount(); return retVal; From 06e3ca59ea82826176904fde88bd48d4a33905e5 Mon Sep 17 00:00:00 2001 From: nyanpasu64 Date: Sun, 24 Jul 2022 15:34:39 -0700 Subject: [PATCH 3/3] Fix button presses between short and long being ignored entirely --- source/Core/Drivers/Buttons.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Core/Drivers/Buttons.cpp b/source/Core/Drivers/Buttons.cpp index 885a0b50..247e47f1 100644 --- a/source/Core/Drivers/Buttons.cpp +++ b/source/Core/Drivers/Buttons.cpp @@ -60,7 +60,7 @@ ButtonState getButtonState() { // User has released buttons // If they previously had the buttons down we want to check if they were < // long hold and trigger a press - if ((xTaskGetTickCount() - previousStateChange) < timeout && !longPressed) { + if (!longPressed) { // The user didn't hold the button for long // So we send button press