diff --git a/examples/Advanced/11-RGB_LED/11-RGB_LED.ino b/examples/Advanced/11-RGB_LED/11-RGB_LED.ino index 64c5f3b..c8309e9 100644 --- a/examples/Advanced/11-RGB_LED/11-RGB_LED.ino +++ b/examples/Advanced/11-RGB_LED/11-RGB_LED.ino @@ -4,7 +4,7 @@ // HomeSpan: A HomeKit implementation for the ESP32 // // ------------------------------------------------ // // // -// Example 9: Logging messages to the Serial Monitor // +// Example 11: Controlling a full-color RGB LED // // // // // //////////////////////////////////////////////////////////// diff --git a/examples/Advanced/12-ServiceOptions/12-ServiceOptions.ino b/examples/Advanced/12-ServiceOptions/12-ServiceOptions.ino new file mode 100644 index 0000000..d839a3a --- /dev/null +++ b/examples/Advanced/12-ServiceOptions/12-ServiceOptions.ino @@ -0,0 +1,64 @@ + +//////////////////////////////////////////////////////////// +// // +// HomeSpan: A HomeKit implementation for the ESP32 // +// ------------------------------------------------ // +// // +// Example 12: Service Options: // +// * setting the Primary Service // +// * setting Service Names // +// // +//////////////////////////////////////////////////////////// + +#include "HomeSpan.h" +#include "DEV_LED.h" +#include "DEV_Identify.h" + +void setup() { + + // Example 12... + + Serial.begin(115200); + + homeSpan.begin(Category::Lighting,"HomeSpan LEDs"); + + new SpanAccessory(); + new DEV_Identify("Bridge #1","HomeSpan","123-ABC","HS Bridge","0.9",3); + new Service::HAPProtocolInformation(); + new Characteristic::Version("1.1.0"); + + new SpanAccessory(); + new DEV_Identify("Ceiling Fan #1","HomeSpan","123-ABC","20mA LED","0.9",0); + new DEV_DimmableLED(0,17,ServiceType::Primary); + new Service::Fan(); + new Characteristic::Active(); + new Characteristic::RotationDirection(); + new Characteristic::RotationSpeed(0); + + new SpanAccessory(); + new DEV_Identify("Ceiling Fan #2","HomeSpan","123-ABC","20mA LED","0.9",0); + new DEV_DimmableLED(0,17); + new Service::Fan(ServiceType::Primary); + new Characteristic::Active(); + new Characteristic::RotationDirection(); + new Characteristic::RotationSpeed(0); + + new SpanAccessory(); + new DEV_Identify("Ceiling Fan #3","HomeSpan","123-ABC","20mA LED","0.9",0); + new DEV_DimmableLED(0,17); + new Characteristic::Name("Main Light"); + new Service::Fan(ServiceType::Primary); + new Characteristic::Active(); + new Characteristic::RotationDirection(); + new Characteristic::RotationSpeed(0); + new Characteristic::Name("Fan"); + +} // end of setup() + +////////////////////////////////////// + +void loop(){ + + homeSpan.poll(); + +} // end of loop() diff --git a/examples/Advanced/12-ServiceOptions/DEV_Identify.h b/examples/Advanced/12-ServiceOptions/DEV_Identify.h new file mode 100644 index 0000000..7e6826f --- /dev/null +++ b/examples/Advanced/12-ServiceOptions/DEV_Identify.h @@ -0,0 +1,38 @@ + +////////////////////////////////// +// DEVICE-SPECIFIC SERVICES // +////////////////////////////////// + +struct DEV_Identify : Service::AccessoryInformation { + + int nBlinks; // number of times to blink built-in LED in identify routine + SpanCharacteristic *identify; // reference to the Identify Characteristic + + DEV_Identify(char *name, char *manu, char *sn, char *model, char *version, int nBlinks) : Service::AccessoryInformation(){ + + new Characteristic::Name(name); // create all the required Characteristics with values set based on above arguments + new Characteristic::Manufacturer(manu); + new Characteristic::SerialNumber(sn); + new Characteristic::Model(model); + new Characteristic::FirmwareRevision(version); + identify=new Characteristic::Identify(); // store a reference to the Identify Characteristic for use below + + this->nBlinks=nBlinks; // store the number of times to blink the built-in LED + + pinMode(LED_BUILTIN,OUTPUT); // make sure built-in LED is set for output + } + + StatusCode update(){ + + for(int i=0;iledPin=ledPin; + pinMode(ledPin,OUTPUT); + + Serial.print("Configuring On/Off LED: Pin="); // initialization message + Serial.print(ledPin); + Serial.print("\n"); + + } // end constructor + + StatusCode update(){ // update() method + + LOG1("Updating On/Off LED on pin="); + LOG1(ledPin); + LOG1(": Current Power="); + LOG1(power->getVal()?"true":"false"); + LOG1(" New Power="); + LOG1(power->getNewVal()?"true":"false"); + LOG1("\n"); + + digitalWrite(ledPin,power->getNewVal()); + + return(StatusCode::OK); // return OK status code + + } // update +}; + +////////////////////////////////// + +struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED + + PwmPin *pwmPin; // reference to PWM Pin + int ledPin; // pin number defined for this LED + 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){ // constructor() method + + 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% + + this->channel=channel; // save the channel number (from 0-15) + this->ledPin=ledPin; // save LED 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 + Serial.print(ledPin); + Serial.print(" Channel="); + Serial.print(channel); + Serial.print("\n"); + + } // end constructor + + StatusCode update(){ // update() method + + LOG1("Updating Dimmable LED on pin="); + LOG1(ledPin); + LOG1(": Current Power="); + LOG1(power->getVal()?"true":"false"); + LOG1(" Current Brightness="); + LOG1(level->getVal()); + + if(power->updated()){ + LOG1(" New Power="); + LOG1(power->getNewVal()?"true":"false"); + } + + if(level->updated()){ + LOG1(" New Brightness="); + LOG1(level->getNewVal()); + } + + LOG1("\n"); + + pwmPin->set(channel,power->getNewVal()*level->getNewVal()); + + return(StatusCode::OK); // return OK status code + + } // update +}; + +////////////////////////////////// diff --git a/src/Services.h b/src/Services.h index 6b0a954..fa76803 100644 --- a/src/Services.h +++ b/src/Services.h @@ -107,6 +107,8 @@ namespace Characteristic { struct SerialNumber : SpanCharacteristic { SerialNumber(char *value) : SpanCharacteristic{"30",PR,(char *)value}{} }; + struct SwingMode : SpanCharacteristic { SwingMode(uint8_t value=0) : SpanCharacteristic{"B6",PR+PW+EV,(uint8_t)value}{} }; + struct Version : SpanCharacteristic { Version(char *value) : SpanCharacteristic{"37",PR,(char *)value}{} }; }