From bde63bf79d321ce90fd4aa52756d7fdde3be6ff2 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 1 Nov 2020 10:00:20 -0600 Subject: [PATCH] Updated PushButton to add initial logic for double-clicking --- src/Utils.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/Utils.h | 5 +++ 2 files changed, 89 insertions(+) diff --git a/src/Utils.cpp b/src/Utils.cpp index 97e4a7e..d1488a8 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -70,6 +70,7 @@ PushButton::PushButton(uint8_t pin){ void PushButton::init(uint8_t pin){ status=0; + doubleCheck=false; this->pin=pin; pinMode(pin, INPUT_PULLUP); } @@ -123,6 +124,83 @@ boolean PushButton::triggered(uint16_t shortTime, uint16_t longTime){ ////////////////////////////////////// +boolean PushButton::triggered(uint16_t shortTime, uint16_t longTime, uint16_t doubleTime){ + + unsigned long cTime=millis(); + + switch(status){ + + case 0: + if(doubleCheck && cTime>doubleAlarm){ + doubleCheck=false; + pressType=0; + return(true); + } + + if(!digitalRead(pin)){ // button is pressed + shortAlarm=cTime+shortTime; + if(!doubleCheck){ + status=1; + doubleAlarm=shortAlarm+doubleTime; + longAlarm=cTime+longTime; + } else { + status=4; + } + } + break; + + case 1: + case 2: + if(digitalRead(pin)){ // button is released + status=0; + if(cTime>shortAlarm){ + doubleCheck=true; + } + } else + + if(cTime>longAlarm){ // button is long-pressed + longAlarm=cTime+longTime; + status=3; + pressType=1; + return(true); + } + break; + + case 3: + if(digitalRead(pin)) // button has been released after a long press + status=0; + else if(cTime>longAlarm){ + longAlarm=cTime+longTime; + pressType=1; + return(true); + } + break; + + case 4: + if(digitalRead(pin)){ // button is released + status=0; + } else + + if(cTime>shortAlarm){ // button is still pressed + status=5; + pressType=2; + doubleCheck=false; + return(true); + } + break; + + case 5: + if(digitalRead(pin)) // button has been released after double-click + status=0; + break; + + } + + return(false); +} + +////////////////////////////////////// + boolean PushButton::primed(){ if(millis()>shortAlarm && status==1){ @@ -141,6 +219,12 @@ boolean PushButton::longPress(){ ////////////////////////////////////// +int PushButton::type(){ + return(pressType); +} + +////////////////////////////////////// + void PushButton::wait(){ while(!digitalRead(pin)); } diff --git a/src/Utils.h b/src/Utils.h index 680e5be..2a2d8d6 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -43,8 +43,11 @@ class PushButton{ int status; uint8_t pin; + boolean doubleCheck; uint32_t shortAlarm; + uint32_t doubleAlarm; uint32_t longAlarm; + int pressType; boolean isLongPress; public: @@ -75,6 +78,7 @@ class PushButton{ // Resets state of PushButton. Should be called once before any loops that will // repeatedly check the button for a trigger event. + boolean triggered(uint16_t shortTime, uint16_t longTime, uint16_t doubleTime); boolean triggered(uint16_t shortTime, uint16_t longTime); // Returns true if button has been triggered by either a Long Press or Short Press, where a @@ -94,6 +98,7 @@ class PushButton{ // After returning true, subsequent calls will always return false until the button has been released and reset. boolean longPress(); + int type(); // Returns true if last trigger event was a Long Press, or false if last trigger was a Short Press