From 1a3887b6cfad28ec93f5e65d5358375694e2184e Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 29 Jan 2023 18:30:25 -0600 Subject: [PATCH] Added switch toggle logic to PushButton Class Adds new method toggled() which is the analog of triggered(). Next step: Extend SpanButton to access this new logic. --- src/Utils.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/Utils.h | 9 +++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 4fd4c68..09e9dde 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -88,6 +88,7 @@ PushButton::PushButton(int pin, triggerType_t triggerType){ this->pin=pin; this->triggerType=triggerType; + status=0; doubleCheck=false; @@ -95,7 +96,7 @@ PushButton::PushButton(int pin, triggerType_t triggerType){ pinMode(pin, INPUT_PULLUP); else if(triggerType==TRIGGER_ON_HIGH) pinMode(pin, INPUT_PULLDOWN); - + #if SOC_TOUCH_SENSOR_NUM > 0 else if (triggerType==TRIGGER_ON_TOUCH && threshold==0){ for(int i=0;isingleAlarm){ // switch has been in "on" state for sufficient time + toggleStatus=2; + pressType=ON; + return(true); + } + break; + + case 2: + if(!triggerType(pin)){ // switch is toggled "off" after being in "on" state + toggleStatus=0; + pressType=OFF; + return(true); + } + break; + + } // switch + + return(false); +} + +////////////////////////////////////// + boolean PushButton::primed(){ if(millis()>singleAlarm && status==1){ diff --git a/src/Utils.h b/src/Utils.h index ecac65e..eab47b3 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -80,6 +80,7 @@ typedef uint16_t touch_value_t; class PushButton{ int status; + int toggleStatus; boolean doubleCheck; uint32_t singleAlarm; uint32_t doubleAlarm; @@ -101,7 +102,9 @@ class PushButton{ enum { SINGLE=0, DOUBLE=1, - LONG=2 + LONG=2, + ON=3, + OFF=4 }; static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));} @@ -158,12 +161,14 @@ class PushButton{ int type(); -// Returns 0=Single Press, 1=Double Press, or 2=Long Press +// Returns 0=Single Press, 1=Double Press, or 2=Long Press void wait(); // Waits for button to be released. Use after Long Press if button release confirmation is desired + boolean toggled(uint16_t toggleTime); + int getPin(){return(pin);} // Returns pin number