diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 935c8ff..67343b3 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1963,7 +1963,7 @@ SpanRange::SpanRange(int min, int max, int step){ // SpanButton // /////////////////////////////// -SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, Button buttonType){ +SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, Button buttonType) : PushButton(pin, buttonType){ 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); @@ -1971,13 +1971,11 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t while(1); } - this->pin=pin; this->longTime=longTime; this->singleTime=singleTime; this->doubleTime=doubleTime; service=homeSpan.Accessories.back()->Services.back(); - pushButton=new PushButton(pin,buttonType); // create underlying PushButton homeSpan.PushButtons.push_back(this); } @@ -1985,8 +1983,8 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t void SpanButton::check(){ - if(pushButton->triggered(singleTime,longTime,doubleTime)) // if the underlying PushButton is triggered - service->button(pin,pushButton->type()); // call the Service's button() routine with pin and type as parameters + 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 } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 9c2262d..6f05a30 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -691,17 +691,15 @@ struct [[deprecated("Please use Characteristic::setRange() method instead.")]] S /////////////////////////////// -class SpanButton{ +class SpanButton : PushButton { friend class Span; friend class SpanService; - int pin; // pin number uint16_t singleTime; // minimum time (in millis) required to register a single press uint16_t longTime; // minimum time (in millis) required to register a long press uint16_t doubleTime; // maximum time (in millis) between single presses to register a double press instead SpanService *service; // Service to which this PushButton is attached - PushButton *pushButton; // PushButton associated with this SpanButton void check(); // check PushButton and call button() if pressed @@ -715,8 +713,8 @@ class SpanButton{ SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200, Button buttonType=Button::GROUNDED); SpanButton(int pin, Button buttonType, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,buttonType){}; + SpanButton(int pin, boolean (*pressed)(int pin), uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,Button::CUSTOM){this->pressed=pressed;}; - static void configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){PushButton::configureTouch(measureTime,sleepTime,thresh);} }; /////////////////////////////// diff --git a/src/Utils.cpp b/src/Utils.cpp index 12cd87b..625aea9 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -113,6 +113,12 @@ PushButton::PushButton(int pin, Button buttonType){ ////////////////////////////////////// +PushButton::PushButton(int pin, boolean (*pressed)(int pin)) : PushButton(pin, Button::CUSTOM){ + this->pressed=pressed; +} + +////////////////////////////////////// + boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t doubleTime){ unsigned long cTime=millis(); @@ -220,6 +226,13 @@ void PushButton::reset(){ ////////////////////////////////////// +void PushButton::configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){ + touchSetCycles(measureTime,sleepTime); + touchThreshold=thresh; +} + +////////////////////////////////////// + uint16_t PushButton::touchThreshold; //////////////////////////////// diff --git a/src/Utils.h b/src/Utils.h index a9f288c..5f84f51 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -40,7 +40,8 @@ String mask(char *c, int n); // simply utility that creates a String fr enum class Button { GROUNDED, POWERED, - TOUCH + TOUCH, + CUSTOM }; ///////////////////////////////////////////////// @@ -80,13 +81,11 @@ struct TempBuffer { class PushButton{ int status; - int pin; boolean doubleCheck; uint32_t singleAlarm; uint32_t doubleAlarm; uint32_t longAlarm; int pressType; - boolean (*pressed)(int pin); static uint16_t touchThreshold; @@ -94,6 +93,11 @@ class PushButton{ static boolean poweredButton(int pin){return(digitalRead(pin));} static boolean touchButton(int pin){return(touchRead(pin)