Added position() method to SpanToggle()

Created PushButton::OPEN and PushButton::CLOSED enums.

This completes all SpanToggle() functionality.  Will add a SpanToggle() to UnitTest.
This commit is contained in:
Gregg 2023-02-03 23:09:08 -06:00
parent c3d0c98e04
commit e2678f5661
4 changed files with 20 additions and 22 deletions

View File

@ -2032,7 +2032,11 @@ SpanRange::SpanRange(int min, int max, int step){
SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, triggerType_t triggerType) : PushButton(pin, triggerType){ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, triggerType_t triggerType) : PushButton(pin, triggerType){
if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){ if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){
Serial.printf("\nFATAL ERROR! Can't create new SpanButton(%d,%u,%u,%u) without a defined Service ***\n",pin,longTime,singleTime,doubleTime); if(buttonType==BUTTON)
Serial.printf("\nFATAL ERROR! Can't create new SpanButton(%d,%u,%u,%u) without a defined Service ***\n",pin,longTime,singleTime,doubleTime);
else
Serial.printf("\nFATAL ERROR! Can't create new SpanToggle(%d,%u) without a defined Service ***\n",pin,longTime);
Serial.printf("\n=== PROGRAM HALTED ==="); Serial.printf("\n=== PROGRAM HALTED ===");
while(1); while(1);
} }
@ -2049,8 +2053,9 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t
void SpanButton::check(){ void SpanButton::check(){
if(triggered(singleTime,longTime,doubleTime)) // if the underlying PushButton is triggered if( (buttonType==BUTTON && triggered(singleTime,longTime,doubleTime)) ||
service->button(pin,type()); // call the Service's button() routine with pin and type as parameters (buttonType==TOGGLE && toggled(longTime)) ) // if the underlying PushButton is triggered/toggled
service->button(pin,type()); // call the Service's button() routine with pin and type as parameters
} }
/////////////////////////////// ///////////////////////////////

View File

@ -817,14 +817,6 @@ class SpanButton : public PushButton {
public: public:
enum {
SINGLE=0,
DOUBLE=1,
LONG=2,
ON=3,
OFF=4
};
static constexpr triggerType_t TRIGGER_ON_LOW=PushButton::TRIGGER_ON_LOW; static constexpr triggerType_t TRIGGER_ON_LOW=PushButton::TRIGGER_ON_LOW;
static constexpr triggerType_t TRIGGER_ON_HIGH=PushButton::TRIGGER_ON_HIGH; static constexpr triggerType_t TRIGGER_ON_HIGH=PushButton::TRIGGER_ON_HIGH;
@ -846,6 +838,7 @@ class SpanToggle : SpanButton {
public: public:
SpanToggle(int pin, triggerType_t triggerType=TRIGGER_ON_LOW, uint16_t toggleTime=5) : SpanButton(pin,triggerType,toggleTime){buttonType=TOGGLE;}; SpanToggle(int pin, triggerType_t triggerType=TRIGGER_ON_LOW, uint16_t toggleTime=5) : SpanButton(pin,triggerType,toggleTime){buttonType=TOGGLE;};
int position(){return(pressType);}
}; };
/////////////////////////////// ///////////////////////////////

View File

@ -113,10 +113,10 @@ PushButton::PushButton(int pin, triggerType_t triggerType){
#endif #endif
if(triggerType(pin)){ if(triggerType(pin)){
pressType=ON; pressType=CLOSED;
toggleStatus=2; toggleStatus=2;
} else { } else {
pressType=OFF; pressType=OPEN;
toggleStatus=0; toggleStatus=0;
} }
@ -208,28 +208,28 @@ boolean PushButton::toggled(uint16_t toggleTime){
switch(toggleStatus){ switch(toggleStatus){
case 0: case 0:
if(triggerType(pin)){ // switch is toggled "on" if(triggerType(pin)){ // switch is toggled CLOSED
singleAlarm=cTime+toggleTime; singleAlarm=cTime+toggleTime;
toggleStatus=1; toggleStatus=1;
} }
break; break;
case 1: case 1:
if(!triggerType(pin)){ // switch is toggled "off" too soon if(!triggerType(pin)){ // switch is toggled back OPEN too soon
toggleStatus=0; toggleStatus=0;
} }
else if(cTime>singleAlarm){ // switch has been in "on" state for sufficient time else if(cTime>singleAlarm){ // switch has been in CLOSED state for sufficient time
toggleStatus=2; toggleStatus=2;
pressType=ON; pressType=CLOSED;
return(true); return(true);
} }
break; break;
case 2: case 2:
if(!triggerType(pin)){ // switch is toggled "off" after being in "on" state if(!triggerType(pin)){ // switch is toggled OPEN after being in CLOSED state
toggleStatus=0; toggleStatus=0;
pressType=OFF; pressType=OPEN;
return(true); return(true);
} }
break; break;

View File

@ -85,13 +85,13 @@ class PushButton{
uint32_t singleAlarm; uint32_t singleAlarm;
uint32_t doubleAlarm; uint32_t doubleAlarm;
uint32_t longAlarm; uint32_t longAlarm;
int pressType;
static touch_value_t threshold; static touch_value_t threshold;
static const int calibCount=20; static const int calibCount=20;
protected: protected:
int pressType;
typedef boolean (*triggerType_t)(int pin); typedef boolean (*triggerType_t)(int pin);
int pin; int pin;
@ -103,8 +103,8 @@ class PushButton{
SINGLE=0, // applicable only for push button SINGLE=0, // applicable only for push button
DOUBLE=1, // applicable only for push button DOUBLE=1, // applicable only for push button
LONG=2, // applicable only for push button LONG=2, // applicable only for push button
ON=3, // applicable only for toggle switch CLOSED=3, // applicable only for toggle switch
OFF=4 // applicable only for toggle switch OPEN=4 // applicable only for toggle switch
}; };
static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));} static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));}