diff --git a/examples/Expert/14-TargetStates/14-TargetStates.ino b/examples/Expert/14-TargetStates/14-TargetStates.ino index 33ee060..e0ae5dd 100644 --- a/examples/Expert/14-TargetStates/14-TargetStates.ino +++ b/examples/Expert/14-TargetStates/14-TargetStates.ino @@ -10,7 +10,7 @@ #include "HomeSpan.h" #include "DEV_Identify.h" -#include "DEV_Doors.h" +#include "DEV_DoorsWindows.h" void setup() { @@ -74,7 +74,11 @@ void setup() { new SpanAccessory(); new DEV_Identify("Garage Door","HomeSpan","123-ABC","Door","0.9",0); - new DEV_GarageDoor(); // Create a Garage Door Opener (see DEV_Doors.h for definition) + new DEV_GarageDoor(); // Create a Garage Door Opener (see DEV_DoorsWindows.h for definition) + + new SpanAccessory(); + new DEV_Identify("Window Shade","HomeSpan","123-ABC","Shade","0.9",0); + new DEV_WindowShade(); // Create a motorized Window Shade (see DEV_DoorsWindows.h for definition) } // end of setup() diff --git a/examples/Expert/14-TargetStates/DEV_Doors.h b/examples/Expert/14-TargetStates/DEV_Doors.h deleted file mode 100644 index f603b32..0000000 --- a/examples/Expert/14-TargetStates/DEV_Doors.h +++ /dev/null @@ -1,64 +0,0 @@ - -//////////////////////////////////// -// DEVICE-SPECIFIC LED SERVICES // -//////////////////////////////////// - -struct DEV_GarageDoor : Service::GarageDoorOpener { // A Garage Door Opener - - SpanCharacteristic *current; - SpanCharacteristic *target; - SpanCharacteristic *obstruction; - - unsigned long alarmTime; - - DEV_GarageDoor(ServiceType sType=ServiceType::Regular) : Service::GarageDoorOpener(sType){ // constructor() method - - new SpanEvent(1000); // check for events on this Service every 1 second - - current=new Characteristic::CurrentDoorState(0); - target=new Characteristic::TargetDoorState(0); - obstruction=new Characteristic::ObstructionDetected(false); - - Serial.print("Configuring Garage Door Opener"); // initialization message - Serial.print("\n"); - - } // end constructor - - StatusCode update(){ // update() method - - if(target->getNewVal()==0){ - LOG1("Opening Garage Door\n"); - current->setVal(2); - obstruction->setVal(false); - } else { - LOG1("Closing Garage Door\n"); - current->setVal(3); - obstruction->setVal(false); - } - - alarmTime=millis()+10000; - - return(StatusCode::OK); // return OK status code - - } // update - - void event(){ // event() method - - if(current->getVal()==target->getVal()) - return; - - if(random(20)==0){ - current->setVal(4); - obstruction->setVal(true); - LOG1("Garage Door Obstruction Detected!\n"); - } - - if(current->getVal()==4) - return; - - if(millis()>alarmTime) - current->setVal(target->getVal()); - - } // event - -}; diff --git a/examples/Expert/14-TargetStates/DEV_DoorsWindows.h b/examples/Expert/14-TargetStates/DEV_DoorsWindows.h new file mode 100644 index 0000000..1e4876f --- /dev/null +++ b/examples/Expert/14-TargetStates/DEV_DoorsWindows.h @@ -0,0 +1,128 @@ + +//////////////////////////////////// +// DEVICE-SPECIFIC LED SERVICES // +//////////////////////////////////// + +struct DEV_GarageDoor : Service::GarageDoorOpener { // A Garage Door Opener + + SpanCharacteristic *current; + SpanCharacteristic *target; + SpanCharacteristic *obstruction; + + unsigned long alarmTime; + + DEV_GarageDoor(ServiceType sType=ServiceType::Regular) : Service::GarageDoorOpener(sType){ // constructor() method + + new SpanEvent(1000); // check for events on this Service every 1 second + + current=new Characteristic::CurrentDoorState(0); + target=new Characteristic::TargetDoorState(0); + obstruction=new Characteristic::ObstructionDetected(false); + + Serial.print("Configuring Garage Door Opener"); // initialization message + Serial.print("\n"); + + } // end constructor + + StatusCode update(){ // update() method + + if(target->getNewVal()==0){ + LOG1("Opening Garage Door\n"); + current->setVal(2); + obstruction->setVal(false); + } else { + LOG1("Closing Garage Door\n"); + current->setVal(3); + obstruction->setVal(false); + } + + alarmTime=millis()+10000; + + return(StatusCode::OK); // return OK status code + + } // update + + void event(){ // event() method + + if(current->getVal()==target->getVal()) + return; + + if(random(30)==0){ + current->setVal(4); + obstruction->setVal(true); + LOG1("Garage Door Obstruction Detected!\n"); + } + + if(current->getVal()==4) + return; + + if(millis()>alarmTime) + current->setVal(target->getVal()); + + } // event + +}; + +//////////////////////////////////// + +struct DEV_WindowShade : Service::WindowCovering { // A motorized Window Shade with Hold Feature + + SpanCharacteristic *current; + SpanCharacteristic *target; + SpanCharacteristic *state; + SpanCharacteristic *hold; + + unsigned long alarmTime; + int speed=5; + + DEV_WindowShade(ServiceType sType=ServiceType::Regular) : Service::WindowCovering(sType){ // constructor() method + + new SpanEvent(1000); // check for events on this Service every 1 second + + current=new Characteristic::CurrentPosition(0); + target=new Characteristic::TargetPosition(0); + state=new Characteristic::PositionState(2); + + Serial.print("Configuring Motorized Window Shade"); // initialization message + Serial.print("\n"); + + } // end constructor + + StatusCode update(){ // update() method + + if(target->getNewVal()>current->getVal()){ + LOG1("Raising Shade\n"); + state->setVal(1); + alarmTime=millis()+speed; + } else + if(target->getNewVal()getVal()){ + LOG1("Lowering Shade\n"); + state->setVal(0); + alarmTime=millis()+speed; + } + + return(StatusCode::OK); // return OK status code + + } // update + + void event(){ // event() method + + if(current->getVal()==target->getVal()) + return; + + if(millis()getVal()==1) + current->setVal(current->getVal()+1); + else + current->setVal(current->getVal()-1); + + if(current->getVal()==target->getVal()) + state->setVal(2); + else + alarmTime=millis()+speed; + + } // event + +}; diff --git a/src/Services.h b/src/Services.h index 5a8da1c..4a482ac 100644 --- a/src/Services.h +++ b/src/Services.h @@ -91,10 +91,14 @@ namespace Characteristic { struct CurrentDoorState : SpanCharacteristic { CurrentDoorState(uint8_t value=1) : SpanCharacteristic{"E",PR+EV,(uint8_t)value}{} }; + struct CurrentPosition : SpanCharacteristic { CurrentPosition(uint8_t value=0) : SpanCharacteristic{"6D",PR+EV,(uint8_t)value}{} }; + struct CurrentTemperature : SpanCharacteristic { CurrentTemperature(double value=0) : SpanCharacteristic{"11",PR+EV,(double)value}{} }; struct FirmwareRevision : SpanCharacteristic { FirmwareRevision(char *value) : SpanCharacteristic{"52",PR,(char *)value}{} }; + struct HoldPosition : SpanCharacteristic { HoldPosition(boolean value=false) : SpanCharacteristic{"6F",PW,(boolean)value}{} }; + struct Hue : SpanCharacteristic { Hue(double value=0) : SpanCharacteristic{"13",PR+PW+EV,(double)value}{} }; struct Identify : SpanCharacteristic { Identify() : SpanCharacteristic{"14",PW,(boolean)false}{} }; @@ -119,6 +123,8 @@ namespace Characteristic { struct PM25Density : SpanCharacteristic { PM25Density(double value=0) : SpanCharacteristic{"C6",PR+EV,(double)value}{} }; + struct PositionState : SpanCharacteristic { PositionState(uint8_t value=2) : SpanCharacteristic{"72",PR+EV,(uint8_t)value}{} }; + struct RotationDirection : SpanCharacteristic { RotationDirection(int value=0) : SpanCharacteristic{"28",PR+PW+EV,(int)value}{} }; struct RotationSpeed : SpanCharacteristic { RotationSpeed(double value=0) : SpanCharacteristic{"29",PR+PW+EV,(double)value}{} }; @@ -147,6 +153,8 @@ namespace Characteristic { struct TargetDoorState : SpanCharacteristic { TargetDoorState(uint8_t value=1) : SpanCharacteristic{"32",PR+PW+EV,(uint8_t)value}{} }; + struct TargetPosition : SpanCharacteristic { TargetPosition(uint8_t value=0) : SpanCharacteristic{"7C",PR+PW+EV,(uint8_t)value}{} }; + struct TemperatureDisplayUnits : SpanCharacteristic { TemperatureDisplayUnits(uint8_t value=0) : SpanCharacteristic{"36",PR+PW+EV,(uint8_t)value}{} }; struct Version : SpanCharacteristic { Version(char *value) : SpanCharacteristic{"37",PR,(char *)value}{} };