ESP + Reorganizacja
This commit is contained in:
83
ESP32/HomeSpan-master/examples/10-RGB_LED/10-RGB_LED.ino
Normal file
83
ESP32/HomeSpan-master/examples/10-RGB_LED/10-RGB_LED.ino
Normal file
@@ -0,0 +1,83 @@
|
||||
/*********************************************************************************
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-2024 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 10: Controlling a full-color RGB LED //
|
||||
// //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#include "HomeSpan.h"
|
||||
#include "DEV_LED.h"
|
||||
|
||||
void setup() {
|
||||
|
||||
// Example 10 illustrates how to control an RGB LED to set any color and brightness.
|
||||
// The configuration below should look familiar by now. We've created a new derived Service,
|
||||
// called DEV_RgbLED to house all the required logic. You'll find all the code in DEV_LED.h.
|
||||
// For completeness, this configuration also contains an on/off LED and a dimmable LED as shown
|
||||
// in prior examples.
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
homeSpan.begin(Category::Bridges,"HomeSpan Bridge");
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("Simple LED");
|
||||
new DEV_LED(16); // Create an On/Off LED attached to pin 16
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("Dimmable LED");
|
||||
new DEV_DimmableLED(17); // Create a Dimmable (PWM-driven) LED using attached to pin 17
|
||||
|
||||
new SpanAccessory();
|
||||
new Service::AccessoryInformation();
|
||||
new Characteristic::Identify();
|
||||
new Characteristic::Name("RGB LED");
|
||||
new DEV_RgbLED(32,22,23); // Create an RGB LED attached to pins 32,22,23 (for R, G, and B LED anodes)
|
||||
|
||||
} // end of setup()
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
void loop(){
|
||||
|
||||
homeSpan.poll();
|
||||
|
||||
} // end of loop()
|
||||
190
ESP32/HomeSpan-master/examples/10-RGB_LED/DEV_LED.h
Normal file
190
ESP32/HomeSpan-master/examples/10-RGB_LED/DEV_LED.h
Normal file
@@ -0,0 +1,190 @@
|
||||
|
||||
////////////////////////////////////
|
||||
// DEVICE-SPECIFIC LED SERVICES //
|
||||
////////////////////////////////////
|
||||
|
||||
struct DEV_LED : Service::LightBulb { // ON/OFF LED
|
||||
|
||||
int ledPin; // pin number defined for this LED
|
||||
SpanCharacteristic *power; // reference to the On Characteristic
|
||||
|
||||
DEV_LED(int ledPin) : Service::LightBulb(){ // constructor() method
|
||||
|
||||
power=new Characteristic::On();
|
||||
this->ledPin=ledPin;
|
||||
pinMode(ledPin,OUTPUT);
|
||||
|
||||
Serial.print("Configuring On/Off LED: Pin="); // initialization message
|
||||
Serial.print(ledPin);
|
||||
Serial.print("\n");
|
||||
|
||||
} // end constructor
|
||||
|
||||
boolean update(){ // update() method
|
||||
|
||||
LOG1("Updating On/Off LED on pin=");
|
||||
LOG1(ledPin);
|
||||
LOG1(": Current Power=");
|
||||
LOG1(power->getVal()?"true":"false");
|
||||
LOG1(" New Power=");
|
||||
LOG1(power->getNewVal()?"true":"false");
|
||||
LOG1("\n");
|
||||
|
||||
digitalWrite(ledPin,power->getNewVal());
|
||||
|
||||
return(true); // return true
|
||||
|
||||
} // update
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
|
||||
struct DEV_DimmableLED : Service::LightBulb { // Dimmable LED
|
||||
|
||||
LedPin *ledPin; // reference to Led Pin
|
||||
SpanCharacteristic *power; // reference to the On Characteristic
|
||||
SpanCharacteristic *level; // reference to the Brightness Characteristic
|
||||
|
||||
DEV_DimmableLED(int pin) : Service::LightBulb(){ // constructor() method
|
||||
|
||||
power=new Characteristic::On();
|
||||
|
||||
level=new Characteristic::Brightness(50); // Brightness Characteristic with an initial value of 50%
|
||||
level->setRange(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->ledPin=new LedPin(pin); // configures a PWM LED for output to the specified pin
|
||||
|
||||
Serial.print("Configuring Dimmable LED: Pin="); // initialization message
|
||||
Serial.print(ledPin->getPin());
|
||||
Serial.print("\n");
|
||||
|
||||
} // end constructor
|
||||
|
||||
boolean update(){ // update() method
|
||||
|
||||
LOG1("Updating Dimmable LED on pin=");
|
||||
LOG1(ledPin->getPin());
|
||||
LOG1(": Current Power=");
|
||||
LOG1(power->getVal()?"true":"false");
|
||||
LOG1(" Current Brightness=");
|
||||
LOG1(level->getVal());
|
||||
|
||||
if(power->updated()){
|
||||
LOG1(" New Power=");
|
||||
LOG1(power->getNewVal()?"true":"false");
|
||||
}
|
||||
|
||||
if(level->updated()){
|
||||
LOG1(" New Brightness=");
|
||||
LOG1(level->getNewVal());
|
||||
}
|
||||
|
||||
LOG1("\n");
|
||||
|
||||
ledPin->set(power->getNewVal()*level->getNewVal());
|
||||
|
||||
return(true); // return true
|
||||
|
||||
} // update
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
|
||||
struct DEV_RgbLED : Service::LightBulb { // RGB LED (Command Cathode)
|
||||
|
||||
LedPin *redPin, *greenPin, *bluePin;
|
||||
|
||||
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 red_pin, int green_pin, int blue_pin) : 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%
|
||||
V->setRange(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->redPin=new LedPin(red_pin); // configures a PWM LED for output to the RED pin
|
||||
this->greenPin=new LedPin(green_pin); // configures a PWM LED for output to the GREEN pin
|
||||
this->bluePin=new LedPin(blue_pin); // configures a PWM LED for output to the BLUE pin
|
||||
|
||||
char cBuf[128];
|
||||
sprintf(cBuf,"Configuring RGB LED: Pins=(%d,%d,%d)\n",redPin->getPin(),greenPin->getPin(),bluePin->getPin());
|
||||
Serial.print(cBuf);
|
||||
|
||||
} // end constructor
|
||||
|
||||
boolean update(){ // update() method
|
||||
|
||||
boolean p;
|
||||
float v, h, s, r, g, b;
|
||||
|
||||
h=H->getVal<float>(); // get and store all current values. Note the use of the <float> template to properly read the values
|
||||
s=S->getVal<float>();
|
||||
v=V->getVal<float>(); // though H and S are defined as FLOAT in HAP, V (which is brightness) is defined as INT, but will be re-cast appropriately
|
||||
p=power->getVal();
|
||||
|
||||
char cBuf[128];
|
||||
sprintf(cBuf,"Updating RGB LED: Pins=(%d,%d,%d): ",redPin->getPin(),greenPin->getPin(),bluePin->getPin());
|
||||
LOG1(cBuf);
|
||||
|
||||
if(power->updated()){
|
||||
p=power->getNewVal();
|
||||
sprintf(cBuf,"Power=%s->%s, ",power->getVal()?"true":"false",p?"true":"false");
|
||||
} else {
|
||||
sprintf(cBuf,"Power=%s, ",p?"true":"false");
|
||||
}
|
||||
LOG1(cBuf);
|
||||
|
||||
if(H->updated()){
|
||||
h=H->getNewVal<float>();
|
||||
sprintf(cBuf,"H=%.0f->%.0f, ",H->getVal<float>(),h);
|
||||
} else {
|
||||
sprintf(cBuf,"H=%.0f, ",h);
|
||||
}
|
||||
LOG1(cBuf);
|
||||
|
||||
if(S->updated()){
|
||||
s=S->getNewVal<float>();
|
||||
sprintf(cBuf,"S=%.0f->%.0f, ",S->getVal<float>(),s);
|
||||
} else {
|
||||
sprintf(cBuf,"S=%.0f, ",s);
|
||||
}
|
||||
LOG1(cBuf);
|
||||
|
||||
if(V->updated()){
|
||||
v=V->getNewVal<float>();
|
||||
sprintf(cBuf,"V=%.0f->%.0f ",V->getVal<float>(),v);
|
||||
} else {
|
||||
sprintf(cBuf,"V=%.0f ",v);
|
||||
}
|
||||
LOG1(cBuf);
|
||||
|
||||
// Here we call a static function of LedPin that converts HSV to RGB.
|
||||
// Parameters must all be floats in range of H[0,360], S[0,1], and V[0,1]
|
||||
// R, G, B, returned [0,1] range as well
|
||||
|
||||
LedPin::HSVtoRGB(h,s/100.0,v/100.0,&r,&g,&b); // since HomeKit provides S and V in percent, scale down by 100
|
||||
|
||||
int R, G, B;
|
||||
|
||||
R=p*r*100; // since LedPin uses percent, scale back up by 100, and multiple by status fo power (either 0 or 1)
|
||||
G=p*g*100;
|
||||
B=p*b*100;
|
||||
|
||||
sprintf(cBuf,"RGB=(%d,%d,%d)\n",R,G,B);
|
||||
LOG1(cBuf);
|
||||
|
||||
redPin->set(R); // update each ledPin with new values
|
||||
greenPin->set(G);
|
||||
bluePin->set(B);
|
||||
|
||||
return(true); // return true
|
||||
|
||||
} // update
|
||||
};
|
||||
|
||||
//////////////////////////////////
|
||||
Reference in New Issue
Block a user