2017-02-19 16:49:17 +00:00
|
|
|
/*
|
2017-10-01 13:32:36 +01:00
|
|
|
xdrv_snfled.ino - PWM, WS2812 and sonoff led support for Sonoff-Tasmota
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
Copyright (C) 2017 Theo Arends
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-19 16:49:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
2017-10-01 13:32:36 +01:00
|
|
|
* PWM, WS2812, Sonoff B1, AiLight, Sonoff Led and BN-SZ01
|
2017-09-02 13:37:02 +01:00
|
|
|
*
|
2017-10-18 17:22:34 +01:00
|
|
|
* light_type Module Color ColorTemp
|
2017-10-01 13:32:36 +01:00
|
|
|
* 1 PWM1 W no (Sonoff BN-SZ)
|
|
|
|
* 2 PWM2 CW yes (Sonoff Led)
|
|
|
|
* 3 PWM3 RGB no (H801 and MagicHome)
|
|
|
|
* 4 PWM4 RGBW no (H801 and MagicHome)
|
|
|
|
* 5 PWM5 RGBCW yes (H801)
|
|
|
|
* 9 reserved no
|
|
|
|
* 10 reserved yes
|
|
|
|
* 11 +WS2812 RGB no
|
|
|
|
* 12 AiLight RGBW no
|
|
|
|
* 13 Sonoff B1 RGBCW yes
|
2017-09-16 16:34:03 +01:00
|
|
|
*
|
|
|
|
* led_scheme WS2812 Others Effect
|
|
|
|
* 0 yes yes Color On/Off
|
|
|
|
* 1 yes yes Wakeup light
|
|
|
|
* 2 yes no Clock
|
|
|
|
* 3 yes no Incandescent
|
|
|
|
* 4 yes no RGB
|
|
|
|
* 5 yes no Christmas
|
|
|
|
* 6 yes no Hanukkah
|
|
|
|
* 7 yes no Kwanzaa
|
|
|
|
* 8 yes no Rainbow
|
|
|
|
* 9 yes no Fire
|
|
|
|
*
|
2017-02-19 16:49:17 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
uint8_t ledTable[] = {
|
2017-09-16 16:34:03 +01:00
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4,
|
|
|
|
4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8,
|
|
|
|
8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 14,
|
|
|
|
14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 22,
|
|
|
|
22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32,
|
|
|
|
33, 33, 34, 35, 36, 36, 37, 38, 39, 40, 40, 41, 42, 43, 44, 45,
|
|
|
|
46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
|
|
|
|
61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78,
|
|
|
|
80, 81, 82, 83, 85, 86, 87, 89, 90, 91, 93, 94, 95, 97, 98, 99,
|
|
|
|
101,102,104,105,107,108,110,111,113,114,116,117,119,121,122,124,
|
|
|
|
125,127,129,130,132,134,135,137,139,141,142,144,146,148,150,151,
|
|
|
|
153,155,157,159,161,163,165,166,168,170,172,174,176,178,180,182,
|
|
|
|
184,186,189,191,193,195,197,199,201,204,206,208,210,212,215,217,
|
|
|
|
219,221,224,226,228,231,233,235,238,240,243,245,248,250,253,255 };
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-08-12 16:55:20 +01:00
|
|
|
uint8_t sl_dcolor[5];
|
|
|
|
uint8_t sl_tcolor[5];
|
|
|
|
uint8_t sl_lcolor[5];
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-09-16 17:19:15 +01:00
|
|
|
uint8_t sl_power = 0;
|
2017-10-04 16:29:49 +01:00
|
|
|
uint8_t sl_any = 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
uint8_t sl_wakeupActive = 0;
|
|
|
|
uint8_t sl_wakeupDimmer = 0;
|
|
|
|
uint16_t sl_wakeupCntr = 0;
|
|
|
|
|
2017-09-16 16:34:03 +01:00
|
|
|
unsigned long stripTimerCntr = 0; // Bars and Gradient
|
|
|
|
|
2017-08-12 16:55:20 +01:00
|
|
|
/*********************************************************************************************\
|
2017-08-16 16:05:36 +01:00
|
|
|
* Sonoff B1 and AiLight inspired by OpenLight https://github.com/icamgo/noduino-sdk
|
2017-08-12 16:55:20 +01:00
|
|
|
\*********************************************************************************************/
|
2017-07-15 14:07:30 +01:00
|
|
|
|
2017-08-13 10:19:34 +01:00
|
|
|
extern "C" {
|
|
|
|
void os_delay_us(unsigned int);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t sl_pdi;
|
|
|
|
uint8_t sl_pdcki;
|
2017-08-12 16:55:20 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightDiPulse(uint8_t times)
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-08-13 10:19:34 +01:00
|
|
|
for (uint8_t i = 0; i < times; i++) {
|
|
|
|
digitalWrite(sl_pdi, HIGH);
|
|
|
|
digitalWrite(sl_pdi, LOW);
|
2017-07-03 10:45:15 +01:00
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightDckiPulse(uint8_t times)
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
2017-08-13 10:19:34 +01:00
|
|
|
for (uint8_t i = 0; i < times; i++) {
|
|
|
|
digitalWrite(sl_pdcki, HIGH);
|
|
|
|
digitalWrite(sl_pdcki, LOW);
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightMy92x1Write(uint8_t data)
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
2017-08-16 16:05:36 +01:00
|
|
|
for (uint8_t i = 0; i < 4; i++) { // Send 8bit Data
|
|
|
|
digitalWrite(sl_pdcki, LOW);
|
|
|
|
digitalWrite(sl_pdi, (data & 0x80));
|
|
|
|
digitalWrite(sl_pdcki, HIGH);
|
|
|
|
data = data << 1;
|
|
|
|
digitalWrite(sl_pdi, (data & 0x80));
|
|
|
|
digitalWrite(sl_pdcki, LOW);
|
|
|
|
digitalWrite(sl_pdi, LOW);
|
|
|
|
data = data << 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightMy92x1Init()
|
2017-08-16 16:05:36 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t chips = light_type -11; // 1 (AiLight) or 2 (Sonoff B1)
|
2017-08-12 16:55:20 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
LightDckiPulse(chips * 32); // Clear all duty register
|
2017-08-16 16:05:36 +01:00
|
|
|
os_delay_us(12); // TStop > 12us.
|
2017-08-12 16:55:20 +01:00
|
|
|
// Send 12 DI pulse, after 6 pulse's falling edge store duty data, and 12
|
|
|
|
// pulse's rising edge convert to command mode.
|
2017-10-18 17:22:34 +01:00
|
|
|
LightDiPulse(12);
|
2017-08-16 16:05:36 +01:00
|
|
|
os_delay_us(12); // Delay >12us, begin send CMD data
|
2017-08-13 14:31:49 +01:00
|
|
|
for (uint8_t n = 0; n < chips; n++) { // Send CMD data
|
2017-10-18 17:22:34 +01:00
|
|
|
LightMy92x1Write(0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-08-16 16:05:36 +01:00
|
|
|
os_delay_us(12); // TStart > 12us. Delay 12 us.
|
2017-08-12 16:55:20 +01:00
|
|
|
// Send 16 DI pulse, at 14 pulse's falling edge store CMD data, and
|
|
|
|
// at 16 pulse's falling edge convert to duty mode.
|
2017-10-18 17:22:34 +01:00
|
|
|
LightDiPulse(16);
|
2017-08-16 16:05:36 +01:00
|
|
|
os_delay_us(12); // TStop > 12us.
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightMy92x1Duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t duty_w, uint8_t duty_c)
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
2017-08-16 16:05:36 +01:00
|
|
|
uint8_t channels[2] = { 4, 6 };
|
2017-08-12 16:55:20 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t didx = light_type -12; // 0 or 1
|
2017-08-16 16:05:36 +01:00
|
|
|
|
|
|
|
uint8_t duty[2][6] = {{ duty_r, duty_g, duty_b, duty_w, 0, 0 }, // Definition for RGBW channels
|
|
|
|
{ duty_w, duty_c, 0, duty_g, duty_r, duty_b }}; // Definition for RGBWC channels
|
|
|
|
|
|
|
|
os_delay_us(12); // TStop > 12us.
|
|
|
|
for (uint8_t channel = 0; channel < channels[didx]; channel++) {
|
2017-10-18 17:22:34 +01:00
|
|
|
LightMy92x1Write(duty[didx][channel]); // Send 8bit Data
|
2017-08-13 14:31:49 +01:00
|
|
|
}
|
2017-08-16 16:05:36 +01:00
|
|
|
os_delay_us(12); // TStart > 12us. Ready for send DI pulse.
|
2017-10-18 17:22:34 +01:00
|
|
|
LightDiPulse(8); // Send 8 DI pulse. After 8 pulse falling edge, store old data.
|
2017-08-16 16:05:36 +01:00
|
|
|
os_delay_us(12); // TStop > 12us.
|
2017-08-13 14:31:49 +01:00
|
|
|
}
|
|
|
|
|
2017-08-12 16:55:20 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightInit(void)
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if (light_type < 6) { // PWM
|
|
|
|
for (byte i = 0; i < light_type; i++) {
|
|
|
|
Settings.pwm_value[i] = 0; // Disable direct PWM control
|
2017-10-05 12:28:31 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if (1 == light_type) {
|
|
|
|
Settings.led_color[0] = 255; // One PWM channel only supports Dimmer but needs max color
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if (SONOFF_LED == Settings.module) { // Fix Sonoff Led instabilities
|
2017-10-01 13:32:36 +01:00
|
|
|
if (!my_module.gp.io[4]) {
|
2017-10-05 12:28:31 +01:00
|
|
|
pinMode(4, OUTPUT); // Stop floating outputs
|
2017-10-01 13:32:36 +01:00
|
|
|
digitalWrite(4, LOW);
|
|
|
|
}
|
|
|
|
if (!my_module.gp.io[5]) {
|
2017-10-05 12:28:31 +01:00
|
|
|
pinMode(5, OUTPUT); // Stop floating outputs
|
2017-10-01 13:32:36 +01:00
|
|
|
digitalWrite(5, LOW);
|
|
|
|
}
|
|
|
|
if (!my_module.gp.io[14]) {
|
2017-10-05 12:28:31 +01:00
|
|
|
pinMode(14, OUTPUT); // Stop floating outputs
|
2017-10-01 13:32:36 +01:00
|
|
|
digitalWrite(14, LOW);
|
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_scheme = 0;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#ifdef USE_WS2812 // ************************************************************************
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (11 == light_type) {
|
|
|
|
Ws2812Init();
|
|
|
|
if (1 == Settings.led_scheme) {
|
|
|
|
Settings.led_scheme = 0;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_WS2812 ************************************************************************
|
|
|
|
else {
|
2017-08-13 10:19:34 +01:00
|
|
|
sl_pdi = pin[GPIO_DI];
|
|
|
|
sl_pdcki = pin[GPIO_DCKI];
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-08-13 10:19:34 +01:00
|
|
|
pinMode(sl_pdi, OUTPUT);
|
|
|
|
pinMode(sl_pdcki, OUTPUT);
|
|
|
|
digitalWrite(sl_pdi, LOW);
|
|
|
|
digitalWrite(sl_pdcki, LOW);
|
2017-08-12 16:55:20 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
LightMy92x1Init();
|
|
|
|
Settings.led_scheme = 0;
|
2017-07-03 10:45:15 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-09-16 17:19:15 +01:00
|
|
|
sl_power = 0;
|
2017-10-04 16:29:49 +01:00
|
|
|
sl_any = 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
sl_wakeupActive = 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightSetColorTemp(uint16_t ct)
|
2017-08-16 16:05:36 +01:00
|
|
|
{
|
|
|
|
/* Color Temperature (https://developers.meethue.com/documentation/core-concepts)
|
2017-09-02 13:37:02 +01:00
|
|
|
*
|
2017-08-16 16:05:36 +01:00
|
|
|
* ct = 153 = 2000K = Warm = CCWW = 00FF
|
|
|
|
* ct = 500 = 6500K = Cold = CCWW = FF00
|
|
|
|
*/
|
|
|
|
uint16_t my_ct = ct - 153;
|
|
|
|
if (my_ct > 347) {
|
|
|
|
my_ct = 347;
|
|
|
|
}
|
|
|
|
uint16_t icold = (100 * (347 - my_ct)) / 136;
|
|
|
|
uint16_t iwarm = (100 * my_ct) / 136;
|
2017-10-18 17:22:34 +01:00
|
|
|
if (5 == (light_type &7)) {
|
|
|
|
Settings.led_color[0] = 0;
|
|
|
|
Settings.led_color[1] = 0;
|
|
|
|
Settings.led_color[2] = 0;
|
|
|
|
Settings.led_color[3] = (uint8_t)icold;
|
|
|
|
Settings.led_color[4] = (uint8_t)iwarm;
|
2017-08-16 16:05:36 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_color[0] = (uint8_t)icold;
|
|
|
|
Settings.led_color[1] = (uint8_t)iwarm;
|
2017-08-16 16:05:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint16_t LightGetColorTemp()
|
2017-08-16 16:05:36 +01:00
|
|
|
{
|
|
|
|
uint8_t ct_idx = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
if (5 == (light_type &7)) {
|
2017-08-16 16:05:36 +01:00
|
|
|
ct_idx = 3;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
uint16_t my_ct = Settings.led_color[ct_idx +1];
|
2017-08-16 16:05:36 +01:00
|
|
|
if (my_ct > 0) {
|
|
|
|
return ((my_ct * 136) / 100) + 154;
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
my_ct = Settings.led_color[ct_idx];
|
2017-08-16 16:05:36 +01:00
|
|
|
return 499 - ((my_ct * 136) / 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightSetDimmer(uint8_t myDimmer)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-08-12 16:55:20 +01:00
|
|
|
float temp;
|
2017-08-15 21:09:52 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((SONOFF_BN == Settings.module) && (100 == myDimmer)) {
|
2017-08-12 16:55:20 +01:00
|
|
|
myDimmer = 99; // BN-SZ01 starts flickering at dimmer = 100
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
float dimmer = 100 / (float)myDimmer;
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
|
|
|
temp = (float)Settings.led_color[i] / dimmer;
|
2017-08-12 16:55:20 +01:00
|
|
|
sl_dcolor[i] = (uint8_t)temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightSetColor()
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
|
|
|
uint8_t highest = 0;
|
|
|
|
float temp;
|
2017-07-15 14:07:30 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-08-12 16:55:20 +01:00
|
|
|
if (highest < sl_dcolor[i]) {
|
|
|
|
highest = sl_dcolor[i];
|
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
float mDim = (float)highest / 2.55;
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_dimmer = (uint8_t)mDim;
|
|
|
|
float dimmer = 100 / mDim;
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
|
|
|
temp = (float)sl_dcolor[i] * dimmer;
|
|
|
|
Settings.led_color[i] = (uint8_t)temp;
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
char* LightGetColor(char* scolor)
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
LightSetDimmer(Settings.led_dimmer);
|
2017-08-12 16:55:20 +01:00
|
|
|
scolor[0] = '\0';
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-08-12 16:55:20 +01:00
|
|
|
snprintf_P(scolor, 11, PSTR("%s%02X"), scolor, sl_dcolor[i]);
|
|
|
|
}
|
|
|
|
return scolor;
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightPreparePower()
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-08-12 16:55:20 +01:00
|
|
|
char scolor[11];
|
2017-10-08 15:51:05 +01:00
|
|
|
char scommand[16];
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if (Settings.led_dimmer && !(sl_power)) {
|
|
|
|
ExecuteCommandPower(devices_present, 7); // No publishPowerState
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (!Settings.led_dimmer && sl_power) {
|
|
|
|
ExecuteCommandPower(devices_present, 6); // No publishPowerState
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
#ifdef USE_DOMOTICZ
|
2017-10-18 17:22:34 +01:00
|
|
|
// MqttPublishDomoticzPowerState(1);
|
|
|
|
DomoticzUpdatePowerState(devices_present);
|
2017-07-15 14:07:30 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
2017-10-08 15:51:05 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
GetPowerDevice(scommand, devices_present, sizeof(scommand));
|
|
|
|
if ((light_type &7) > 1) {
|
2017-10-08 15:51:05 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\", \"" D_CMND_DIMMER "\":%d, \"" D_CMND_COLOR "\":\"%s\"}"),
|
2017-10-18 17:22:34 +01:00
|
|
|
scommand, GetStateText(sl_power), Settings.led_dimmer, LightGetColor(scolor));
|
2017-07-15 14:07:30 +01:00
|
|
|
} else {
|
2017-10-08 15:51:05 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\", \"" D_CMND_DIMMER "\":%d}"),
|
2017-10-18 17:22:34 +01:00
|
|
|
scommand, GetStateText(sl_power), Settings.led_dimmer);
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightSetPower(uint8_t mpower)
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
sl_power = mpower;
|
2017-07-03 10:45:15 +01:00
|
|
|
if (sl_wakeupActive) {
|
|
|
|
sl_wakeupActive--;
|
|
|
|
}
|
2017-09-18 17:06:46 +01:00
|
|
|
if (sl_power) {
|
|
|
|
sl_any = 1;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
LightAnimate();
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightAnimate()
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-03-05 14:07:30 +00:00
|
|
|
// {"Wakeup":"Done"}
|
2017-02-19 16:49:17 +00:00
|
|
|
uint8_t fadeValue;
|
2017-08-12 16:55:20 +01:00
|
|
|
uint8_t cur_col[5];
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-09-16 16:34:03 +01:00
|
|
|
stripTimerCntr++;
|
2017-09-16 17:19:15 +01:00
|
|
|
if (!sl_power) { // Power Off
|
2017-10-18 17:22:34 +01:00
|
|
|
sleep = Settings.sleep;
|
2017-09-16 16:34:03 +01:00
|
|
|
stripTimerCntr = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-08-12 16:55:20 +01:00
|
|
|
sl_tcolor[i] = 0;
|
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-09-16 16:34:03 +01:00
|
|
|
sleep = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
switch (Settings.led_scheme) {
|
2017-09-16 16:34:03 +01:00
|
|
|
case 0: // Power On
|
2017-10-18 17:22:34 +01:00
|
|
|
LightSetDimmer(Settings.led_dimmer); // Power On
|
|
|
|
if (0 == Settings.led_fade) {
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_tcolor[i] = sl_dcolor[i];
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if (sl_tcolor[i] != sl_dcolor[i]) {
|
|
|
|
if (sl_tcolor[i] < sl_dcolor[i]) {
|
2017-10-18 17:22:34 +01:00
|
|
|
sl_tcolor[i] += ((sl_dcolor[i] - sl_tcolor[i]) >> Settings.led_speed) +1;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
if (sl_tcolor[i] > sl_dcolor[i]) {
|
2017-10-18 17:22:34 +01:00
|
|
|
sl_tcolor[i] -= ((sl_tcolor[i] - sl_dcolor[i]) >> Settings.led_speed) +1;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
break;
|
|
|
|
case 1: // Power On using wake up duration
|
|
|
|
if (2 == sl_wakeupActive) {
|
|
|
|
sl_wakeupActive = 1;
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_tcolor[i] = 0;
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_wakeupCntr = 0;
|
|
|
|
sl_wakeupDimmer = 0;
|
|
|
|
}
|
|
|
|
sl_wakeupCntr++;
|
2017-10-18 17:22:34 +01:00
|
|
|
if (sl_wakeupCntr > ((Settings.led_wakeup * STATES) / Settings.led_dimmer)) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_wakeupCntr = 0;
|
|
|
|
sl_wakeupDimmer++;
|
2017-10-18 17:22:34 +01:00
|
|
|
if (sl_wakeupDimmer <= Settings.led_dimmer) {
|
|
|
|
LightSetDimmer(sl_wakeupDimmer);
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_tcolor[i] = sl_dcolor[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUP "\":\"" D_DONE "\"}"));
|
2017-10-18 17:22:34 +01:00
|
|
|
MqttPublishPrefixTopic_P(2, PSTR(D_CMND_WAKEUP));
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_wakeupActive = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_scheme = 0;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef USE_WS2812 // ************************************************************************
|
|
|
|
default:
|
2017-10-18 17:22:34 +01:00
|
|
|
if (11 == light_type) {
|
|
|
|
Ws2812ShowScheme(Settings.led_scheme -2);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
#endif // USE_WS2812 ************************************************************************
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((Settings.led_scheme < 2) || !sl_power) {
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if (sl_lcolor[i] != sl_tcolor[i]) {
|
|
|
|
sl_any = 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
if (sl_any) {
|
|
|
|
sl_any = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_lcolor[i] = sl_tcolor[i];
|
2017-10-18 17:22:34 +01:00
|
|
|
cur_col[i] = (Settings.led_table) ? ledTable[sl_lcolor[i]] : sl_lcolor[i];
|
|
|
|
if (light_type < 6) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if (pin[GPIO_PWM1 +i] < 99) {
|
2017-10-18 17:22:34 +01:00
|
|
|
uint16_t curcol = cur_col[i] * (Settings.pwm_range / 255);
|
2017-10-05 12:28:31 +01:00
|
|
|
// snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "Cur_Col%d %d, CurCol %d"), i, cur_col[i], curcol);
|
2017-10-18 17:22:34 +01:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
analogWrite(pin[GPIO_PWM1 +i], bitRead(pwm_inverted, i) ? Settings.pwm_range - curcol : curcol);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef USE_WS2812 // ************************************************************************
|
2017-10-18 17:22:34 +01:00
|
|
|
if (11 == light_type) {
|
|
|
|
Ws2812SetColor(0, cur_col[0], cur_col[1], cur_col[2]);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#endif // USE_ES2812 ************************************************************************
|
2017-10-18 17:22:34 +01:00
|
|
|
if (light_type > 11) {
|
|
|
|
LightMy92x1Duty(cur_col[0], cur_col[1], cur_col[2], cur_col[3], cur_col[4]);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 14:07:30 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Hue support
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-09-18 17:06:46 +01:00
|
|
|
float sl_Hue = 0.0;
|
|
|
|
float sl_Sat = 0.0;
|
|
|
|
float sl_Bri = 0.0;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightRgbToHsb()
|
2017-09-18 17:06:46 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
LightSetDimmer(Settings.led_dimmer);
|
2017-09-18 17:06:46 +01:00
|
|
|
|
|
|
|
// convert colors to float between (0.0 - 1.0)
|
|
|
|
float r = sl_dcolor[0] / 255.0f;
|
|
|
|
float g = sl_dcolor[1] / 255.0f;
|
|
|
|
float b = sl_dcolor[2] / 255.0f;
|
|
|
|
|
|
|
|
float max = (r > g && r > b) ? r : (g > b) ? g : b;
|
|
|
|
float min = (r < g && r < b) ? r : (g < b) ? g : b;
|
|
|
|
|
|
|
|
float d = max - min;
|
|
|
|
|
|
|
|
sl_Hue = 0.0;
|
|
|
|
sl_Bri = max;
|
|
|
|
sl_Sat = (0.0f == sl_Bri) ? 0 : (d / sl_Bri);
|
|
|
|
|
|
|
|
if (d != 0.0f)
|
|
|
|
{
|
|
|
|
if (r == max) {
|
|
|
|
sl_Hue = (g - b) / d + (g < b ? 6.0f : 0.0f);
|
|
|
|
} else if (g == max) {
|
|
|
|
sl_Hue = (b - r) / d + 2.0f;
|
|
|
|
} else {
|
|
|
|
sl_Hue = (r - g) / d + 4.0f;
|
|
|
|
}
|
|
|
|
sl_Hue /= 6.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightHsbToRgb()
|
2017-09-18 17:06:46 +01:00
|
|
|
{
|
|
|
|
float r;
|
|
|
|
float g;
|
|
|
|
float b;
|
|
|
|
|
|
|
|
float h = sl_Hue;
|
|
|
|
float s = sl_Sat;
|
|
|
|
float v = sl_Bri;
|
|
|
|
|
|
|
|
if (0.0f == sl_Sat) {
|
|
|
|
r = g = b = v; // achromatic or black
|
|
|
|
} else {
|
|
|
|
if (h < 0.0f) {
|
|
|
|
h += 1.0f;
|
|
|
|
}
|
|
|
|
else if (h >= 1.0f) {
|
|
|
|
h -= 1.0f;
|
|
|
|
}
|
|
|
|
h *= 6.0f;
|
|
|
|
int i = (int)h;
|
|
|
|
float f = h - i;
|
|
|
|
float q = v * (1.0f - s * f);
|
|
|
|
float p = v * (1.0f - s);
|
|
|
|
float t = v * (1.0f - s * (1.0f - f));
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
r = v;
|
|
|
|
g = t;
|
|
|
|
b = p;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
r = q;
|
|
|
|
g = v;
|
|
|
|
b = p;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
r = p;
|
|
|
|
g = v;
|
|
|
|
b = t;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
r = p;
|
|
|
|
g = q;
|
|
|
|
b = v;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
r = t;
|
|
|
|
g = p;
|
|
|
|
b = v;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = v;
|
|
|
|
g = p;
|
|
|
|
b = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sl_dcolor[0] = (uint8_t)(r * 255.0f);
|
|
|
|
sl_dcolor[1] = (uint8_t)(g * 255.0f);
|
|
|
|
sl_dcolor[2] = (uint8_t)(b * 255.0f);
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightReplaceHsb(String *response)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((light_type &7) > 2) {
|
|
|
|
LightRgbToHsb();
|
2017-09-18 17:06:46 +01:00
|
|
|
response->replace("{h}", String((uint16_t)(65535.0f * sl_Hue)));
|
|
|
|
response->replace("{s}", String((uint8_t)(254.0f * sl_Sat)));
|
|
|
|
response->replace("{b}", String((uint8_t)(254.0f * sl_Bri)));
|
2017-08-12 16:55:20 +01:00
|
|
|
} else {
|
|
|
|
response->replace("{h}", "0");
|
|
|
|
response->replace("{s}", "0");
|
2017-10-18 17:22:34 +01:00
|
|
|
// response->replace("{b}", String((uint8_t)(2.54f * (float)Settings.led_dimmer)));
|
|
|
|
response->replace("{b}", String((uint8_t)(0.01f * (float)Settings.led_dimmer)));
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightGetHsb(float *hue, float *sat, float *bri)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((light_type &7) > 2) {
|
|
|
|
LightRgbToHsb();
|
2017-09-18 17:06:46 +01:00
|
|
|
*hue = sl_Hue;
|
|
|
|
*sat = sl_Sat;
|
|
|
|
*bri = sl_Bri;
|
2017-08-12 16:55:20 +01:00
|
|
|
} else {
|
|
|
|
*hue = 0;
|
|
|
|
*sat = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
// *bri = (2.54f * (float)Settings.led_dimmer);
|
|
|
|
*bri = (0.01f * (float)Settings.led_dimmer);
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightSetHsb(float hue, float sat, float bri, uint16_t ct)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-08-13 14:31:49 +01:00
|
|
|
/*
|
2017-08-12 16:55:20 +01:00
|
|
|
char stemp1[10];
|
|
|
|
char stemp2[10];
|
|
|
|
char stemp3[10];
|
2017-09-02 13:37:02 +01:00
|
|
|
dtostrfi(hue, 3, stemp1);
|
|
|
|
dtostrfi(sat, 3, stemp2);
|
|
|
|
dtostrfi(bri, 3, stemp3);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR("HUE: Set Hue %s, Sat %s, Bri %s, Ct %d"), stemp1, stemp2, stemp3, ct);
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-08-13 14:31:49 +01:00
|
|
|
*/
|
2017-08-15 21:09:52 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if (light_type > 2) {
|
|
|
|
if ((5 == (light_type &7)) && (ct > 0)) {
|
|
|
|
LightSetColorTemp(ct);
|
2017-08-16 16:05:36 +01:00
|
|
|
} else {
|
2017-09-18 17:06:46 +01:00
|
|
|
sl_Hue = hue;
|
|
|
|
sl_Sat = sat;
|
|
|
|
sl_Bri = bri;
|
2017-10-18 17:22:34 +01:00
|
|
|
LightHsbToRgb();
|
|
|
|
LightSetColor();
|
2017-08-16 16:05:36 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
LightPreparePower();
|
|
|
|
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_COLOR));
|
2017-08-12 16:55:20 +01:00
|
|
|
} else {
|
|
|
|
uint8_t tmp = (uint8_t)(bri * 100);
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_dimmer = tmp;
|
|
|
|
if (2 == (light_type &7)) {
|
2017-08-15 21:09:52 +01:00
|
|
|
if (ct > 0) {
|
2017-10-18 17:22:34 +01:00
|
|
|
LightSetColorTemp(ct);
|
2017-08-15 21:09:52 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
LightPreparePower();
|
|
|
|
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_COLOR));
|
2017-08-15 21:09:52 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
LightPreparePower();
|
|
|
|
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_DIMMER));
|
2017-08-15 21:09:52 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2017-02-19 16:49:17 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-04-25 17:24:42 +01:00
|
|
|
boolean serviced = true;
|
|
|
|
boolean coldim = false;
|
2017-08-12 16:55:20 +01:00
|
|
|
char scolor[11];
|
|
|
|
char *p;
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if (((light_type &7) > 1) && !strcasecmp_P(type, PSTR(D_CMND_COLOR))) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if (dataBuf[0] == '#') {
|
|
|
|
dataBuf++;
|
2017-09-08 11:57:08 +01:00
|
|
|
data_len--;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((2 * (light_type &7)) == data_len) {
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
strlcpy(scolor, dataBuf + (i *2), 3);
|
2017-08-12 16:55:20 +01:00
|
|
|
sl_dcolor[i] = (uint8_t)strtol(scolor, &p, 16);
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
LightSetColor();
|
2017-03-12 17:36:33 +00:00
|
|
|
coldim = true;
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COLOR "\":\"%s\"}"), LightGetColor(scolor));
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
#ifdef USE_WS2812 // ***********************************************************************
|
2017-10-18 17:22:34 +01:00
|
|
|
else if ((11 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_LED)) && (index > 0) && (index <= Settings.led_pixels)) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if (dataBuf[0] == '#') {
|
|
|
|
dataBuf++;
|
|
|
|
data_len--;
|
|
|
|
}
|
2017-09-18 17:06:46 +01:00
|
|
|
uint8_t sl_ledcolor[3];
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((2 * (light_type &7)) == data_len) {
|
|
|
|
for (byte i = 0; i < (light_type &7); i++) {
|
2017-09-16 16:34:03 +01:00
|
|
|
strlcpy(scolor, dataBuf + (i *2), 3);
|
|
|
|
sl_ledcolor[i] = (uint8_t)strtol(scolor, &p, 16);
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
Ws2812SetColor(index, sl_ledcolor[0], sl_ledcolor[1], sl_ledcolor[2]);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LED "%d\":\"%s\"}"), index, Ws2812GetColor(index, scolor));
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if ((11 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_PIXELS))) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if ((payload > 0) && (payload <= WS2812_MAX_LEDS)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_pixels = payload;
|
|
|
|
Ws2812Clear();
|
2017-09-18 17:06:46 +01:00
|
|
|
sl_any = 1;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PIXELS "\":%d}"), Settings.led_pixels);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if ((11 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_WIDTH))) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if ((payload >= 0) && (payload <= 4)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_width = payload;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIDTH "\":%d}"), Settings.led_width);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if ((11 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_SCHEME))) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if ((payload >= 0) && (payload <= 9)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_scheme = payload;
|
|
|
|
if (1 == Settings.led_scheme) {
|
2017-09-16 16:34:03 +01:00
|
|
|
sl_wakeupActive = 3;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
ExecuteCommandPower(devices_present, 1);
|
2017-09-16 16:34:03 +01:00
|
|
|
stripTimerCntr = 0;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SCHEME "\":%d}"), Settings.led_scheme);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#endif // USE_WS2812 ************************************************************************
|
2017-09-18 17:06:46 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_WAKEUP))) {
|
|
|
|
if ((payload >= 0) && (payload <= 100)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_dimmer = payload;
|
2017-09-18 17:06:46 +01:00
|
|
|
}
|
|
|
|
sl_wakeupActive = 3;
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_scheme = 1;
|
|
|
|
ExecuteCommandPower(devices_present, 1);
|
2017-09-18 17:06:46 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUP "\":\"" D_STARTED "\"}"));
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_COLORTEMPERATURE)) && ((2 == (light_type &7)) || (5 == (light_type &7)))) { // ColorTemp
|
2017-08-16 16:05:36 +01:00
|
|
|
if ((payload >= 153) && (payload <= 500)) { // https://developers.meethue.com/documentation/core-concepts
|
2017-10-18 17:22:34 +01:00
|
|
|
LightSetColorTemp(payload);
|
2017-08-16 16:05:36 +01:00
|
|
|
coldim = true;
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COLORTEMPERATURE "\":%d}"), LightGetColorTemp());
|
2017-08-16 16:05:36 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_DIMMER))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 100)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_dimmer = payload;
|
2017-03-12 17:36:33 +00:00
|
|
|
coldim = true;
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DIMMER "\":%d}"), Settings.led_dimmer);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_LEDTABLE))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload >= 0) && (payload <= 2)) {
|
2017-02-19 16:49:17 +00:00
|
|
|
switch (payload) {
|
|
|
|
case 0: // Off
|
|
|
|
case 1: // On
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_table = payload;
|
2017-02-19 16:49:17 +00:00
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_table ^= 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
sl_any = 1;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LEDTABLE "\":\"%s\"}"), GetStateText(Settings.led_table));
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_FADE))) {
|
2017-06-06 22:23:23 +01:00
|
|
|
switch (payload) {
|
|
|
|
case 0: // Off
|
|
|
|
case 1: // On
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_fade = payload;
|
2017-06-06 22:23:23 +01:00
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_fade ^= 1;
|
2017-06-06 22:23:23 +01:00
|
|
|
break;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_FADE "\":\"%s\"}"), GetStateText(Settings.led_fade));
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_SPEED))) { // 1 - fast, 8 - slow
|
2017-06-06 22:23:23 +01:00
|
|
|
if ((payload > 0) && (payload <= 8)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_speed = payload;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SPEED "\":%d}"), Settings.led_speed);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_WAKEUPDURATION))) {
|
2017-07-30 16:55:37 +01:00
|
|
|
if ((payload > 0) && (payload < 3001)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.led_wakeup = payload;
|
2017-02-19 16:49:17 +00:00
|
|
|
sl_wakeupActive = 0;
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUPDURATION "\":%d}"), Settings.led_wakeup);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-09-08 16:14:39 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR("UNDOCA"))) { // Theos legacy status
|
2017-10-18 17:22:34 +01:00
|
|
|
LightGetColor(scolor);
|
2017-09-08 16:14:39 +01:00
|
|
|
scolor[6] = '\0'; // RGB only
|
2017-09-16 16:34:03 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, %d, %d, %d, %d, %d"),
|
2017-10-18 17:22:34 +01:00
|
|
|
scolor, Settings.led_fade, Settings.led_table, Settings.led_scheme, Settings.led_speed, Settings.led_width);
|
|
|
|
MqttPublishPrefixTopic_P(1, type);
|
2017-09-13 13:19:34 +01:00
|
|
|
mqtt_data[0] = '\0';
|
2017-09-08 16:14:39 +01:00
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
else {
|
|
|
|
serviced = false; // Unknown command
|
|
|
|
}
|
2017-03-12 17:36:33 +00:00
|
|
|
if (coldim) {
|
2017-10-18 17:22:34 +01:00
|
|
|
LightPreparePower();
|
2017-03-12 17:36:33 +00:00
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|