/*********************************************************************************
* MIT License
*
* Copyright (c) 2020-2022 Gregg E. Berman
*
* https://github.com/HomeSpan/HomeSpan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
********************************************************************************/
////////////////////////////////////////////////////////////
// //
// HomeSpan: A HomeKit implementation for the ESP32 //
// ------------------------------------------------ //
// //
// Example 6: One working on/off LED and one working //
// dimmable LED, both based on the LightBulb //
// Service //
// //
////////////////////////////////////////////////////////////
#include "HomeSpan.h"
#include "extras/PwmPin.h"
struct FadingLED : Service::LightBulb {
LedPin *ledPin; // reference to Led Pin
SpanCharacteristic *power; // reference to the On Characteristic
SpanCharacteristic *level; // reference to the Brightness Characteristic
FadingLED(int _ledPin) : Service::LightBulb(){
power=new Characteristic::On();
level=new Characteristic::Brightness(0);
ledPin=new LedPin(_ledPin);
}
boolean update(){
ledPin->fade(power->getNewVal()*level->getNewVal(),2000,LedPin::PROPORTIONAL); // use fade() to set new level; timing=2 seconds, proportional scale
while(ledPin->fadeStatus()==LedPin::FADING); // wait until fading is completed
return(true);
}
};
void setup() {
// Example 6 changes Example 5 so that LED #2 is now dimmable, instead of just on/off. This requires us to create a new
// derived Service we will name "DEV_DimmableLED" Instead of creating a new file to store this definition, we will simply
// tack it on to the end of the DEV_LED.h file that includes the code we created in Example 5 to control an on/off LED.
// Grouping similar-style Services in one ".h" file makes it easier to organize your custom devices.
// As usual, all previous comments have been deleted and only new changes from the previous example are shown.
//FadingLED() annotation if using built in pwm of esp32
// NOTE: The Arduino/ESP32 code base does not include the function analogWrite() which is typically used to create a PWM
// output to drive the brightness of an LED. The ESP32 code base itself includes a set of functions to create PWM output
// and the ESP32 chip has built-in PWM functionality specifically for this purpose. There are numerous libraries
// you can download that mimics or reproduces analogWrite() in some form or another. HomeSpan conveniently comes with
// its own version of a wrapper around the ESP32 PWM classes that make it very easy to define LED pins, and set the
// PWM level (or duty cycle) from 0-100%. These functions are encapsualted in the LedPin class, as defined in
// extras/PwmPin.h. We will include this file in our updated DEV_LED.h for use with DEV_DimmableLED.
Serial.begin(115200);
homeSpan.enableOTA();
//homeSpan.setPortNum(80);
homeSpan.setControlPin(21,PushButton::TRIGGER_ON_HIGH).setStatusPin(12);
homeSpan.setStatusAutoOff(45);
homeSpan.begin(Category::Lighting, "ESP32-WROOM-32D");
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Temp");
new FadingLED(33); // NECESSARY FOR ALL THE BELOW TO LOAD INTO HOMEKIT. STILL UNSURE WHY.
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Sofa");
new FadingLED(23);
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Sofa Side Tables");
new FadingLED(22);
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Piano Keys");
new FadingLED(19);
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Piano Music");
new FadingLED(18);
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Picture Light");
new FadingLED(17);
/* new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Island");
new DEV_DimmableLED(19); // BBPIN R17 - BUNDLE 1 - BROWN???
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Entry");
new DEV_DimmableLED(12);
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Temp2");
new DEV_DimmableLED(36); //
*/
} // end of setup()
//////////////////////////////////////
void loop() {
homeSpan.poll();
} // end of loop()