From 77c48dcf46758d716ec93041b7c9e99dce1f979b Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 3 Oct 2020 10:45:01 -0500 Subject: [PATCH] Updated SpanButton to use Utils::PushButton SpanButton now instantiates PushButton instead of keeping track of its own triggering logic. This means that SpanButton longPress() is triggered by holding down the button for longTime WITHOUT the need to release. This is a desired change. Next: Must update Example 15 to state that SpanButtons trigger a long press withouth needing to be released. To do: Consider adding a "repeating mode" for longPress in which the SpanButton re-triggers every longTime while button continues to be pressed. Best way to implement is probably by creating a PushButton::extend() method. --- .../15-RealPushButtons/15-RealPushButtons.ino | 4 ++- src/HAP.cpp | 8 ++++-- src/HomeSpan.cpp | 26 ++----------------- src/HomeSpan.h | 6 ++--- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino b/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino index e9209c8..c7bbd00 100644 --- a/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino +++ b/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino @@ -82,6 +82,8 @@ void setup() { Serial.begin(115200); + homeSpan.setLogLevel(1); + homeSpan.begin(Category::Bridges,"HomeSpan Bridge"); new SpanAccessory(); @@ -92,7 +94,7 @@ void setup() { new SpanAccessory(); new DEV_Identify("PushButton LED","HomeSpan","123-ABC","20mA LED","0.9",0); - new DEV_DimmableLED(0,17,19,5,18); // NEW! added three extra arguments to specific the pin numbers for three SpanButtons() - see DEV_LED.h + new DEV_DimmableLED(0,17,23,5,18); // NEW! added three extra arguments to specific the pin numbers for three SpanButtons() - see DEV_LED.h } // end of setup() diff --git a/src/HAP.cpp b/src/HAP.cpp index 3c7adda..2e28aa3 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1197,8 +1197,12 @@ void HAPClient::callServiceLoops(){ void HAPClient::checkPushButtons(){ - for(int i=0;icheck(); // check if long- or short-pressed, which calls button() method in attached Service if needed + for(int i=0;ipushButton->triggered(sb->shortTime,sb->longTime)){ // if the underlying PushButton is triggered + sb->service->button(sb->pin,sb->pushButton->longPress()); // call the Service's button() routine with pin and longPress() as parameters + } + } } diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index f1fbb30..4c939b2 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1352,32 +1352,10 @@ SpanButton::SpanButton(int pin, unsigned long longTime, unsigned long shortTime) if((void(*)(int,boolean))(service->*(&SpanService::button))==(void(*)(int,boolean))(&SpanService::button)) Serial.print("*** WARNING: No button() method defined for this PushButton!\n\n"); + pushButton=new PushButton(pin); // create underlying PushButton + homeSpan.PushButtons.push_back(this); - pinMode(pin,INPUT_PULLUP); } -/////////////////////////////// - -void SpanButton::check(){ - - if(!isTriggered && !digitalRead(pin)){ - isTriggered=true; - unsigned long cTime=millis(); - shortAlarm=cTime+shortTime; - longAlarm=cTime+longTime; - } else - - if(isTriggered && digitalRead(pin)){ - unsigned long cTime=millis(); - if(cTime>longAlarm) - service->button(pin, true); - else if(cTime>shortAlarm) - service->button(pin, false); - isTriggered=false; - } - -} - - /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 65ef024..1d34fd3 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -262,15 +262,13 @@ struct SpanBuf{ // temporary storage buffer for us struct SpanButton{ int pin; // pin number - boolean isTriggered=false; // flag triggered when button is pressed unsigned long shortTime; // time (in millis) required to register a short press unsigned long longTime; // time (in millis) required to register a long press - unsigned long shortAlarm; // alarm time to trigger a short press - unsigned long longAlarm; // alarm time to triger a long press SpanService *service; // Service to which this PushButton is attached + PushButton *pushButton; // PushButton associated with this SpanButton + SpanButton(int pin, unsigned long longTime=2000, unsigned long shortTime=5); - void check(); }; /////////////////////////////////////////////////