diff --git a/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino b/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino index e8d6235..8e7062a 100644 --- a/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino +++ b/examples/Tutorials/D-Expert/15-RealPushButtons/15-RealPushButtons.ino @@ -26,9 +26,8 @@ void setup() { new Characteristic::Version("1.1.0"); new SpanAccessory(); - new DEV_Identify("Switched LED","HomeSpan","123-ABC","20mA LED","0.9",0); - new DEV_DimmableLED(0,17); + new DEV_DimmableLED(0,17,19,5,18); } // end of setup() diff --git a/examples/Tutorials/D-Expert/15-RealPushButtons/DEV_LED.h b/examples/Tutorials/D-Expert/15-RealPushButtons/DEV_LED.h index 0e9ff37..2029754 100644 --- a/examples/Tutorials/D-Expert/15-RealPushButtons/DEV_LED.h +++ b/examples/Tutorials/D-Expert/15-RealPushButtons/DEV_LED.h @@ -11,21 +11,29 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED PwmPin *pwmPin; // reference to PWM Pin int ledPin; // pin number defined for this LED + int powerPin; // NEW! pin with pushbutton to turn on/off LED + int raisePin; // NEW! pin with pushbutton to increase brightness + int lowerPin; // NEW! pin with pushButton to decrease brightness int channel; // PWM channel used for this LED (should be unique for each LED) SpanCharacteristic *power; // reference to the On Characteristic SpanCharacteristic *level; // reference to the Brightness Characteristic - DEV_DimmableLED(int channel, int ledPin, ServiceType sType=ServiceType::Regular) : Service::LightBulb(sType){ // // NEW! modified constructor() method + DEV_DimmableLED(int channel, int ledPin, int powerPin, int raisePin, int lowerPin, ServiceType sType=ServiceType::Regular) : Service::LightBulb(sType){ power=new Characteristic::On(); level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50% new SpanRange(5,100,1); // sets the range of the Brightness to be from a min of 5%, to a max of 100%, in steps of 1% - new SpanButton(19); + new SpanButton(powerPin); // NEW! create new SpanButton to control power on pin number "powerPin" + new SpanButton(raisePin,1000); // NEW! create new SpanButton to increase brightness on pin number "raisePin" + new SpanButton(lowerPin,3000,500); // NEW! create new SpanButton to decrease brightness on pin number "lowerPin" this->channel=channel; // save the channel number (from 0-15) this->ledPin=ledPin; // save LED pin number + this->powerPin=powerPin; // NEW! save power pushbutton pin number + this->raisePin=raisePin; // NEW! save increase brightness pushbutton pin number + this->lowerPin=lowerPin; // NEW! save decrease brightness pushbutton pin number this->pwmPin=new PwmPin(channel, ledPin); // configure the PWM channel and attach the specified ledPin Serial.print("Configuring Dimmable LED: Pin="); // initialization message @@ -65,13 +73,41 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED void button(int pin, boolean isLong){ - Serial.print("Found button press on pin: "); - Serial.print(pin); - Serial.print(" type: "); - Serial.print(isLong?"LONG":"SHORT"); - Serial.print("\n"); + LOG1("Found button press on pin: "); + LOG1(pin); + LOG1(" type: "); + LOG1(isLong?"LONG":"SHORT"); + LOG1("\n"); + + if(pin==powerPin && !isLong){ + power->setVal(1-power->getVal()); + } else + if(pin==powerPin && isLong){ + if(power->getVal()) + level->setVal(100); + else + level->setVal(5); + } else + + if(pin==raisePin){ + int newLevel=level->getVal()+(isLong?10:1); + if(newLevel>100) + newLevel=100; + level->setVal(newLevel); + } else + + if(pin==lowerPin){ + int newLevel=level->getVal()-(isLong?10:1); + if(newLevel<5) + newLevel=5; + level->setVal(newLevel); + } + + pwmPin->set(channel,power->getVal()*level->getVal()); + } + }; ////////////////////////////////// diff --git a/src/HAP.cpp b/src/HAP.cpp index 252e3c9..1fb55b3 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -96,22 +96,12 @@ void HAPClient::init(){ for(int i=0;iServices.size();j++){ - SpanService *s=homeSpan.Accessories[i]->Services[j]; - - if((void*)(s->*(&SpanService::loop)) != (void*)(&SpanService::loop)) // save pointers to services in Loops vector + SpanService *s=homeSpan.Accessories[i]->Services[j]; + if((void(*)())(s->*(&SpanService::loop)) != (void(*)())(&SpanService::loop)) // save pointers to services in Loops vector homeSpan.Loops.push_back(s); } } - - for(int i=0;ipin); - Serial.print(" iid: "); - Serial.print(homeSpan.PushButtons[i]->service->iid); - Serial.print("\n"); - } - } ////////////////////////////////////// diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 87d7aff..02c4fbd 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -386,10 +386,29 @@ void Span::processSerialCommand(char *c){ } break; + case 'i':{ + Serial.print("\n*** HomeSpan Info ***\n\n"); + char cBuf[128]; + for(int i=0;iServices.size();j++){ + SpanService *s=Accessories[i]->Services[j]; + sprintf(cBuf,"Service aid=%2d iid=%2d Update: %3s Loop: %3s Button: %3s\n",Accessories[i]->aid,s->iid, + (void(*)())(s->*(&SpanService::update))!=(void(*)())(&SpanService::update)?"YES":"NO", + (void(*)())(s->*(&SpanService::loop))!=(void(*)())(&SpanService::loop)?"YES":"NO", + (void(*)(int,boolean))(s->*(&SpanService::button))!=(void(*)(int,boolean))(&SpanService::button)?"YES":"NO" + ); + Serial.print(cBuf); + } + } + Serial.print("\n*** End Status ***\n"); + } + break; + case '?': { Serial.print("\n*** HomeSpan Commands ***\n\n"); Serial.print(" s - print connection status\n"); Serial.print(" d - print attributes database\n"); + Serial.print(" i - print detailed info about configuration\n"); Serial.print(" W - delete stored WiFi data and restart\n"); Serial.print(" H - delete stored HomeKit Pairing data and restart\n"); Serial.print(" F - delete all stored WiFi Network and HomeKit Pairing data and restart\n"); @@ -1167,12 +1186,15 @@ SpanButton::SpanButton(int pin, unsigned long longTime, unsigned long shortTime) Serial.print("Configuring PushButton: Pin="); // initialization message Serial.print(pin); Serial.print("\n"); - + this->pin=pin; this->shortTime=shortTime; this->longTime=longTime; service=homeSpan.Accessories.back()->Services.back(); + if((void(*)(int,boolean))(service->*(&SpanService::button))==(void(*)(int,boolean))(&SpanService::button)) + Serial.print("*** WARNING: No button() method defined for this PushButton!\n\n"); + homeSpan.PushButtons.push_back(this); pinMode(pin,INPUT_PULLUP); @@ -1182,20 +1204,20 @@ SpanButton::SpanButton(int pin, unsigned long longTime, unsigned long shortTime) void SpanButton::check(){ - if(state==UNTRIGGERED && !digitalRead(pin)){ - state=TRIGGERED; + if(!isTriggered && !digitalRead(pin)){ + isTriggered=true; unsigned long cTime=millis(); shortAlarm=cTime+shortTime; longAlarm=cTime+longTime; } else - if(state==TRIGGERED && digitalRead(pin)){ + if(isTriggered && digitalRead(pin)){ unsigned long cTime=millis(); if(cTime>longAlarm) service->button(pin, true); else if(cTime>shortAlarm) service->button(pin, false); - state=UNTRIGGERED; + isTriggered=false; } } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 06ddeea..74c056e 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -239,19 +239,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 - enum { - UNTRIGGERED, - TRIGGERED, - SHORT, - LONG - } state=UNTRIGGERED; - SpanButton(int pin, unsigned long longTime=2000, unsigned long shortTime=5); void check(); };