2020-03-25 08:00:55 +00:00
|
|
|
#include "wled.h"
|
2020-03-31 00:38:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Alexa Voice On/Off/Brightness/Color Control. Emulates a Philips Hue bridge to Alexa.
|
|
|
|
*
|
|
|
|
* This was put together from these two excellent projects:
|
|
|
|
* https://github.com/kakopappa/arduino-esp8266-alexa-wemo-switch
|
|
|
|
* https://github.com/probonopd/ESP8266HueEmulator
|
|
|
|
*/
|
|
|
|
#include "src/dependencies/espalexa/EspalexaDevice.h"
|
2018-01-09 23:37:45 +00:00
|
|
|
|
2018-11-01 14:36:13 +00:00
|
|
|
#ifndef WLED_DISABLE_ALEXA
|
2019-03-01 16:10:42 +00:00
|
|
|
void onAlexaChange(EspalexaDevice* dev);
|
2019-01-09 21:52:42 +00:00
|
|
|
|
2017-02-21 22:59:47 +00:00
|
|
|
void alexaInit()
|
|
|
|
{
|
2019-10-18 11:26:39 +00:00
|
|
|
if (alexaEnabled && WLED_CONNECTED)
|
2017-02-21 22:59:47 +00:00
|
|
|
{
|
2019-01-09 21:52:42 +00:00
|
|
|
if (espalexaDevice == nullptr) //only init once
|
|
|
|
{
|
2019-03-01 16:10:42 +00:00
|
|
|
espalexaDevice = new EspalexaDevice(alexaInvocationName, onAlexaChange, EspalexaDeviceType::extendedcolor);
|
2019-01-09 21:52:42 +00:00
|
|
|
espalexa.addDevice(espalexaDevice);
|
|
|
|
espalexa.begin(&server);
|
|
|
|
} else {
|
|
|
|
espalexaDevice->setName(alexaInvocationName);
|
2018-11-09 16:00:36 +00:00
|
|
|
}
|
2017-02-21 22:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 21:52:42 +00:00
|
|
|
void handleAlexa()
|
2017-02-21 22:59:47 +00:00
|
|
|
{
|
2019-10-18 11:26:39 +00:00
|
|
|
if (!alexaEnabled || !WLED_CONNECTED) return;
|
2019-01-09 21:52:42 +00:00
|
|
|
espalexa.loop();
|
2017-02-21 22:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 16:10:42 +00:00
|
|
|
void onAlexaChange(EspalexaDevice* dev)
|
2017-12-27 23:37:13 +00:00
|
|
|
{
|
2019-03-01 16:10:42 +00:00
|
|
|
EspalexaDeviceProperty m = espalexaDevice->getLastChangedProperty();
|
2018-11-09 16:00:36 +00:00
|
|
|
|
2019-03-01 16:10:42 +00:00
|
|
|
if (m == EspalexaDeviceProperty::on)
|
|
|
|
{
|
2019-01-09 21:52:42 +00:00
|
|
|
if (!macroAlexaOn)
|
|
|
|
{
|
|
|
|
if (bri == 0)
|
|
|
|
{
|
|
|
|
bri = briLast;
|
2022-02-20 21:24:11 +00:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2019-01-09 21:52:42 +00:00
|
|
|
}
|
2021-06-13 23:58:12 +00:00
|
|
|
} else {
|
2021-07-09 16:54:28 +00:00
|
|
|
applyPreset(macroAlexaOn, CALL_MODE_ALEXA);
|
2021-06-13 23:58:12 +00:00
|
|
|
if (bri == 0) espalexaDevice->setValue(briLast); //stop Alexa from complaining if macroAlexaOn does not actually turn on
|
|
|
|
}
|
2019-03-01 16:10:42 +00:00
|
|
|
} else if (m == EspalexaDeviceProperty::off)
|
2018-01-09 10:55:07 +00:00
|
|
|
{
|
2019-01-09 21:52:42 +00:00
|
|
|
if (!macroAlexaOff)
|
|
|
|
{
|
|
|
|
if (bri > 0)
|
|
|
|
{
|
|
|
|
briLast = bri;
|
|
|
|
bri = 0;
|
2022-02-20 21:24:11 +00:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2019-01-09 21:52:42 +00:00
|
|
|
}
|
2021-06-13 23:58:12 +00:00
|
|
|
} else {
|
2021-07-09 16:54:28 +00:00
|
|
|
applyPreset(macroAlexaOff, CALL_MODE_ALEXA);
|
2021-06-13 23:58:12 +00:00
|
|
|
if (bri != 0) espalexaDevice->setValue(0); //stop Alexa from complaining if macroAlexaOff does not actually turn off
|
|
|
|
}
|
2019-03-01 16:10:42 +00:00
|
|
|
} else if (m == EspalexaDeviceProperty::bri)
|
2018-01-09 10:55:07 +00:00
|
|
|
{
|
2019-03-01 16:10:42 +00:00
|
|
|
bri = espalexaDevice->getValue();
|
2022-02-20 21:24:11 +00:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2019-01-09 21:52:42 +00:00
|
|
|
} else //color
|
2018-01-09 10:55:07 +00:00
|
|
|
{
|
2020-03-24 23:59:48 +00:00
|
|
|
if (espalexaDevice->getColorMode() == EspalexaColorMode::ct) //shade of white
|
|
|
|
{
|
2022-02-23 18:20:07 +00:00
|
|
|
byte rgbw[4];
|
2020-03-24 23:59:48 +00:00
|
|
|
uint16_t ct = espalexaDevice->getCt();
|
2021-11-28 03:01:58 +00:00
|
|
|
if (!ct) return;
|
|
|
|
uint16_t k = 1000000 / ct; //mireds to kelvin
|
|
|
|
|
|
|
|
if (strip.hasCCTBus()) {
|
2022-02-23 18:20:07 +00:00
|
|
|
strip.setCCT(k);
|
|
|
|
rgbw[0]= 0; rgbw[1]= 0; rgbw[2]= 0; rgbw[3]= 255;
|
2022-02-04 12:28:00 +00:00
|
|
|
} else if (strip.hasWhiteChannel()) {
|
2020-03-24 23:59:48 +00:00
|
|
|
switch (ct) { //these values empirically look good on RGBW
|
2022-02-23 18:20:07 +00:00
|
|
|
case 199: rgbw[0]=255; rgbw[1]=255; rgbw[2]=255; rgbw[3]=255; break;
|
|
|
|
case 234: rgbw[0]=127; rgbw[1]=127; rgbw[2]=127; rgbw[3]=255; break;
|
|
|
|
case 284: rgbw[0]= 0; rgbw[1]= 0; rgbw[2]= 0; rgbw[3]=255; break;
|
|
|
|
case 350: rgbw[0]=130; rgbw[1]= 90; rgbw[2]= 0; rgbw[3]=255; break;
|
|
|
|
case 383: rgbw[0]=255; rgbw[1]=153; rgbw[2]= 0; rgbw[3]=255; break;
|
|
|
|
default : colorKtoRGB(k, rgbw);
|
2020-03-24 23:59:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-23 18:20:07 +00:00
|
|
|
colorKtoRGB(k, rgbw);
|
2020-03-24 23:59:48 +00:00
|
|
|
}
|
2022-02-24 12:43:12 +00:00
|
|
|
strip.setColor(0, RGBW32(rgbw[0], rgbw[1], rgbw[2], rgbw[3]));
|
2020-03-24 23:59:48 +00:00
|
|
|
} else {
|
|
|
|
uint32_t color = espalexaDevice->getRGB();
|
2022-02-23 18:20:07 +00:00
|
|
|
strip.setColor(0, color);
|
2020-03-24 23:59:48 +00:00
|
|
|
}
|
2022-02-23 18:20:07 +00:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2018-01-09 10:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-07 21:52:48 +00:00
|
|
|
|
2018-11-09 16:00:36 +00:00
|
|
|
|
2018-11-01 14:36:13 +00:00
|
|
|
#else
|
2018-11-09 16:00:36 +00:00
|
|
|
void alexaInit(){}
|
|
|
|
void handleAlexa(){}
|
2018-11-01 14:36:13 +00:00
|
|
|
#endif
|