WLED/wled00/wled12_alexa.ino

79 lines
1.9 KiB
Arduino
Raw Normal View History

/*
* Alexa Voice On/Off/Brightness 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
*/
#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;
colorUpdated(10);
}
} 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;
colorUpdated(10);
}
} 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();
2019-01-09 22:52:42 +01:00
colorUpdated(10);
} else //color
{
2019-03-01 17:10:42 +01:00
uint32_t color = espalexaDevice->getRGB();
col[3] = ((color >> 24) & 0xFF); // white color from Alexa is "pure white only"
2019-01-09 22:52:42 +01:00
col[0] = ((color >> 16) & 0xFF);
col[1] = ((color >> 8) & 0xFF);
col[2] = (color & 0xFF);
if (useRGBW && col[3] == 0) colorRGBtoRGBW(col); // do not touch white value if EspalexaDevice.cpp did set the white channel
2019-01-09 22:52:42 +01:00
colorUpdated(10);
}
}
#else
void alexaInit(){}
void handleAlexa(){}
#endif