diff --git a/examples/Advanced/11-RGB_LED/11-RGB_LED.ino b/examples/Advanced/11-RGB_LED/11-RGB_LED.ino new file mode 100644 index 0000000..9729984 --- /dev/null +++ b/examples/Advanced/11-RGB_LED/11-RGB_LED.ino @@ -0,0 +1,45 @@ + +//////////////////////////////////////////////////////////// +// // +// HomeSpan: A HomeKit implementation for the ESP32 // +// ------------------------------------------------ // +// // +// Example 11: Controlling an RGB LED using the // +// LightBulb Service // +// // +//////////////////////////////////////////////////////////// + + +#include "HomeSpan.h" +#include "DEV_Identify.h" +#include "DEV_LED.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/11-RGB_LED.ino b/examples/Advanced/11-RGB_LED/11-RGB_LED/11-RGB_LED.ino new file mode 100644 index 0000000..5e88231 --- /dev/null +++ b/examples/Advanced/11-RGB_LED/11-RGB_LED/11-RGB_LED.ino @@ -0,0 +1,44 @@ + +//////////////////////////////////////////////////////////// +// // +// 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 new file mode 100644 index 0000000..17cd16f --- /dev/null +++ b/examples/Advanced/11-RGB_LED/11-RGB_LED/DEV_Identify.h @@ -0,0 +1,63 @@ + +////////////////////////////////// +// 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 new file mode 100644 index 0000000..ee81c35 --- /dev/null +++ b/examples/Advanced/11-RGB_LED/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); + + } // end constructor + + StatusCode update(){ // update() method + + 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 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); // instantiate the 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. + + } // end constructor + + StatusCode update(){ // update() method + + 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 *B; // 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% + + 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. + + } // end constructor + + StatusCode update(){ // update() method + + pwmPin->set(channel,power->newValue.BOOL*level->newValue.INT); + + return(StatusCode::OK); // return OK status code + + } // update +}; + +////////////////////////////////// diff --git a/src/Settings.h b/src/Settings.h index e224953..98550b1 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -18,7 +18,7 @@ const int MAX_CONNECTIONS=8; // Verbosity -- controls message output // // 0=Minimal, 1=Informative, 2=All // -#define VERBOSITY 2 +#define VERBOSITY 1 //-------------------------------------------------// diff --git a/src/extras/PwmPin.cpp b/src/extras/PwmPin.cpp index b32bd89..a1004b0 100644 --- a/src/extras/PwmPin.cpp +++ b/src/extras/PwmPin.cpp @@ -30,3 +30,60 @@ void PwmPin::set(uint8_t channel, uint8_t level){ ledc_channel_config(&ledChannel); } + +/////////////////// + +void PwmPin::HSVtoRGB(double h, double s, double v, double *r, double *g, double *b ){ + + // The algorithm below was provided on the web at https://www.cs.rit.edu/~ncs/color/t_convert.html + // h = [0,360] + // s = [0,1] + // v = [0,1] + + int i; + double f, p, q, t; + + if( s == 0 ){ + *r = *g = *b = v; + return; + } + + h /= 60; + i = floor( h ) ; + f = h - i; + p = v * ( 1 - s ); + q = v * ( 1 - s * f ); + t = v * ( 1 - s * ( 1 - f ) ); + switch( i % 6 ) { + case 0: + *r = v; + *g = t; + *b = p; + break; + case 1: + *r = q; + *g = v; + *b = p; + break; + case 2: + *r = p; + *g = v; + *b = t; + break; + case 3: + *r = p; + *g = q; + *b = v; + break; + case 4: + *r = t; + *g = p; + *b = v; + break; + case 5: + *r = v; + *g = p; + *b = q; + break; + } +} diff --git a/src/extras/PwmPin.h b/src/extras/PwmPin.h index 79d26f2..82180a3 100644 --- a/src/extras/PwmPin.h +++ b/src/extras/PwmPin.h @@ -18,4 +18,8 @@ class PwmPin { public: PwmPin(uint8_t channel, uint8_t pin); // assigns pin to be output of one of 16 PWM channels (0-15) void set(uint8_t channel, uint8_t level); // sets the PWM duty of channel to level (0-100) + int getPin(){return pin;} // returns the pin number + + static void HSVtoRGB(double h, double s, double v, double *r, double *g, double *b ); + };