diff --git a/ESP32/10-RGB_LED/10-RGB_LED.ino b/ESP32/10-RGB_LED/10-RGB_LED.ino new file mode 100644 index 0000000..78aeb54 --- /dev/null +++ b/ESP32/10-RGB_LED/10-RGB_LED.ino @@ -0,0 +1,78 @@ +/********************************************************************************* + * 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" +int RedHomeKit = 0; +int GreenHomeKit = 0; +int BlueHomeKit = 0; + +void setup() { + Serial.begin(115200); + + homeSpan.setPairingCode("11122333"); + homeSpan.setQRID("111-22-333"); +// konfiguracja WIFI przez port szerefowy "WsetRange(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% + + char cBuf[128]; + Serial.print(cBuf); + + } // end constructor + + boolean update(){ // update() method + + boolean p; + float v, h, s, r, g, b; + + h=H->getVal(); // get and store all current values. Note the use of the template to properly read the values + s=S->getVal(); + v=V->getVal(); // 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]; + 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(); + sprintf(cBuf,"H=%.0f->%.0f, ",H->getVal(),h); + } else { + sprintf(cBuf,"H=%.0f, ",h); + } + LOG1(cBuf); + + if(S->updated()){ + s=S->getNewVal(); + sprintf(cBuf,"S=%.0f->%.0f, ",S->getVal(),s); + } else { + sprintf(cBuf,"S=%.0f, ",s); + } + LOG1(cBuf); + + if(V->updated()){ + v=V->getNewVal(); + sprintf(cBuf,"V=%.0f->%.0f ",V->getVal(),v); + } else { + sprintf(cBuf,"V=%.0f ",v); + } + LOG1(cBuf); + + 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; + + RedHomeKit = R; + GreenHomeKit = G; + BlueHomeKit = B; + + sprintf(cBuf,"RGB=(%d,%d,%d)\n",R,G,B); + LOG1(cBuf); + + return(true); // return true + } +}; \ No newline at end of file