diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 95911b8..bba3091 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -994,14 +994,14 @@ void Span::processSerialCommand(const char *c){ for(auto button=PushButtons.begin(); button!=PushButtons.end(); button++){ if((*button)->service==(*svc)){ Serial.printf(" \u25bc SpanButton: Pin=%d, Single=%ums, Double=%ums, Long=%ums, Type=",(*button)->pin,(*button)->singleTime,(*button)->doubleTime,(*button)->longTime); - if((*button)->pressed==PushButton::GROUNDED) - Serial.printf("GROUNDED\n"); - else if((*button)->pressed==PushButton::POWERED) - Serial.printf("POWERED\n"); + if((*button)->triggerType==PushButton::TRIGGER_ON_LOW) + Serial.printf("TRIGGER_ON_LOW\n"); + else if((*button)->triggerType==PushButton::TRIGGER_ON_HIGH) + Serial.printf("TRIGGER_ON_HIGH\n"); #if SOC_TOUCH_SENSOR_NUM > 0 - else if((*button)->pressed==PushButton::TOUCH) - Serial.printf("TOUCH\n"); + else if((*button)->triggerType==PushButton::TRIGGER_ON_TOUCH) + Serial.printf("TRIGGER_ON_TOUCH\n"); #endif else Serial.printf("USER-DEFINED\n"); @@ -1972,7 +1972,7 @@ SpanRange::SpanRange(int min, int max, int step){ // SpanButton // /////////////////////////////// -SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, pressTest_t pressed) : PushButton(pin, pressed){ +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); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 666d943..9111fc0 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -710,9 +710,16 @@ class SpanButton : PushButton { DOUBLE=1, LONG=2 }; + + static constexpr triggerType_t TRIGGER_ON_LOW=PushButton::TRIGGER_ON_LOW; + static constexpr triggerType_t TRIGGER_ON_HIGH=PushButton::TRIGGER_ON_HIGH; + +#if SOC_TOUCH_SENSOR_NUM > 0 + static constexpr triggerType_t TRIGGER_ON_TOUCH=PushButton::TRIGGER_ON_TOUCH; +#endif - SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200, pressTest_t pressed=GROUNDED); - SpanButton(int pin, pressTest_t pressed, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,pressed){}; + SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200, triggerType_t triggerType=TRIGGER_ON_LOW); + SpanButton(int pin, triggerType_t triggerType, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,triggerType){}; }; diff --git a/src/Utils.cpp b/src/Utils.cpp index 8ebd7cf..5997e28 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -85,20 +85,20 @@ String Utils::mask(char *c, int n){ // PushButton // //////////////////////////////// -PushButton::PushButton(int pin, pressTest_t pressed){ +PushButton::PushButton(int pin, triggerType_t triggerType){ this->pin=pin; - this->pressed=pressed; + this->triggerType=triggerType; status=0; doubleCheck=false; - if(pressed==GROUNDED) + if(triggerType==TRIGGER_ON_LOW) pinMode(pin, INPUT_PULLUP); - else if(pressed==POWERED) + else if(triggerType==TRIGGER_ON_HIGH) pinMode(pin, INPUT_PULLDOWN); #if SOC_TOUCH_SENSOR_NUM > 0 - else if (pressed==TOUCH && threshold==0){ + else if (triggerType==TRIGGER_ON_TOUCH && threshold==0){ for(int i=0;isingleAlarm){ doubleCheck=true; @@ -159,7 +159,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d break; case 3: - if(!pressed(pin)) // button has been released after a long press + if(!triggerType(pin)) // button has been released after a long press status=0; else if(cTime>longAlarm){ longAlarm=cTime+longTime; @@ -169,7 +169,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d break; case 4: - if(!pressed(pin)){ // button is released + if(!triggerType(pin)){ // button is released status=0; } else @@ -182,7 +182,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d break; case 5: - if(!pressed(pin)) // button has been released after double-click + if(!triggerType(pin)) // button has been released after double-click status=0; break; @@ -212,7 +212,7 @@ int PushButton::type(){ ////////////////////////////////////// void PushButton::wait(){ - while(pressed(pin)); + while(triggerType(pin)); } ////////////////////////////////////// diff --git a/src/Utils.h b/src/Utils.h index 43e1d44..b32922c 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -87,10 +87,10 @@ class PushButton{ protected: - typedef boolean (*pressTest_t)(int pin); + typedef boolean (*triggerType_t)(int pin); int pin; - pressTest_t pressed; + triggerType_t triggerType; public: @@ -100,25 +100,25 @@ class PushButton{ LONG=2 }; - static boolean GROUNDED(int pin){return(!digitalRead(pin));} - static boolean POWERED(int pin){return(digitalRead(pin));} + static boolean TRIGGER_ON_LOW(int pin){return(!digitalRead(pin));} + static boolean TRIGGER_ON_HIGH(int pin){return(digitalRead(pin));} #if SOC_TOUCH_VERSION_1 // ESP32 - static boolean TOUCH(int pin){return(touchRead(pin)threshold);} + static boolean TRIGGER_ON_TOUCH(int pin){return(touchRead(pin)>threshold);} #endif - PushButton(int pin, pressTest_t pressed=GROUNDED); + PushButton(int pin, triggerType_t triggerType=TRIGGER_ON_LOW); // Creates pushbutton of specified type on specified pin // -// pin: pin number to which the button is connected -// pressed: a function of of the form 'boolean f(int)' that is passed -// the parameter *pin* and returns TRUE if the button associated -// with *pin* is pressed, or FALSE if not. Can choose from 3 pre-specifed -// pressTest_t functions (GROUNDED, POWERED, and TOUCH), or write your -// own custom handler +// pin: pin number to which the button is connected +// triggerType: a function of of the form 'boolean f(int)' that is passed +// the parameter *pin* and returns TRUE if the button associated +// with *pin* is pressed, or FALSE if not. Can choose from 3 pre-specifed +// triggerType_t functions (TRIGGER_ON_LOW, TRIGGER_ON_HIGH, and TRIGGER_ON_TOUCH), or write your +// own custom handler void reset(); @@ -166,14 +166,14 @@ class PushButton{ static void setTouchCycles(uint16_t measureTime, uint16_t sleepTime){touchSetCycles(measureTime,sleepTime);} -// Sets the measure time and sleep time touch cycles , and lower threshold that triggers a touch - used only when buttonType=Button::TOUCH +// Sets the measure time and sleep time touch cycles , and lower threshold that triggers a touch - used only when triggerType=PushButton::TRIGGER_ON_TOUCH // measureTime: duration of measurement time of all touch sensors in number of clock cycles // sleepTime: duration of sleep time (between measurements) of all touch sensors number of clock cycles static void setTouchThreshold(touch_value_t thresh){threshold=thresh;} -// Sets the threshold that triggers a touch - used only when buttonType=Button::TOUCH +// Sets the threshold that triggers a touch - used only when triggerType=TRIGGER_ON_TOUCH // thresh: the read value of touch sensors, beyond which which sensors are considered touched (i.e. "pressed"). // This is a class-level value applied to all touch sensor buttons.