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.
This commit is contained in:
Gregg 2020-10-03 10:45:01 -05:00
parent e350aa6723
commit 77c48dcf46
4 changed files with 13 additions and 31 deletions

View File

@ -82,6 +82,8 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
homeSpan.setLogLevel(1);
homeSpan.begin(Category::Bridges,"HomeSpan Bridge"); homeSpan.begin(Category::Bridges,"HomeSpan Bridge");
new SpanAccessory(); new SpanAccessory();
@ -92,7 +94,7 @@ void setup() {
new SpanAccessory(); new SpanAccessory();
new DEV_Identify("PushButton LED","HomeSpan","123-ABC","20mA LED","0.9",0); 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() } // end of setup()

View File

@ -1197,8 +1197,12 @@ void HAPClient::callServiceLoops(){
void HAPClient::checkPushButtons(){ void HAPClient::checkPushButtons(){
for(int i=0;i<homeSpan.PushButtons.size();i++) // loop over all defined pushbuttons for(int i=0;i<homeSpan.PushButtons.size();i++){ // loop over all defined pushbuttons
homeSpan.PushButtons[i]->check(); // check if long- or short-pressed, which calls button() method in attached Service if needed SpanButton *sb=homeSpan.PushButtons[i]; // temporary pointer to SpanButton
if(sb->pushButton->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
}
}
} }

View File

@ -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)) if((void(*)(int,boolean))(service->*(&SpanService::button))==(void(*)(int,boolean))(&SpanService::button))
Serial.print("*** WARNING: No button() method defined for this PushButton!\n\n"); Serial.print("*** WARNING: No button() method defined for this PushButton!\n\n");
pushButton=new PushButton(pin); // create underlying PushButton
homeSpan.PushButtons.push_back(this); 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;
}
}
/////////////////////////////// ///////////////////////////////

View File

@ -262,15 +262,13 @@ struct SpanBuf{ // temporary storage buffer for us
struct SpanButton{ struct SpanButton{
int pin; // pin number 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 shortTime; // time (in millis) required to register a short press
unsigned long longTime; // time (in millis) required to register a long 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 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); SpanButton(int pin, unsigned long longTime=2000, unsigned long shortTime=5);
void check();
}; };
///////////////////////////////////////////////// /////////////////////////////////////////////////