From d5ff5c506d0062da2384e2214c8cb4eac797b487 Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 17 Aug 2020 07:00:47 -0500 Subject: [PATCH] Example 10NEW -> Example 14 Updated Example 10NEW to fully reflect the use of loop() methods instead of TimedResets to emulate a push button. Renamed Example 10 to Example 14, since it must come after we introduce loops(). Deleted both original Example 10 and Example 10NEW --- .../Advanced/10-TimedResets/DEV_Blinker.h | 93 ------------------- .../10NEW-TimedResets/10NEW-TimedResets.ino | 80 ---------------- .../Advanced/10NEW-TimedResets/DEV_Identify.h | 38 -------- .../14-Pushbuttons/14-Pushbuttons.ino} | 37 ++++---- .../14-Pushbuttons}/DEV_Blinker.h | 49 +++++----- .../14-Pushbuttons}/DEV_Identify.h | 0 6 files changed, 39 insertions(+), 258 deletions(-) delete mode 100644 examples/Advanced/10-TimedResets/DEV_Blinker.h delete mode 100644 examples/Advanced/10NEW-TimedResets/10NEW-TimedResets.ino delete mode 100644 examples/Advanced/10NEW-TimedResets/DEV_Identify.h rename examples/{Advanced/10-TimedResets/10-TimedResets.ino => Expert/14-Pushbuttons/14-Pushbuttons.ino} (59%) rename examples/{Advanced/10NEW-TimedResets => Expert/14-Pushbuttons}/DEV_Blinker.h (54%) rename examples/{Advanced/10-TimedResets => Expert/14-Pushbuttons}/DEV_Identify.h (100%) diff --git a/examples/Advanced/10-TimedResets/DEV_Blinker.h b/examples/Advanced/10-TimedResets/DEV_Blinker.h deleted file mode 100644 index deaec4f..0000000 --- a/examples/Advanced/10-TimedResets/DEV_Blinker.h +++ /dev/null @@ -1,93 +0,0 @@ - -//////////////////////////////////// -// DEVICE-SPECIFIC LED SERVICES // -//////////////////////////////////// - -// NOTE: This example is constructed only for the purpose of demonstrating how to -// use SpanTimedReset() to emulate a pushbutton in HomeSpan. The length of the blinking -// routine is much longer than HomeSpan should spend on an update(). To see how this -// effects HomeKit, try changing the number of blinks to 50, or keep it at 3 and -// increase the delay times in update() so that the blink routine takes 10 seconds or more. -// When activated, HomeKit will think the device has become non-responsive. -// -// In practice, pushbuton emulation is used for very short routines, such as driving -// an IR LED or an RF transmitter to send a code to a remote device. - -struct DEV_Blinker : Service::LightBulb { // LED Blinker - - int ledPin; // pin number defined for this LED - int nBlinks; // NEW! number of times to blink - - SpanCharacteristic *power; // reference to the On Characteristic - - DEV_Blinker(int ledPin, int nBlinks) : Service::LightBulb(){ // constructor() method - - power=new Characteristic::On(); - - // Here we create a new Timed Reset of 2000 milliseconds. Similar to SpanRange(), SpanTimedReset() automatically - // attaches to the last Characteristic instantiated, which in this case the the "power" Characteristic::On above. - // SpanTimedReset() will notify HomeKit that the Characteristic has been turned off by HomeSpan 2000 milliseconds - // after HomeKit requests it be turned on. This DOES NOT cause HomeKit to send an "off" request to HomeSpan (with - // one exception --- see * below). Rather, HomeSpan is notifying HomeKit that HomeSpan itself has turned "off" the - // Characteristic, and that HomeKit should reflect this new "off" status in the Tile shown for this device in the - // HomeKit Controller. - // - // Note that in practice you'll want to set the reset time to 500ms or less to better emulate a pushbutton. - // We've used a full 2 seconds in this example for illustrative purposes only. - - new SpanTimedReset(1000); // *** NEW!! instantiate SpanTimedReset with a delay of 2000 milliseconds - - this->ledPin=ledPin; - this->nBlinks=nBlinks; // NEW! number of blinks - pinMode(ledPin,OUTPUT); - - Serial.print("Configuring LED Blinker: Pin="); // initialization message - Serial.print(ledPin); - Serial.print(" Blinks="); // NEW! add output message for number of blinks - Serial.print(nBlinks); - Serial.print("\n"); - - } // end constructor - - StatusCode update(){ // update() method - - // Instead of turning on or off the LED according to newValue, we blink it for - // the number of times specified, and leave it in the off position when finished. - // This line is deleted... - - // digitalWrite(ledPin,power->getNewVal()); - - // and is replaced by... - - if(power->getNewVal()){ // check to ensure HomeKit is requesting we "turn on" this device (else ignore) - - LOG1("Activating the LED Blinker on pin="); - LOG1(ledPin); - LOG1("\n"); - - for(int i=0;inBlinks=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; this->nBlinks=nBlinks; // NEW! number of blinks @@ -49,7 +41,7 @@ struct DEV_Blinker : Service::LightBulb { // LED Blinker StatusCode update(){ // update() method - // Instead of turning on or off the LED according to newValue, we blink it for + // NEW! Instead of turning on or off the LED according to newValue, we blink it for // the number of times specified, and leave it in the off position when finished. // This line is deleted... @@ -80,11 +72,14 @@ struct DEV_Blinker : Service::LightBulb { // LED Blinker } // update + // NEW! Here we implement a very simple loop() method that checks to see if the power Characteristic + // is "on" for at least 3 seconds. If so, it resets the value to "off" (false). + void loop(){ - if(power->getVal() && power->timeVal()>3000){ - LOG1("Resetting Blinking LED Control\n"); - power->setVal(false); + if(power->getVal() && power->timeVal()>3000){ // check that power is true, and that time since last modification is greater than 3 seconds + LOG1("Resetting Blinking LED Control\n"); // log message + power->setVal(false); // set power to false } } // loop @@ -93,9 +88,9 @@ struct DEV_Blinker : Service::LightBulb { // LED Blinker ////////////////////////////////// -// * EXCEPTION: There is an apparent bug in HomeKit such that if you have an Accessory with three or more -// Services, and the Accessory receives a notification message from the device, AND the HomeKit interface is -// open to show the detailed control for Service in the Accessory, then for some reason HomeKit tries to -// update the device with the same status it just received from the device, even though this is contrary to -// the purpose of notification requests. This is why it's a good idea to check that newValue.BOOL==true. It -// avoids triggering the device if for some reason HomeKit should send a reqeust to update newValue to false. +// HomeKit Bug Note: There is an apparent bug in HomeKit uncovered during the development of this example. +// If you have an Accessory with three or more Services, and the Accessory receives a notification message +// from the device, AND the HomeKit interface is open to show the detailed control for this Service tile +// in the HomeKit app, then for some reason HomeKit sends an update() request back to the device asking to +// set the Characteristic to the value that it just received from an Event Notification. HomeKit is not supposed +// to send update requests in response to an Event Notification. diff --git a/examples/Advanced/10-TimedResets/DEV_Identify.h b/examples/Expert/14-Pushbuttons/DEV_Identify.h similarity index 100% rename from examples/Advanced/10-TimedResets/DEV_Identify.h rename to examples/Expert/14-Pushbuttons/DEV_Identify.h