From 04ad1646d3a6c25398dcaf958a83e77d4a3f43f9 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 25 Jul 2020 21:52:49 -0500 Subject: [PATCH] Created Example 7 Implements DEV_Identify in it's own file DEV_Identify.h --- .../7-IdentifyRoutines/7-IdentifyRoutines.ino | 89 +++++++++++++++++++ .../7-IdentifyRoutines/DEV_Identify.h | 63 +++++++++++++ .../Intermediate/7-IdentifyRoutines/DEV_LED.h | 60 +++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 examples/Intermediate/7-IdentifyRoutines/7-IdentifyRoutines.ino create mode 100644 examples/Intermediate/7-IdentifyRoutines/DEV_Identify.h create mode 100644 examples/Intermediate/7-IdentifyRoutines/DEV_LED.h diff --git a/examples/Intermediate/7-IdentifyRoutines/7-IdentifyRoutines.ino b/examples/Intermediate/7-IdentifyRoutines/7-IdentifyRoutines.ino new file mode 100644 index 0000000..97d2065 --- /dev/null +++ b/examples/Intermediate/7-IdentifyRoutines/7-IdentifyRoutines.ino @@ -0,0 +1,89 @@ + +//////////////////////////////////////////////////////////// +// // +// HomeSpan: A HomeKit implementation for the ESP32 // +// ------------------------------------------------ // +// // +// Example 7: Transforming AccessoryInformation into a // +// derived Service that implements the // +// Identify Characteristic // +// // +//////////////////////////////////////////////////////////// + +#include "HomeSpan.h" +#include "DEV_LED.h" +#include "DEV_Identify.h" // NEW! This is where we store all code for the DEV_Identify Service + +void setup() { + + // In Example 5 we saw how to create a derived Service to encapsulate all the functionality needed to implement DEV_LED + // in it's own DEV_LED.h file. Then, in Example 6 we extended that further by implementing DEV_DimmableLED. In this + // example we do the same for the AccessoryInformation Service. Note how AccessoryInformation, and all of its + // Characteristics, need to be defined for every Accessory. By deriving a new Service that implements a multi-argument + // constructor we can avoid having to separately create each required Characteristic every time. Creating a derived Service + // also allows us to implement device-specific code for the Identify Characteristic. We will call this derived Service + // DEV_Identify, and store its code in "DEV_Identify.h" which has already been included above. + + // As usual, all previous comments have been deleted and only new changes from the previous example are shown. + + // NOTE: To see how this works in practice, you'll need to unpair your device and re-pair it once the new code is loaded. + // This will allow oyu to activate the identify routines. + + Serial.begin(115200); + + homeSpan.begin(Category::Lighting,"HomeSpan LEDs"); + + new SpanAccessory(); + + // Rather than instantiate the AccessoryInformation Service and all of it's required Characteristics, + // we'll delete these line (comment them out)... + + // new Service::AccessoryInformation(); + // new Characteristic::Name("LED #1"); + // new Characteristic::Manufacturer("HomeSpan"); + // new Characteristic::SerialNumber("123-ABC"); + // new Characteristic::Model("20mA LED"); + // new Characteristic::FirmwareRevision("0.9"); + // new Characteristic::Identify(); + + // ...and replace them with this single line that implements everything above. See DEV_Identify.h for + // details on how this is defined. Note there is an extra argument at the end we set to 3. + // This optional argument will be used to run the identify routine (see code for details) + + new DEV_Identify("LED #1","HomeSpan","123-ABC","20mA LED","0.9",3); // NEW! This implements all the Characteristics above + + new Service::HAPProtocolInformation(); + new Characteristic::Version("1.1.0"); + + new DEV_LED(16); // create an on/off LED attached to pin 16 (same as in Example 5) + + new SpanAccessory(); + + // Same as above, we can replace all of this... + + // new Service::AccessoryInformation(); + // new Characteristic::Name("LED #2"); + // new Characteristic::Manufacturer("HomeSpan"); + // new Characteristic::SerialNumber("123-ABC"); + // new Characteristic::Model("20mA LED"); + // new Characteristic::FirmwareRevision("0.9"); + // new Characteristic::Identify(); + + // ...with this (note we set last argument to 5 this time - see code for what this does) + + new DEV_Identify("LED #2","HomeSpan","123-ABC","20mA LED","0.9",5); // NEW! This implements all the Characteristics above + + new Service::HAPProtocolInformation(); + new Characteristic::Version("1.1.0"); + + new DEV_DimmableLED(0,17); // NEW! create a dimmable LED attached to pin 17 using PWM channel 0. See new code at end of DEV_LED.h + +} // end of setup() + +////////////////////////////////////// + +void loop(){ + + homeSpan.poll(); + +} // end of loop() diff --git a/examples/Intermediate/7-IdentifyRoutines/DEV_Identify.h b/examples/Intermediate/7-IdentifyRoutines/DEV_Identify.h new file mode 100644 index 0000000..17cd16f --- /dev/null +++ b/examples/Intermediate/7-IdentifyRoutines/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); + + } // 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); // 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); // configure 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 +}; + +//////////////////////////////////