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.
This commit is contained in:
Gregg 2023-01-29 18:30:25 -06:00
parent 03ba061b9b
commit 1a3887b6cf
2 changed files with 57 additions and 3 deletions

View File

@ -88,6 +88,7 @@ PushButton::PushButton(int pin, triggerType_t triggerType){
this->pin=pin; this->pin=pin;
this->triggerType=triggerType; this->triggerType=triggerType;
status=0; status=0;
doubleCheck=false; doubleCheck=false;
@ -95,7 +96,7 @@ PushButton::PushButton(int pin, triggerType_t triggerType){
pinMode(pin, INPUT_PULLUP); pinMode(pin, INPUT_PULLUP);
else if(triggerType==TRIGGER_ON_HIGH) else if(triggerType==TRIGGER_ON_HIGH)
pinMode(pin, INPUT_PULLDOWN); pinMode(pin, INPUT_PULLDOWN);
#if SOC_TOUCH_SENSOR_NUM > 0 #if SOC_TOUCH_SENSOR_NUM > 0
else if (triggerType==TRIGGER_ON_TOUCH && threshold==0){ else if (triggerType==TRIGGER_ON_TOUCH && threshold==0){
for(int i=0;i<calibCount;i++) for(int i=0;i<calibCount;i++)
@ -110,6 +111,14 @@ PushButton::PushButton(int pin, triggerType_t triggerType){
#endif #endif
} }
#endif #endif
if(triggerType(pin)){
pressType=ON;
toggleStatus=2;
} else {
pressType=OFF;
toggleStatus=0;
}
} }
@ -192,6 +201,46 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
////////////////////////////////////// //////////////////////////////////////
boolean PushButton::toggled(uint16_t toggleTime){
unsigned long cTime=millis();
switch(toggleStatus){
case 0:
if(triggerType(pin)){ // switch is toggled "on"
singleAlarm=cTime+toggleTime;
toggleStatus=1;
}
break;
case 1:
if(!triggerType(pin)){ // switch is toggled "off" too soon
toggleStatus=0;
}
else if(cTime>singleAlarm){ // 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(){ boolean PushButton::primed(){
if(millis()>singleAlarm && status==1){ if(millis()>singleAlarm && status==1){

View File

@ -80,6 +80,7 @@ typedef uint16_t touch_value_t;
class PushButton{ class PushButton{
int status; int status;
int toggleStatus;
boolean doubleCheck; boolean doubleCheck;
uint32_t singleAlarm; uint32_t singleAlarm;
uint32_t doubleAlarm; uint32_t doubleAlarm;
@ -101,7 +102,9 @@ class PushButton{
enum { enum {
SINGLE=0, SINGLE=0,
DOUBLE=1, DOUBLE=1,
LONG=2 LONG=2,
ON=3,
OFF=4
}; };
static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));} static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));}
@ -158,12 +161,14 @@ class PushButton{
int type(); 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(); void wait();
// Waits for button to be released. Use after Long Press if button release confirmation is desired // 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);} int getPin(){return(pin);}
// Returns pin number // Returns pin number