WLED/wled00/wled_alexa.cpp

94 lines
2.3 KiB
C++
Raw Normal View History

#include "wled_alexa.h"
#include "wled.h"
#include "const.h"
2020-03-25 10:14:23 +01:00
#include "wled_led.h"
#include "wled_eeprom.h"
#include "wled_colors.h"
#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);
}
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)
{
2019-03-01 17:10:42 +01:00
EspalexaDeviceProperty m = espalexaDevice->getLastChangedProperty();
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;
2020-02-22 16:17:32 +01:00
colorUpdated(NOTIFIER_CALL_MODE_ALEXA);
2019-01-09 22:52:42 +01:00
}
} else applyMacro(macroAlexaOn);
2019-03-01 17:10:42 +01:00
} else if (m == EspalexaDeviceProperty::off)
{
2019-01-09 22:52:42 +01:00
if (!macroAlexaOff)
{
if (bri > 0)
{
briLast = bri;
bri = 0;
2020-02-22 16:17:32 +01:00
colorUpdated(NOTIFIER_CALL_MODE_ALEXA);
2019-01-09 22:52:42 +01:00
}
} else applyMacro(macroAlexaOff);
2019-03-01 17:10:42 +01:00
} else if (m == EspalexaDeviceProperty::bri)
{
2019-03-01 17:10:42 +01:00
bri = espalexaDevice->getValue();
2020-02-22 16:17:32 +01:00
colorUpdated(NOTIFIER_CALL_MODE_ALEXA);
2019-01-09 22:52:42 +01:00
} else //color
{
2020-03-25 00:59:48 +01:00
if (espalexaDevice->getColorMode() == EspalexaColorMode::ct) //shade of white
{
uint16_t ct = espalexaDevice->getCt();
if (useRGBW)
{
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;
}
} else {
colorCTtoRGB(ct, col);
}
} else {
uint32_t color = espalexaDevice->getRGB();
col[0] = ((color >> 16) & 0xFF);
col[1] = ((color >> 8) & 0xFF);
col[2] = ( color & 0xFF);
}
2020-02-22 16:17:32 +01:00
colorUpdated(NOTIFIER_CALL_MODE_ALEXA);
}
}
#else
void alexaInit(){}
void handleAlexa(){}
#endif