From 819c6ebe0d49116ec6401c743bebae2c9786eeab Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 3 Aug 2020 07:43:39 -0500 Subject: [PATCH] Updated Example 11 --- examples/Advanced/11-RGB_LED/11-RGB_LED.ino | 9 +- .../11-RGB_LED/11-RGB_LED/11-RGB_LED.ino | 44 ---- .../11-RGB_LED/11-RGB_LED/DEV_Identify.h | 63 ------ .../Advanced/11-RGB_LED/11-RGB_LED/DEV_LED.h | 202 ------------------ examples/Advanced/11-RGB_LED/DEV_Identify.h | 27 ++- examples/Advanced/11-RGB_LED/DEV_LED.h | 135 ++++++++++-- 6 files changed, 150 insertions(+), 330 deletions(-) delete mode 100644 examples/Advanced/11-RGB_LED/11-RGB_LED/11-RGB_LED.ino delete mode 100644 examples/Advanced/11-RGB_LED/11-RGB_LED/DEV_Identify.h delete mode 100644 examples/Advanced/11-RGB_LED/11-RGB_LED/DEV_LED.h diff --git a/examples/Advanced/11-RGB_LED/11-RGB_LED.ino b/examples/Advanced/11-RGB_LED/11-RGB_LED.ino index 9729984..5e88231 100644 --- a/examples/Advanced/11-RGB_LED/11-RGB_LED.ino +++ b/examples/Advanced/11-RGB_LED/11-RGB_LED.ino @@ -4,15 +4,14 @@ // HomeSpan: A HomeKit implementation for the ESP32 // // ------------------------------------------------ // // // -// Example 11: Controlling an RGB LED using the // -// LightBulb Service // +// Example 9: Logging messages to the Serial Monitor // +// // // // //////////////////////////////////////////////////////////// - #include "HomeSpan.h" -#include "DEV_Identify.h" -#include "DEV_LED.h" +#include "DEV_LED.h" +#include "DEV_Identify.h" void setup() { diff --git a/examples/Advanced/11-RGB_LED/11-RGB_LED/11-RGB_LED.ino b/examples/Advanced/11-RGB_LED/11-RGB_LED/11-RGB_LED.ino deleted file mode 100644 index 5e88231..0000000 --- a/examples/Advanced/11-RGB_LED/11-RGB_LED/11-RGB_LED.ino +++ /dev/null @@ -1,44 +0,0 @@ - -//////////////////////////////////////////////////////////// -// // -// HomeSpan: A HomeKit implementation for the ESP32 // -// ------------------------------------------------ // -// // -// Example 9: Logging messages to the Serial Monitor // -// // -// // -//////////////////////////////////////////////////////////// - -#include "HomeSpan.h" -#include "DEV_LED.h" -#include "DEV_Identify.h" - -void setup() { - - // Example 11 illustrates how to control an RGB LED to set any color and brightness. - // The config below should look familiar by now. We've created a new derived Service, - // call RgbLED to house all the required logic. You'll find all the code in DEV_LED.h. - - 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("LED Blinker","HomeSpan","123-ABC","20mA LED","0.9",0); - new DEV_RgbLED(0,1,2,32,22,23); // An RGB LED requires three PWM channels and three pins to be specified - -} // end of setup() - -////////////////////////////////////// - -void loop(){ - - homeSpan.poll(); - -} // end of loop() diff --git a/examples/Advanced/11-RGB_LED/11-RGB_LED/DEV_Identify.h b/examples/Advanced/11-RGB_LED/11-RGB_LED/DEV_Identify.h deleted file mode 100644 index 17cd16f..0000000 --- a/examples/Advanced/11-RGB_LED/11-RGB_LED/DEV_Identify.h +++ /dev/null @@ -1,63 +0,0 @@ - -////////////////////////////////// -// DEVICE-SPECIFIC SERVICES // -////////////////////////////////// - -// Here we define the DEV_Identify Service as derived class of AccessoryInformation - -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 - - // Next we define the constructor using all the arguments needed to implement the required Characteristics - // of AccessoryInformation, plus one extra argument at the end called "nBlinks" we will use to specify how many - // times HomeSpan should blink the built-in LED when HomeKit calls this device's Identify routine during pairing. - - 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 - } - - // How HomeKit Identifies Devices: - // - // When HomeKit first pairs with a new device it "calls" that device's identify routine for every defined Accessory. - // To do so, HomeKit requests the Identify Characteristic for each defined AccessoryInformation Service to be set to "true". - // The Identify Characteristic is write-only, so no value is ever stored, even though HomeKit is requesting its value - // be updated. We can therefore use the same update() method as if the Identify Characteristic was the same as any - // other boolean Characteristic. - - // There are many ways to implement some form of identification. For an LED, you could blink it one or more times. - // For a LightBulb, you can flash it on and off. For window shade, you could raise and lower it. - // Most commerical devices don't do anything. Because HomeSpan can be used to control many different types of - // device, below we implement a very generic routine that simply blinks the internal LED of the ESP32 the - // number of times specified above. In principle, this code could call a user-defined routine that is different - // for each physcially-attached device (light, shade, fan, etc), but in practice this is overkill. - - // Note that the blink routine below starts by turning off the built-in LED and then leaves it on once it has blinked - // the specified number of times. This is because when HomeSpan starts up if confirms to user that it has connected - // to the WiFi network by turning on the built-in LED. Thus we want to leave it on when blinking is completed. - - 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->value.BOOL?"true":"false"); - LOG1(" New Power="); - LOG1(power->newValue.BOOL?"true":"false"); - LOG1("\n"); - - digitalWrite(ledPin,power->newValue.BOOL); - - 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 <- NEW!! - 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) : Service::LightBulb(){ // 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->value.BOOL?"true":"false"); - LOG1(" Current Brightness="); - LOG1(level->value.INT); - - if(power->isUpdated){ - LOG1(" New Power="); - LOG1(power->newValue.BOOL?"true":"false"); - } - - if(level->isUpdated){ - LOG1(" New Brightness="); - LOG1(level->newValue.INT); - } - - LOG1("\n"); - - pwmPin->set(channel,power->newValue.BOOL*level->newValue.INT); - - return(StatusCode::OK); // return OK status code - - } // update -}; - -////////////////////////////////// -struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode) - - PwmPin *redPin; - PwmPin *greenPin; - PwmPin *bluePin; - int redChannel; - int greenChannel; - int blueChannel; - SpanCharacteristic *power; // reference to the On Characteristic - SpanCharacteristic *H; // reference to the Hue Characteristic - SpanCharacteristic *S; // reference to the Saturation Characteristic - SpanCharacteristic *V; // reference to the Brightness Characteristic - - DEV_RgbLED(int redChannel, int greenChannel, int blueChannel, int redPin, int greenPin, int bluePin) : Service::LightBulb(){ // constructor() method - - power=new Characteristic::On(); - H=new Characteristic::Hue(0); // instantiate the Hue Characteristic with an initial value of 0 out of 360 - S=new Characteristic::Saturation(0); // instantiate the Saturation Characteristic with an initial value of 0% - V=new Characteristic::Brightness(100); // instantiate the Brightness Characteristic with an initial value of 100% - 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->redChannel=redChannel; // save the channel number (from 0-15) - this->greenChannel=greenChannel; // save the channel number (from 0-15) - this->blueChannel=blueChannel; // save the channel number (from 0-15) - - this->redPin=new PwmPin(redChannel, redPin); // configure the PWM channel and attach the specified pin - this->greenPin=new PwmPin(greenChannel, greenPin); // configure the PWM channel and attach the specified pin - this->bluePin=new PwmPin(blueChannel, bluePin); // configure the PWM channel and attach the specified pin - - char cBuf[128]; - sprintf(cBuf,"Configuring RGB LED: Pins=(%d,%d,%d) Channels=(%d,%d,%d)\n",redPin,greenPin,bluePin,redChannel,greenChannel,blueChannel); - Serial.print(cBuf); - - } // end constructor - - StatusCode update(){ // update() method - - boolean p; - int v; - double h, s, r, g, b; - - h=H->value.FLOAT; // get all current values - s=S->value.FLOAT; - v=V->value.INT; - p=power->value.BOOL; - - char cBuf[128]; - sprintf(cBuf,"Updating RGB LED on pins=(%d,%d,%d): ",redPin->getPin(),greenPin->getPin(),bluePin->getPin()); - LOG1(cBuf); - - if(power->isUpdated){ - p=power->newValue.BOOL; - sprintf(cBuf,"Power=%s->%s, ",power->value.BOOL?"true":"false",p?"true":"false"); - } else { - sprintf(cBuf,"Power=%s, ",p?"true":"false"); - } - LOG1(cBuf); - - if(H->isUpdated){ - h=H->newValue.FLOAT; - sprintf(cBuf,"H=%d->%d, ",(int)H->value.FLOAT,(int)h); - } else { - sprintf(cBuf,"H=%d, ",(int)h); - } - LOG1(cBuf); - - if(S->isUpdated){ - s=S->newValue.FLOAT; - sprintf(cBuf,"S=%d->%d, ",(int)S->value.FLOAT,(int)s); - } else { - sprintf(cBuf,"S=%d, ",(int)s); - } - LOG1(cBuf); - - if(V->isUpdated){ - v=V->newValue.INT; - sprintf(cBuf,"V=%d->%d ",V->value.INT,v); - } else { - sprintf(cBuf,"V=%d ",v); - } - LOG1(cBuf); - - PwmPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); - - int R, G, B; - - R=p*r*100; - G=p*g*100; - B=p*b*100; - - sprintf(cBuf,"RGB=(%d,%d,%d)\n",R,G,B); - LOG1(cBuf); - - redPin->set(redChannel,R); - greenPin->set(greenChannel,G); - bluePin->set(blueChannel,B); - - return(StatusCode::OK); // return OK status code - - } // update -}; - -////////////////////////////////// diff --git a/examples/Advanced/11-RGB_LED/DEV_Identify.h b/examples/Advanced/11-RGB_LED/DEV_Identify.h index ee81c35..17cd16f 100644 --- a/examples/Advanced/11-RGB_LED/DEV_Identify.h +++ b/examples/Advanced/11-RGB_LED/DEV_Identify.h @@ -3,11 +3,17 @@ // DEVICE-SPECIFIC SERVICES // ////////////////////////////////// +// Here we define the DEV_Identify Service as derived class of AccessoryInformation + 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 - + + // Next we define the constructor using all the arguments needed to implement the required Characteristics + // of AccessoryInformation, plus one extra argument at the end called "nBlinks" we will use to specify how many + // times HomeSpan should blink the built-in LED when HomeKit calls this device's Identify routine during pairing. + 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 @@ -22,6 +28,25 @@ struct DEV_Identify : Service::AccessoryInformation { pinMode(LED_BUILTIN,OUTPUT); // make sure built-in LED is set for output } + // How HomeKit Identifies Devices: + // + // When HomeKit first pairs with a new device it "calls" that device's identify routine for every defined Accessory. + // To do so, HomeKit requests the Identify Characteristic for each defined AccessoryInformation Service to be set to "true". + // The Identify Characteristic is write-only, so no value is ever stored, even though HomeKit is requesting its value + // be updated. We can therefore use the same update() method as if the Identify Characteristic was the same as any + // other boolean Characteristic. + + // There are many ways to implement some form of identification. For an LED, you could blink it one or more times. + // For a LightBulb, you can flash it on and off. For window shade, you could raise and lower it. + // Most commerical devices don't do anything. Because HomeSpan can be used to control many different types of + // device, below we implement a very generic routine that simply blinks the internal LED of the ESP32 the + // number of times specified above. In principle, this code could call a user-defined routine that is different + // for each physcially-attached device (light, shade, fan, etc), but in practice this is overkill. + + // Note that the blink routine below starts by turning off the built-in LED and then leaves it on once it has blinked + // the specified number of times. This is because when HomeSpan starts up if confirms to user that it has connected + // to the WiFi network by turning on the built-in LED. Thus we want to leave it on when blinking is completed. + 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->value.BOOL?"true":"false"); + LOG1(" New Power="); + LOG1(power->newValue.BOOL?"true":"false"); + LOG1("\n"); + digitalWrite(ledPin,power->newValue.BOOL); return(StatusCode::OK); // return OK status code @@ -34,6 +44,7 @@ struct DEV_LED : Service::LightBulb { // ON/OFF LED struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED PwmPin *pwmPin; // reference to PWM Pin + int ledPin; // pin number defined for this LED <- NEW!! 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 @@ -42,15 +53,41 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED power=new Characteristic::On(); - level=new Characteristic::Brightness(50); // instantiate the Brightness Characteristic with an initial value of 50% + 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->pwmPin=new PwmPin(channel, ledPin); // configures the PWM channel and attach the specified ledPin. pinMode() does NOT need to be called. + 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->value.BOOL?"true":"false"); + LOG1(" Current Brightness="); + LOG1(level->value.INT); + + if(power->isUpdated){ + LOG1(" New Power="); + LOG1(power->newValue.BOOL?"true":"false"); + } + + if(level->isUpdated){ + LOG1(" New Brightness="); + LOG1(level->newValue.INT); + } + + LOG1("\n"); pwmPin->set(channel,power->newValue.BOOL*level->newValue.INT); @@ -60,35 +97,103 @@ struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED }; ////////////////////////////////// - struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode) PwmPin *redPin; PwmPin *greenPin; - PwmPin *bluePin; + PwmPin *bluePin; int redChannel; int greenChannel; int blueChannel; SpanCharacteristic *power; // reference to the On Characteristic SpanCharacteristic *H; // reference to the Hue Characteristic SpanCharacteristic *S; // reference to the Saturation Characteristic - SpanCharacteristic *B; // reference to the Brightness Characteristic + SpanCharacteristic *V; // reference to the Brightness Characteristic DEV_RgbLED(int redChannel, int greenChannel, int blueChannel, int redPin, int greenPin, int bluePin) : Service::LightBulb(){ // constructor() method power=new Characteristic::On(); - H=new Characteristic::Brightness(100); // instantiate the Brightness Characteristic with an initial value of 100% - 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% + H=new Characteristic::Hue(0); // instantiate the Hue Characteristic with an initial value of 0 out of 360 + S=new Characteristic::Saturation(0); // instantiate the Saturation Characteristic with an initial value of 0% + V=new Characteristic::Brightness(100); // instantiate the Brightness Characteristic with an initial value of 100% + 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->pwmPin=new PwmPin(channel, ledPin); // configures the PWM channel and attach the specified ledPin. pinMode() does NOT need to be called. + this->redChannel=redChannel; // save the channel number (from 0-15) + this->greenChannel=greenChannel; // save the channel number (from 0-15) + this->blueChannel=blueChannel; // save the channel number (from 0-15) + + this->redPin=new PwmPin(redChannel, redPin); // configure the PWM channel and attach the specified pin + this->greenPin=new PwmPin(greenChannel, greenPin); // configure the PWM channel and attach the specified pin + this->bluePin=new PwmPin(blueChannel, bluePin); // configure the PWM channel and attach the specified pin + + char cBuf[128]; + sprintf(cBuf,"Configuring RGB LED: Pins=(%d,%d,%d) Channels=(%d,%d,%d)\n",redPin,greenPin,bluePin,redChannel,greenChannel,blueChannel); + Serial.print(cBuf); } // end constructor StatusCode update(){ // update() method - - pwmPin->set(channel,power->newValue.BOOL*level->newValue.INT); - + + boolean p; + int v; + double h, s, r, g, b; + + h=H->value.FLOAT; // get all current values + s=S->value.FLOAT; + v=V->value.INT; + p=power->value.BOOL; + + char cBuf[128]; + sprintf(cBuf,"Updating RGB LED on pins=(%d,%d,%d): ",redPin->getPin(),greenPin->getPin(),bluePin->getPin()); + LOG1(cBuf); + + if(power->isUpdated){ + p=power->newValue.BOOL; + sprintf(cBuf,"Power=%s->%s, ",power->value.BOOL?"true":"false",p?"true":"false"); + } else { + sprintf(cBuf,"Power=%s, ",p?"true":"false"); + } + LOG1(cBuf); + + if(H->isUpdated){ + h=H->newValue.FLOAT; + sprintf(cBuf,"H=%d->%d, ",(int)H->value.FLOAT,(int)h); + } else { + sprintf(cBuf,"H=%d, ",(int)h); + } + LOG1(cBuf); + + if(S->isUpdated){ + s=S->newValue.FLOAT; + sprintf(cBuf,"S=%d->%d, ",(int)S->value.FLOAT,(int)s); + } else { + sprintf(cBuf,"S=%d, ",(int)s); + } + LOG1(cBuf); + + if(V->isUpdated){ + v=V->newValue.INT; + sprintf(cBuf,"V=%d->%d ",V->value.INT,v); + } else { + sprintf(cBuf,"V=%d ",v); + } + LOG1(cBuf); + + PwmPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); + + int R, G, B; + + R=p*r*100; + G=p*g*100; + B=p*b*100; + + sprintf(cBuf,"RGB=(%d,%d,%d)\n",R,G,B); + LOG1(cBuf); + + redPin->set(redChannel,R); + greenPin->set(greenChannel,G); + bluePin->set(blueChannel,B); + return(StatusCode::OK); // return OK status code } // update