From e2678f5661d7f8cf331e4d56cb4cc4ef0f51c8f9 Mon Sep 17 00:00:00 2001 From: Gregg Date: Fri, 3 Feb 2023 23:09:08 -0600 Subject: [PATCH] Added position() method to SpanToggle() Created PushButton::OPEN and PushButton::CLOSED enums. This completes all SpanToggle() functionality. Will add a SpanToggle() to UnitTest. --- src/HomeSpan.cpp | 11 ++++++++--- src/HomeSpan.h | 9 +-------- src/Utils.cpp | 16 ++++++++-------- src/Utils.h | 6 +++--- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index b07b858..d2f2c29 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -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){ 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 ==="); while(1); } @@ -2049,8 +2053,9 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t void SpanButton::check(){ - if(triggered(singleTime,longTime,doubleTime)) // if the underlying PushButton is triggered - service->button(pin,type()); // call the Service's button() routine with pin and type as parameters + if( (buttonType==BUTTON && triggered(singleTime,longTime,doubleTime)) || + (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 } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 34438d9..3dbe3c3 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -817,14 +817,6 @@ class SpanButton : public PushButton { 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_HIGH=PushButton::TRIGGER_ON_HIGH; @@ -846,6 +838,7 @@ class SpanToggle : SpanButton { public: SpanToggle(int pin, triggerType_t triggerType=TRIGGER_ON_LOW, uint16_t toggleTime=5) : SpanButton(pin,triggerType,toggleTime){buttonType=TOGGLE;}; + int position(){return(pressType);} }; /////////////////////////////// diff --git a/src/Utils.cpp b/src/Utils.cpp index 09e9dde..58aa74a 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -113,10 +113,10 @@ PushButton::PushButton(int pin, triggerType_t triggerType){ #endif if(triggerType(pin)){ - pressType=ON; + pressType=CLOSED; toggleStatus=2; } else { - pressType=OFF; + pressType=OPEN; toggleStatus=0; } @@ -208,28 +208,28 @@ boolean PushButton::toggled(uint16_t toggleTime){ switch(toggleStatus){ case 0: - if(triggerType(pin)){ // switch is toggled "on" + if(triggerType(pin)){ // switch is toggled CLOSED singleAlarm=cTime+toggleTime; toggleStatus=1; } break; case 1: - if(!triggerType(pin)){ // switch is toggled "off" too soon + if(!triggerType(pin)){ // switch is toggled back OPEN too soon 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; - pressType=ON; + pressType=CLOSED; return(true); } break; 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; - pressType=OFF; + pressType=OPEN; return(true); } break; diff --git a/src/Utils.h b/src/Utils.h index 87cc616..1fef484 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -85,13 +85,13 @@ class PushButton{ uint32_t singleAlarm; uint32_t doubleAlarm; uint32_t longAlarm; - int pressType; static touch_value_t threshold; static const int calibCount=20; protected: + int pressType; typedef boolean (*triggerType_t)(int pin); int pin; @@ -103,8 +103,8 @@ class PushButton{ SINGLE=0, // applicable only for push button DOUBLE=1, // applicable only for push button LONG=2, // applicable only for push button - ON=3, // applicable only for toggle switch - OFF=4 // applicable only for toggle switch + CLOSED=3, // applicable only for toggle switch + OPEN=4 // applicable only for toggle switch }; static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));}