Updated PushButton to add initial logic for double-clicking
This commit is contained in:
parent
f0f761c143
commit
bde63bf79d
|
|
@ -70,6 +70,7 @@ PushButton::PushButton(uint8_t pin){
|
||||||
|
|
||||||
void PushButton::init(uint8_t pin){
|
void PushButton::init(uint8_t pin){
|
||||||
status=0;
|
status=0;
|
||||||
|
doubleCheck=false;
|
||||||
this->pin=pin;
|
this->pin=pin;
|
||||||
pinMode(pin, INPUT_PULLUP);
|
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(){
|
boolean PushButton::primed(){
|
||||||
|
|
||||||
if(millis()>shortAlarm && status==1){
|
if(millis()>shortAlarm && status==1){
|
||||||
|
|
@ -141,6 +219,12 @@ boolean PushButton::longPress(){
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
int PushButton::type(){
|
||||||
|
return(pressType);
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
|
||||||
void PushButton::wait(){
|
void PushButton::wait(){
|
||||||
while(!digitalRead(pin));
|
while(!digitalRead(pin));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,8 +43,11 @@ class PushButton{
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
|
boolean doubleCheck;
|
||||||
uint32_t shortAlarm;
|
uint32_t shortAlarm;
|
||||||
|
uint32_t doubleAlarm;
|
||||||
uint32_t longAlarm;
|
uint32_t longAlarm;
|
||||||
|
int pressType;
|
||||||
boolean isLongPress;
|
boolean isLongPress;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -75,6 +78,7 @@ class PushButton{
|
||||||
// Resets state of PushButton. Should be called once before any loops that will
|
// Resets state of PushButton. Should be called once before any loops that will
|
||||||
// repeatedly check the button for a trigger event.
|
// 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);
|
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
|
// 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.
|
// After returning true, subsequent calls will always return false until the button has been released and reset.
|
||||||
|
|
||||||
boolean longPress();
|
boolean longPress();
|
||||||
|
int type();
|
||||||
|
|
||||||
// Returns true if last trigger event was a Long Press, or false if last trigger was a Short Press
|
// Returns true if last trigger event was a Long Press, or false if last trigger was a Short Press
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue