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->triggerType=triggerType;
status=0;
doubleCheck=false;
@ -111,6 +112,14 @@ PushButton::PushButton(int pin, triggerType_t triggerType){
}
#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(){
if(millis()>singleAlarm && status==1){

View File

@ -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));}
@ -164,6 +167,8 @@ class PushButton{
// 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