2020-03-25 09:00:55 +01:00
|
|
|
#include "wled.h"
|
2020-03-31 02:38:08 +02: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-10 00:37:45 +01:00
|
|
|
|
2018-11-01 15:36:13 +01:00
|
|
|
#ifndef WLED_DISABLE_ALEXA
|
2019-03-01 17:10:42 +01:00
|
|
|
void onAlexaChange(EspalexaDevice* dev);
|
2019-01-09 22:52:42 +01:00
|
|
|
|
2017-02-21 23:59:47 +01:00
|
|
|
void alexaInit()
|
|
|
|
{
|
2019-10-18 13:26:39 +02:00
|
|
|
if (alexaEnabled && WLED_CONNECTED)
|
2017-02-21 23:59:47 +01:00
|
|
|
{
|
2019-01-09 22:52:42 +01:00
|
|
|
if (espalexaDevice == nullptr) //only init once
|
|
|
|
{
|
2019-03-01 17:10:42 +01:00
|
|
|
espalexaDevice = new EspalexaDevice(alexaInvocationName, onAlexaChange, EspalexaDeviceType::extendedcolor);
|
2019-01-09 22:52:42 +01:00
|
|
|
espalexa.addDevice(espalexaDevice);
|
|
|
|
espalexa.begin(&server);
|
|
|
|
} else {
|
|
|
|
espalexaDevice->setName(alexaInvocationName);
|
2018-11-09 17:00:36 +01:00
|
|
|
}
|
2017-02-21 23:59:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 22:52:42 +01:00
|
|
|
void handleAlexa()
|
2017-02-21 23:59:47 +01:00
|
|
|
{
|
2019-10-18 13:26:39 +02:00
|
|
|
if (!alexaEnabled || !WLED_CONNECTED) return;
|
2019-01-09 22:52:42 +01:00
|
|
|
espalexa.loop();
|
2017-02-21 23:59:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 17:10:42 +01:00
|
|
|
void onAlexaChange(EspalexaDevice* dev)
|
2017-12-28 00:37:13 +01:00
|
|
|
{
|
2019-03-01 17:10:42 +01:00
|
|
|
EspalexaDeviceProperty m = espalexaDevice->getLastChangedProperty();
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2019-03-01 17:10:42 +01:00
|
|
|
if (m == EspalexaDeviceProperty::on)
|
|
|
|
{
|
2019-01-09 22:52:42 +01:00
|
|
|
if (!macroAlexaOn)
|
|
|
|
{
|
|
|
|
if (bri == 0)
|
|
|
|
{
|
|
|
|
bri = briLast;
|
2022-02-20 22:24:11 +01:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2019-01-09 22:52:42 +01:00
|
|
|
}
|
2021-06-14 01:58:12 +02:00
|
|
|
} else {
|
2021-07-09 18:54:28 +02:00
|
|
|
applyPreset(macroAlexaOn, CALL_MODE_ALEXA);
|
2021-06-14 01:58:12 +02:00
|
|
|
if (bri == 0) espalexaDevice->setValue(briLast); //stop Alexa from complaining if macroAlexaOn does not actually turn on
|
|
|
|
}
|
2019-03-01 17:10:42 +01:00
|
|
|
} else if (m == EspalexaDeviceProperty::off)
|
2018-01-09 11:55:07 +01:00
|
|
|
{
|
2019-01-09 22:52:42 +01:00
|
|
|
if (!macroAlexaOff)
|
|
|
|
{
|
|
|
|
if (bri > 0)
|
|
|
|
{
|
|
|
|
briLast = bri;
|
|
|
|
bri = 0;
|
2022-02-20 22:24:11 +01:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2019-01-09 22:52:42 +01:00
|
|
|
}
|
2021-06-14 01:58:12 +02:00
|
|
|
} else {
|
2021-07-09 18:54:28 +02:00
|
|
|
applyPreset(macroAlexaOff, CALL_MODE_ALEXA);
|
2021-06-14 01:58:12 +02:00
|
|
|
if (bri != 0) espalexaDevice->setValue(0); //stop Alexa from complaining if macroAlexaOff does not actually turn off
|
|
|
|
}
|
2019-03-01 17:10:42 +01:00
|
|
|
} else if (m == EspalexaDeviceProperty::bri)
|
2018-01-09 11:55:07 +01:00
|
|
|
{
|
2019-03-01 17:10:42 +01:00
|
|
|
bri = espalexaDevice->getValue();
|
2022-02-20 22:24:11 +01:00
|
|
|
stateUpdated(CALL_MODE_ALEXA);
|
2019-01-09 22:52:42 +01:00
|
|
|
} else //color
|
2018-01-09 11:55:07 +01:00
|
|
|
{
|
2020-03-25 00:59:48 +01:00
|
|
|
if (espalexaDevice->getColorMode() == EspalexaColorMode::ct) //shade of white
|
|
|
|
{
|
|
|
|
uint16_t ct = espalexaDevice->getCt();
|
2021-11-28 04:01:58 +01:00
|
|
|
if (!ct) return;
|
|
|
|
uint16_t k = 1000000 / ct; //mireds to kelvin
|
|
|
|
|
|
|
|
if (strip.hasCCTBus()) {
|
|
|
|
uint8_t segid = strip.getMainSegmentId();
|
|
|
|
WS2812FX::Segment& seg = strip.getSegment(segid);
|
|
|
|
seg.setCCT(k, segid);
|
2021-12-01 00:16:43 +01:00
|
|
|
col[0]= 0; col[1]= 0; col[2]= 0; col[3]= 255;
|
2022-02-04 13:28:00 +01:00
|
|
|
} else if (strip.hasWhiteChannel()) {
|
2020-03-25 00:59:48 +01:00
|
|
|
switch (ct) { //these values empirically look good on RGBW
|
|
|
|
case 199: col[0]=255; col[1]=255; col[2]=255; col[3]=255; break;
|
|
|
|
case 234: col[0]=127; col[1]=127; col[2]=127; col[3]=255; break;
|
|
|
|
case 284: col[0]= 0; col[1]= 0; col[2]= 0; col[3]=255; break;
|
|
|
|
case 350: col[0]=130; col[1]= 90; col[2]= 0; col[3]=255; break;
|
|
|
|
case 383: col[0]=255; col[1]=153; col[2]= 0; col[3]=255; break;
|
2021-11-28 04:01:58 +01:00
|
|
|
default : colorKtoRGB(k, col);
|
2020-03-25 00:59:48 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-28 04:01:58 +01:00
|
|
|
colorKtoRGB(k, col);
|
2020-03-25 00:59:48 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t color = espalexaDevice->getRGB();
|
|
|
|
|
|
|
|
col[0] = ((color >> 16) & 0xFF);
|
|
|
|
col[1] = ((color >> 8) & 0xFF);
|
|
|
|
col[2] = ( color & 0xFF);
|
2020-06-26 23:09:19 +02:00
|
|
|
col[3] = 0;
|
2020-03-25 00:59:48 +01:00
|
|
|
}
|
2021-07-09 18:54:28 +02:00
|
|
|
colorUpdated(CALL_MODE_ALEXA);
|
2018-01-09 11:55:07 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-07 22:52:48 +01:00
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-11-01 15:36:13 +01:00
|
|
|
#else
|
2018-11-09 17:00:36 +01:00
|
|
|
void alexaInit(){}
|
|
|
|
void handleAlexa(){}
|
2018-11-01 15:36:13 +01:00
|
|
|
#endif
|