2017-02-19 16:49:17 +00:00
|
|
|
/*
|
2018-05-28 10:35:23 +01:00
|
|
|
xdrv_04_light.ino - PWM, WS2812 and sonoff led support for Sonoff-Tasmota
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-12-22 13:55:24 +00:00
|
|
|
Copyright (C) 2018 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-27 11:12:07 +01:00
|
|
|
* PWM, WS2812, Sonoff B1, AiLight, Sonoff Led and BN-SZ01, H801, MagicHome and Arilux
|
2017-09-02 13:37:02 +01:00
|
|
|
*
|
2017-10-27 11:12:07 +01:00
|
|
|
* light_type Module Color ColorTemp Modules
|
|
|
|
* ---------- --------- ----- --------- ----------------------------
|
|
|
|
* 1 PWM1 W no (Sonoff BN-SZ)
|
|
|
|
* 2 PWM2 CW yes (Sonoff Led)
|
2017-11-15 22:07:45 +00:00
|
|
|
* 3 PWM3 RGB no (H801, MagicHome and Arilux LC01)
|
2017-10-27 11:12:07 +01:00
|
|
|
* 4 PWM4 RGBW no (H801, MagicHome and Arilux)
|
2017-11-15 22:07:45 +00:00
|
|
|
* 5 PWM5 RGBCW yes (H801, Arilux LC11)
|
2017-10-27 11:12:07 +01:00
|
|
|
* 9 reserved no
|
|
|
|
* 10 reserved yes
|
2017-11-11 15:02:18 +00:00
|
|
|
* 11 +WS2812 RGB(W) no (One WS2812 RGB or RGBW ledstrip)
|
2017-10-27 11:12:07 +01:00
|
|
|
* 12 AiLight RGBW no
|
|
|
|
* 13 Sonoff B1 RGBCW yes
|
2017-09-16 16:34:03 +01:00
|
|
|
*
|
2017-10-27 11:12:07 +01:00
|
|
|
* light_scheme WS2812 3+ Colors 1+2 Colors Effect
|
|
|
|
* ------------ ------ --------- ---------- -----------------
|
|
|
|
* 0 yes yes yes Color On/Off
|
|
|
|
* 1 yes yes yes Wakeup light
|
|
|
|
* 2 yes yes no Color cycle RGB
|
|
|
|
* 3 yes yes no Color cycle RBG
|
|
|
|
* 4 yes yes no Random RGB colors
|
|
|
|
* 5 yes no no Clock
|
|
|
|
* 6 yes no no Incandescent
|
|
|
|
* 7 yes no no RGB
|
|
|
|
* 8 yes no no Christmas
|
|
|
|
* 9 yes no no Hanukkah
|
|
|
|
* 10 yes no no Kwanzaa
|
|
|
|
* 11 yes no no Rainbow
|
|
|
|
* 12 yes no no Fire
|
2017-09-16 16:34:03 +01:00
|
|
|
*
|
2017-02-19 16:49:17 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-12-29 13:55:39 +00:00
|
|
|
#define WS2812_SCHEMES 7 // Number of additional WS2812 schemes supported by xdrv_ws2812.ino
|
|
|
|
|
2017-10-29 17:18:46 +00:00
|
|
|
enum LightCommands {
|
|
|
|
CMND_COLOR, CMND_COLORTEMPERATURE, CMND_DIMMER, CMND_LED, CMND_LEDTABLE, CMND_FADE,
|
2018-09-29 15:55:53 +01:00
|
|
|
CMND_PIXELS, CMND_RGBWWTABLE, CMND_ROTATION, CMND_SCHEME, CMND_SPEED, CMND_WAKEUP, CMND_WAKEUPDURATION,
|
2018-03-20 15:28:18 +00:00
|
|
|
CMND_WIDTH, CMND_CHANNEL, CMND_HSBCOLOR, CMND_UNDOCA };
|
2017-10-29 17:18:46 +00:00
|
|
|
const char kLightCommands[] PROGMEM =
|
|
|
|
D_CMND_COLOR "|" D_CMND_COLORTEMPERATURE "|" D_CMND_DIMMER "|" D_CMND_LED "|" D_CMND_LEDTABLE "|" D_CMND_FADE "|"
|
2018-09-29 15:55:53 +01:00
|
|
|
D_CMND_PIXELS "|" D_CMND_RGBWWTABLE "|" D_CMND_ROTATION "|" D_CMND_SCHEME "|" D_CMND_SPEED "|" D_CMND_WAKEUP "|" D_CMND_WAKEUPDURATION "|"
|
2018-03-20 15:28:18 +00:00
|
|
|
D_CMND_WIDTH "|" D_CMND_CHANNEL "|" D_CMND_HSBCOLOR "|UNDOCA" ;
|
2017-10-29 17:18:46 +00:00
|
|
|
|
2017-11-15 22:07:45 +00:00
|
|
|
struct LRgbColor {
|
|
|
|
uint8_t R, G, B;
|
|
|
|
};
|
|
|
|
#define MAX_FIXED_COLOR 12
|
|
|
|
const LRgbColor kFixedColor[MAX_FIXED_COLOR] PROGMEM =
|
|
|
|
{ 255,0,0, 0,255,0, 0,0,255, 228,32,0, 0,228,32, 0,32,228, 188,64,0, 0,160,96, 160,32,240, 255,255,0, 255,0,170, 255,255,255 };
|
|
|
|
|
2018-09-04 21:43:27 +01:00
|
|
|
struct LWColor {
|
|
|
|
uint8_t W;
|
|
|
|
};
|
2018-09-04 22:03:19 +01:00
|
|
|
#define MAX_FIXED_WHITE 4
|
|
|
|
const LWColor kFixedWhite[MAX_FIXED_WHITE] PROGMEM = { 0, 255, 128, 32 };
|
2018-09-04 21:43:27 +01:00
|
|
|
|
2017-11-15 22:07:45 +00:00
|
|
|
struct LCwColor {
|
|
|
|
uint8_t C, W;
|
|
|
|
};
|
|
|
|
#define MAX_FIXED_COLD_WARM 4
|
|
|
|
const LCwColor kFixedColdWarm[MAX_FIXED_COLD_WARM] PROGMEM = { 0,0, 255,0, 0,255, 128,128 };
|
|
|
|
|
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-10-23 11:18:15 +01:00
|
|
|
uint8_t light_entry_color[5];
|
|
|
|
uint8_t light_current_color[5];
|
|
|
|
uint8_t light_new_color[5];
|
|
|
|
uint8_t light_last_color[5];
|
2017-12-27 13:10:55 +00:00
|
|
|
uint8_t light_signal_color[5];
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-10-25 13:27:30 +01:00
|
|
|
uint8_t light_wheel = 0;
|
2017-10-23 11:18:15 +01:00
|
|
|
uint8_t light_subtype = 0;
|
2018-01-20 11:12:39 +00:00
|
|
|
uint8_t light_device = 0;
|
2017-10-23 11:18:15 +01:00
|
|
|
uint8_t light_power = 0;
|
|
|
|
uint8_t light_update = 1;
|
|
|
|
uint8_t light_wakeup_active = 0;
|
|
|
|
uint8_t light_wakeup_dimmer = 0;
|
|
|
|
uint16_t light_wakeup_counter = 0;
|
2017-02-19 16:49:17 +00:00
|
|
|
|
2017-11-17 16:52:31 +00:00
|
|
|
uint8_t light_fixed_color_index = 1;
|
|
|
|
|
2018-02-07 16:50:02 +00:00
|
|
|
unsigned long strip_timer_counter = 0; // Bars and Gradient
|
2017-09-16 16:34:03 +01:00
|
|
|
|
2017-11-15 22:07:45 +00:00
|
|
|
#ifdef USE_ARILUX_RF
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Arilux LC11 Rf support stripped from RCSwitch library
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define ARILUX_RF_TIME_AVOID_DUPLICATE 1000 // Milliseconds
|
|
|
|
|
|
|
|
#define ARILUX_RF_MAX_CHANGES 51 // Pulses (sync + 2 x 24 bits)
|
|
|
|
#define ARILUX_RF_SEPARATION_LIMIT 4300 // Microseconds
|
|
|
|
#define ARILUX_RF_RECEIVE_TOLERANCE 60 // Percentage
|
|
|
|
|
|
|
|
unsigned int arilux_rf_timings[ARILUX_RF_MAX_CHANGES];
|
|
|
|
|
2017-11-17 16:52:31 +00:00
|
|
|
unsigned long arilux_rf_received_value = 0;
|
|
|
|
unsigned long arilux_rf_last_received_value = 0;
|
|
|
|
unsigned long arilux_rf_last_time = 0;
|
2017-11-15 22:07:45 +00:00
|
|
|
unsigned long arilux_rf_lasttime = 0;
|
2017-11-17 16:52:31 +00:00
|
|
|
|
2017-11-15 22:07:45 +00:00
|
|
|
unsigned int arilux_rf_change_count = 0;
|
|
|
|
unsigned int arilux_rf_repeat_count = 0;
|
|
|
|
|
|
|
|
uint8_t arilux_rf_toggle = 0;
|
|
|
|
|
2018-02-07 16:50:02 +00:00
|
|
|
|
|
|
|
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0
|
2017-11-15 22:07:45 +00:00
|
|
|
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves RF misses
|
2018-02-07 16:50:02 +00:00
|
|
|
void AriluxRfInterrupt() ICACHE_RAM_ATTR; // As iram is tight and it works this way too
|
2017-11-15 22:07:45 +00:00
|
|
|
#endif // USE_WS2812_DMA
|
2018-02-07 16:50:02 +00:00
|
|
|
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
|
2017-11-15 22:07:45 +00:00
|
|
|
|
|
|
|
void AriluxRfInterrupt()
|
|
|
|
{
|
|
|
|
unsigned long time = micros();
|
|
|
|
unsigned int duration = time - arilux_rf_lasttime;
|
|
|
|
|
|
|
|
if (duration > ARILUX_RF_SEPARATION_LIMIT) {
|
|
|
|
if (abs(duration - arilux_rf_timings[0]) < 200) {
|
|
|
|
arilux_rf_repeat_count++;
|
|
|
|
if (arilux_rf_repeat_count == 2) {
|
|
|
|
unsigned long code = 0;
|
|
|
|
const unsigned int delay = arilux_rf_timings[0] / 31;
|
|
|
|
const unsigned int delayTolerance = delay * ARILUX_RF_RECEIVE_TOLERANCE / 100;
|
|
|
|
for (unsigned int i = 1; i < arilux_rf_change_count -1; i += 2) {
|
|
|
|
code <<= 1;
|
2017-11-17 16:52:31 +00:00
|
|
|
if (abs(arilux_rf_timings[i] - (delay *3)) < delayTolerance && abs(arilux_rf_timings[i +1] - delay) < delayTolerance) {
|
2017-11-15 22:07:45 +00:00
|
|
|
code |= 1;
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 16:52:31 +00:00
|
|
|
if (arilux_rf_change_count > 49) { // Need 1 sync bit and 24 data bits
|
2017-11-15 22:07:45 +00:00
|
|
|
arilux_rf_received_value = code;
|
|
|
|
}
|
|
|
|
arilux_rf_repeat_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arilux_rf_change_count = 0;
|
|
|
|
}
|
|
|
|
if (arilux_rf_change_count >= ARILUX_RF_MAX_CHANGES) {
|
|
|
|
arilux_rf_change_count = 0;
|
|
|
|
arilux_rf_repeat_count = 0;
|
|
|
|
}
|
|
|
|
arilux_rf_timings[arilux_rf_change_count++] = duration;
|
|
|
|
arilux_rf_lasttime = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AriluxRfHandler()
|
|
|
|
{
|
|
|
|
unsigned long now = millis();
|
|
|
|
if (arilux_rf_received_value && !((arilux_rf_received_value == arilux_rf_last_received_value) && (now - arilux_rf_last_time < ARILUX_RF_TIME_AVOID_DUPLICATE))) {
|
|
|
|
arilux_rf_last_received_value = arilux_rf_received_value;
|
|
|
|
arilux_rf_last_time = now;
|
|
|
|
|
|
|
|
uint16_t hostcode = arilux_rf_received_value >> 8 & 0xFFFF;
|
|
|
|
if (Settings.rf_code[1][6] == Settings.rf_code[1][7]) {
|
|
|
|
Settings.rf_code[1][6] = hostcode >> 8 & 0xFF;
|
|
|
|
Settings.rf_code[1][7] = hostcode & 0xFF;
|
|
|
|
}
|
|
|
|
uint16_t stored_hostcode = Settings.rf_code[1][6] << 8 | Settings.rf_code[1][7];
|
|
|
|
|
2017-11-17 16:52:31 +00:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_RFR D_HOST D_CODE " 0x%04X, " D_RECEIVED " 0x%06X"), stored_hostcode, arilux_rf_received_value);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-11-15 22:07:45 +00:00
|
|
|
|
2017-11-17 16:52:31 +00:00
|
|
|
if (hostcode == stored_hostcode) {
|
2018-01-13 14:53:02 +00:00
|
|
|
char command[33];
|
2017-11-17 16:52:31 +00:00
|
|
|
char value = '-';
|
2017-11-15 22:07:45 +00:00
|
|
|
command[0] = '\0';
|
|
|
|
uint8_t keycode = arilux_rf_received_value & 0xFF;
|
|
|
|
switch (keycode) {
|
|
|
|
case 1: // Power On
|
|
|
|
case 3: // Power Off
|
|
|
|
snprintf_P(command, sizeof(command), PSTR(D_CMND_POWER " %d"), (1 == keycode) ? 1 : 0);
|
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
|
|
|
arilux_rf_toggle++;
|
|
|
|
arilux_rf_toggle &= 0x3;
|
|
|
|
snprintf_P(command, sizeof(command), PSTR(D_CMND_COLOR " %d"), 200 + arilux_rf_toggle);
|
|
|
|
break;
|
|
|
|
case 4: // Speed +
|
|
|
|
value = '+';
|
|
|
|
case 7: // Speed -
|
|
|
|
snprintf_P(command, sizeof(command), PSTR(D_CMND_SPEED " %c"), value);
|
|
|
|
break;
|
|
|
|
case 5: // Scheme +
|
|
|
|
value = '+';
|
|
|
|
case 8: // Scheme -
|
|
|
|
snprintf_P(command, sizeof(command), PSTR(D_CMND_SCHEME " %c"), value);
|
|
|
|
break;
|
|
|
|
case 6: // Dimmer +
|
|
|
|
value = '+';
|
|
|
|
case 9: // Dimmer -
|
|
|
|
snprintf_P(command, sizeof(command), PSTR(D_CMND_DIMMER " %c"), value);
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
if ((keycode >= 10) && (keycode <= 21)) {
|
|
|
|
snprintf_P(command, sizeof(command), PSTR(D_CMND_COLOR " %d"), keycode -9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strlen(command)) {
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommand(command, SRC_LIGHT);
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arilux_rf_received_value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AriluxRfInit()
|
|
|
|
{
|
2017-11-21 15:06:51 +00:00
|
|
|
if ((pin[GPIO_ARIRFRCV] < 99) && (pin[GPIO_LED2] < 99)) {
|
2017-11-15 22:07:45 +00:00
|
|
|
if (Settings.last_module != Settings.module) {
|
|
|
|
Settings.rf_code[1][6] = 0;
|
|
|
|
Settings.rf_code[1][7] = 0;
|
|
|
|
Settings.last_module = Settings.module;
|
|
|
|
}
|
|
|
|
arilux_rf_received_value = 0;
|
|
|
|
digitalWrite(pin[GPIO_LED2], !bitRead(led_inverted, 1)); // Turn on RF
|
2017-11-21 15:06:51 +00:00
|
|
|
attachInterrupt(pin[GPIO_ARIRFRCV], AriluxRfInterrupt, CHANGE);
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 16:31:50 +00:00
|
|
|
|
|
|
|
void AriluxRfDisable()
|
|
|
|
{
|
|
|
|
if ((pin[GPIO_ARIRFRCV] < 99) && (pin[GPIO_LED2] < 99)) {
|
|
|
|
detachInterrupt(pin[GPIO_ARIRFRCV]);
|
2018-01-18 15:19:28 +00:00
|
|
|
digitalWrite(pin[GPIO_LED2], bitRead(led_inverted, 1)); // Turn off RF
|
2017-11-24 16:31:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 22:07:45 +00:00
|
|
|
#endif // USE_ARILUX_RF
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
uint8_t light_pdi_pin;
|
|
|
|
uint8_t light_pdcki_pin;
|
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++) {
|
2017-10-23 11:18:15 +01:00
|
|
|
digitalWrite(light_pdi_pin, HIGH);
|
|
|
|
digitalWrite(light_pdi_pin, 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++) {
|
2017-10-23 11:18:15 +01:00
|
|
|
digitalWrite(light_pdcki_pin, HIGH);
|
|
|
|
digitalWrite(light_pdcki_pin, 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-10-23 11:18:15 +01:00
|
|
|
for (uint8_t i = 0; i < 4; i++) { // Send 8bit Data
|
|
|
|
digitalWrite(light_pdcki_pin, LOW);
|
|
|
|
digitalWrite(light_pdi_pin, (data & 0x80));
|
|
|
|
digitalWrite(light_pdcki_pin, HIGH);
|
2017-08-16 16:05:36 +01:00
|
|
|
data = data << 1;
|
2017-10-23 11:18:15 +01:00
|
|
|
digitalWrite(light_pdi_pin, (data & 0x80));
|
|
|
|
digitalWrite(light_pdcki_pin, LOW);
|
|
|
|
digitalWrite(light_pdi_pin, LOW);
|
2017-08-16 16:05:36 +01:00
|
|
|
data = data << 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightMy92x1Init()
|
2017-08-16 16:05:36 +01:00
|
|
|
{
|
2017-10-23 11:18:15 +01:00
|
|
|
uint8_t chips = light_type -11; // 1 (AiLight) or 2 (Sonoff B1)
|
2017-08-12 16:55:20 +01:00
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
LightDckiPulse(chips * 32); // Clear all duty register
|
|
|
|
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-10-23 11:18:15 +01:00
|
|
|
os_delay_us(12); // Delay >12us, begin send CMD data
|
|
|
|
for (uint8_t n = 0; n < chips; n++) { // Send CMD data
|
|
|
|
LightMy92x1Write(0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +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-10-23 11:18:15 +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-10-23 11:18:15 +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
|
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
os_delay_us(12); // TStop > 12us.
|
2017-08-16 16:05:36 +01:00
|
|
|
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-10-23 11:18:15 +01:00
|
|
|
os_delay_us(12); // TStart > 12us. Ready for send DI pulse.
|
|
|
|
LightDiPulse(8); // Send 8 DI pulse. After 8 pulse falling edge, store old data.
|
|
|
|
os_delay_us(12); // TStop > 12us.
|
2017-08-13 14:31:49 +01:00
|
|
|
}
|
|
|
|
|
2017-08-12 16:55:20 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-01-05 11:26:19 +00:00
|
|
|
void LightInit()
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-10-25 13:27:30 +01:00
|
|
|
uint8_t max_scheme = LS_MAX -1;
|
|
|
|
|
2018-01-20 11:12:39 +00:00
|
|
|
light_device = devices_present;
|
2017-11-11 15:02:18 +00:00
|
|
|
light_subtype = light_type &7;
|
|
|
|
|
2017-10-25 13:27:30 +01:00
|
|
|
if (light_type < LT_PWM6) { // PWM
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < light_type; i++) {
|
2017-10-23 11:18:15 +01:00
|
|
|
Settings.pwm_value[i] = 0; // Disable direct PWM control
|
2017-12-16 19:11:12 +00:00
|
|
|
if (pin[GPIO_PWM1 +i] < 99) {
|
|
|
|
pinMode(pin[GPIO_PWM1 +i], OUTPUT);
|
|
|
|
}
|
2017-10-05 12:28:31 +01:00
|
|
|
}
|
2017-10-25 13:27:30 +01:00
|
|
|
if (LT_PWM1 == light_type) {
|
2017-11-08 10:57:16 +00:00
|
|
|
Settings.light_color[0] = 255; // One PWM channel only supports Dimmer but needs max color
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +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-11-21 15:06:51 +00:00
|
|
|
if (pin[GPIO_ARIRFRCV] < 99) {
|
2017-11-15 22:07:45 +00:00
|
|
|
if (pin[GPIO_LED2] < 99) {
|
|
|
|
digitalWrite(pin[GPIO_LED2], bitRead(led_inverted, 1)); // Turn off RF
|
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#ifdef USE_WS2812 // ************************************************************************
|
2017-10-25 13:27:30 +01:00
|
|
|
else if (LT_WS2812 == light_type) {
|
2018-03-17 14:22:29 +00:00
|
|
|
#if (USE_WS2812_CTYPE > NEO_3LED)
|
2017-11-11 15:02:18 +00:00
|
|
|
light_subtype++; // from RGB to RGBW
|
|
|
|
#endif
|
2017-10-18 17:22:34 +01:00
|
|
|
Ws2812Init();
|
2017-12-29 13:55:39 +00:00
|
|
|
max_scheme = LS_MAX + WS2812_SCHEMES;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#endif // USE_WS2812 ************************************************************************
|
|
|
|
else {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_pdi_pin = pin[GPIO_DI];
|
|
|
|
light_pdcki_pin = pin[GPIO_DCKI];
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
pinMode(light_pdi_pin, OUTPUT);
|
|
|
|
pinMode(light_pdcki_pin, OUTPUT);
|
|
|
|
digitalWrite(light_pdi_pin, LOW);
|
|
|
|
digitalWrite(light_pdcki_pin, LOW);
|
2017-08-12 16:55:20 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
LightMy92x1Init();
|
2017-10-25 13:27:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-26 15:33:33 +01:00
|
|
|
if (light_subtype < LST_RGB) {
|
|
|
|
max_scheme = LS_POWER;
|
|
|
|
}
|
|
|
|
if ((LS_WAKEUP == Settings.light_scheme) || (Settings.light_scheme > max_scheme)) {
|
|
|
|
Settings.light_scheme = LS_POWER;
|
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_power = 0;
|
|
|
|
light_update = 1;
|
|
|
|
light_wakeup_active = 0;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2018-09-13 15:45:01 +01:00
|
|
|
if (PHILIPS == Settings.module) {
|
|
|
|
// Xiaomi Philips bulbs follow a different scheme:
|
|
|
|
// channel 0=intensity, channel2=temperature
|
|
|
|
Settings.light_color[1] = (uint8_t)icold;
|
|
|
|
} else
|
2017-10-25 13:27:30 +01:00
|
|
|
if (LST_RGBWC == light_subtype) {
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_color[0] = 0;
|
|
|
|
Settings.light_color[1] = 0;
|
|
|
|
Settings.light_color[2] = 0;
|
|
|
|
Settings.light_color[3] = (uint8_t)icold;
|
|
|
|
Settings.light_color[4] = (uint8_t)iwarm;
|
2017-08-16 16:05:36 +01:00
|
|
|
} else {
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_color[0] = (uint8_t)icold;
|
|
|
|
Settings.light_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-25 13:27:30 +01:00
|
|
|
if (LST_RGBWC == light_subtype) {
|
2017-08-16 16:05:36 +01:00
|
|
|
ct_idx = 3;
|
|
|
|
}
|
2017-10-26 15:33:33 +01:00
|
|
|
uint16_t my_ct = Settings.light_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-26 15:33:33 +01:00
|
|
|
my_ct = Settings.light_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
|
|
|
|
2018-09-13 15:45:01 +01:00
|
|
|
if (PHILIPS == Settings.module) {
|
|
|
|
// Xiaomi Philips bulbs use two PWM channels with a different scheme:
|
|
|
|
float dimmer = 100 / (float)myDimmer;
|
|
|
|
temp = (float)Settings.light_color[0] / dimmer; // channel 1 is intensity
|
|
|
|
light_current_color[0] = (uint8_t)temp;
|
|
|
|
temp = (float)Settings.light_color[1]; // channel 2 is temperature
|
|
|
|
light_current_color[1] = (uint8_t)temp;
|
|
|
|
return;
|
|
|
|
}
|
2017-11-08 10:57:16 +00:00
|
|
|
if (LT_PWM1 == light_type) {
|
|
|
|
Settings.light_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
|
|
|
float dimmer = 100 / (float)myDimmer;
|
2017-10-23 11:18:15 +01:00
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
2017-12-27 13:10:55 +00:00
|
|
|
if (Settings.flag.light_signal) {
|
|
|
|
temp = (float)light_signal_color[i] / dimmer;
|
|
|
|
} else {
|
|
|
|
temp = (float)Settings.light_color[i] / dimmer;
|
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_current_color[i] = (uint8_t)temp;
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightSetColor()
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
|
|
|
uint8_t highest = 0;
|
2017-07-15 14:07:30 +01:00
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
if (highest < light_current_color[i]) {
|
|
|
|
highest = light_current_color[i];
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
float mDim = (float)highest / 2.55;
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_dimmer = (uint8_t)mDim;
|
2017-10-18 17:22:34 +01:00
|
|
|
float dimmer = 100 / mDim;
|
2017-10-23 11:18:15 +01:00
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
2017-10-25 13:27:30 +01:00
|
|
|
float temp = (float)light_current_color[i] * dimmer;
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_color[i] = (uint8_t)temp;
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 13:10:55 +00:00
|
|
|
void LightSetSignal(uint16_t lo, uint16_t hi, uint16_t value)
|
|
|
|
{
|
|
|
|
/* lo - below lo is green
|
|
|
|
hi - above hi is red
|
|
|
|
*/
|
|
|
|
if (Settings.flag.light_signal) {
|
|
|
|
uint16_t signal = 0;
|
|
|
|
if (value > lo) {
|
|
|
|
signal = (value - lo) * 10 / ((hi - lo) * 10 / 256);
|
|
|
|
if (signal > 255) {
|
|
|
|
signal = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "Light signal %d"), signal);
|
|
|
|
// AddLog(LOG_LEVEL_DEBUG);
|
|
|
|
light_signal_color[0] = signal;
|
|
|
|
light_signal_color[1] = 255 - signal;
|
|
|
|
light_signal_color[2] = 0;
|
|
|
|
light_signal_color[3] = 0;
|
|
|
|
light_signal_color[4] = 0;
|
|
|
|
|
|
|
|
Settings.light_scheme = 0;
|
|
|
|
if (!Settings.light_dimmer) {
|
|
|
|
Settings.light_dimmer = 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
char* LightGetColor(uint8_t type, char* scolor)
|
2017-08-12 16:55:20 +01:00
|
|
|
{
|
2017-10-26 15:33:33 +01:00
|
|
|
LightSetDimmer(Settings.light_dimmer);
|
2017-08-12 16:55:20 +01:00
|
|
|
scolor[0] = '\0';
|
2017-10-23 11:18:15 +01:00
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
if (!type && Settings.flag.decimal_text) {
|
|
|
|
snprintf_P(scolor, 25, PSTR("%s%s%d"), scolor, (i > 0) ? "," : "", light_current_color[i]);
|
|
|
|
} else {
|
|
|
|
snprintf_P(scolor, 25, PSTR("%s%02X"), scolor, light_current_color[i]);
|
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
|
|
|
return scolor;
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-25 13:27:30 +01:00
|
|
|
void LightPowerOn()
|
|
|
|
{
|
2017-10-26 15:33:33 +01:00
|
|
|
if (Settings.light_dimmer && !(light_power)) {
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(light_device, POWER_ON, SRC_LIGHT);
|
2017-10-25 13:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 11:12:39 +00:00
|
|
|
void LightState(uint8_t append)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-10-23 11:18:15 +01:00
|
|
|
char scolor[25];
|
2018-01-13 14:53:02 +00:00
|
|
|
char scommand[33];
|
2018-03-20 15:28:18 +00:00
|
|
|
float hsb[3];
|
|
|
|
int16_t h,s,b;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2018-01-20 11:12:39 +00:00
|
|
|
if (append) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,"), mqtt_data);
|
|
|
|
} else {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{"));
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2018-05-13 16:38:44 +01:00
|
|
|
GetPowerDevice(scommand, light_device, sizeof(scommand), Settings.flag.device_index_enable);
|
2018-01-20 11:12:39 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"%s\":\"%s\",\"" D_CMND_DIMMER "\":%d"),
|
|
|
|
mqtt_data, scommand, GetStateText(light_power), Settings.light_dimmer);
|
2017-10-25 13:27:30 +01:00
|
|
|
if (light_subtype > LST_SINGLE) {
|
2018-01-18 15:19:28 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_COLOR "\":\"%s\""), mqtt_data, LightGetColor(0, scolor));
|
2018-03-21 08:56:39 +00:00
|
|
|
// Add status for HSB
|
2018-09-08 07:49:08 +01:00
|
|
|
LightGetHsb(&hsb[0],&hsb[1],&hsb[2], false);
|
|
|
|
// Scale these percentages up to the numbers expected by the client
|
2018-03-20 15:28:18 +00:00
|
|
|
h = round(hsb[0] * 360);
|
|
|
|
s = round(hsb[1] * 100);
|
|
|
|
b = round(hsb[2] * 100);
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_HSBCOLOR "\":\"%d,%d,%d\""), mqtt_data, h,s,b);
|
2018-03-21 08:56:39 +00:00
|
|
|
// Add status for each channel
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_CHANNEL "\":[" ), mqtt_data);
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s%d" ), mqtt_data, (i > 0 ? "," : ""), round(light_current_color[i]/2.55));
|
|
|
|
}
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s]" ), mqtt_data);
|
2018-01-18 15:19:28 +00:00
|
|
|
}
|
|
|
|
if ((LST_COLDWARM == light_subtype) || (LST_RGBWC == light_subtype)) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_COLORTEMPERATURE "\":%d"), mqtt_data, LightGetColorTemp());
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
2018-01-20 11:12:39 +00:00
|
|
|
if (append) {
|
|
|
|
if (light_subtype >= LST_RGB) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_SCHEME "\":%d"), mqtt_data, Settings.light_scheme);
|
|
|
|
}
|
|
|
|
if (LT_WS2812 == light_type) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_WIDTH "\":%d"), mqtt_data, Settings.light_width);
|
|
|
|
}
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_CMND_FADE "\":\"%s\",\"" D_CMND_SPEED "\":%d,\"" D_CMND_LEDTABLE "\":\"%s\""),
|
|
|
|
mqtt_data, GetStateText(Settings.light_fade), Settings.light_speed, GetStateText(Settings.light_correction));
|
|
|
|
} else {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightPreparePower()
|
|
|
|
{
|
|
|
|
if (Settings.light_dimmer && !(light_power)) {
|
2018-01-27 16:52:48 +00:00
|
|
|
if (!Settings.flag.not_power_linked) {
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(light_device, POWER_ON_NO_STATE, SRC_LIGHT);
|
2018-01-27 16:52:48 +00:00
|
|
|
}
|
2018-01-20 11:12:39 +00:00
|
|
|
}
|
|
|
|
else if (!Settings.light_dimmer && light_power) {
|
2018-05-28 14:52:42 +01:00
|
|
|
ExecuteCommandPower(light_device, POWER_OFF_NO_STATE, SRC_LIGHT);
|
2018-01-20 11:12:39 +00:00
|
|
|
}
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
DomoticzUpdatePowerState(light_device);
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
|
|
|
|
LightState(0);
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-25 13:27:30 +01:00
|
|
|
void LightFade()
|
|
|
|
{
|
2017-10-26 15:33:33 +01:00
|
|
|
if (0 == Settings.light_fade) {
|
2017-10-25 13:27:30 +01:00
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
light_new_color[i] = light_current_color[i];
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-26 15:33:33 +01:00
|
|
|
uint8_t shift = Settings.light_speed;
|
|
|
|
if (Settings.light_speed > 6) {
|
|
|
|
shift = (strip_timer_counter % (Settings.light_speed -6)) ? 0 : 8;
|
2017-10-25 13:27:30 +01:00
|
|
|
}
|
|
|
|
if (shift) {
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
if (light_new_color[i] != light_current_color[i]) {
|
|
|
|
if (light_new_color[i] < light_current_color[i]) {
|
|
|
|
light_new_color[i] += ((light_current_color[i] - light_new_color[i]) >> shift) +1;
|
|
|
|
}
|
|
|
|
if (light_new_color[i] > light_current_color[i]) {
|
|
|
|
light_new_color[i] -= ((light_new_color[i] - light_current_color[i]) >> shift) +1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightWheel(uint8_t wheel_pos)
|
|
|
|
{
|
|
|
|
wheel_pos = 255 - wheel_pos;
|
|
|
|
if (wheel_pos < 85) {
|
|
|
|
light_entry_color[0] = 255 - wheel_pos * 3;
|
|
|
|
light_entry_color[1] = 0;
|
|
|
|
light_entry_color[2] = wheel_pos * 3;
|
|
|
|
} else if (wheel_pos < 170) {
|
|
|
|
wheel_pos -= 85;
|
|
|
|
light_entry_color[0] = 0;
|
|
|
|
light_entry_color[1] = wheel_pos * 3;
|
|
|
|
light_entry_color[2] = 255 - wheel_pos * 3;
|
|
|
|
} else {
|
|
|
|
wheel_pos -= 170;
|
|
|
|
light_entry_color[0] = wheel_pos * 3;
|
|
|
|
light_entry_color[1] = 255 - wheel_pos * 3;
|
|
|
|
light_entry_color[2] = 0;
|
|
|
|
}
|
|
|
|
light_entry_color[3] = 0;
|
|
|
|
light_entry_color[4] = 0;
|
2017-10-26 15:33:33 +01:00
|
|
|
float dimmer = 100 / (float)Settings.light_dimmer;
|
2017-10-25 13:27:30 +01:00
|
|
|
for (byte i = 0; i < LST_RGB; i++) {
|
|
|
|
float temp = (float)light_entry_color[i] / dimmer;
|
|
|
|
light_entry_color[i] = (uint8_t)temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightCycleColor(int8_t direction)
|
|
|
|
{
|
2017-10-26 15:33:33 +01:00
|
|
|
if (strip_timer_counter % (Settings.light_speed * 2)) {
|
2017-10-25 13:27:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
light_wheel += direction;
|
|
|
|
LightWheel(light_wheel);
|
|
|
|
memcpy(light_new_color, light_entry_color, sizeof(light_new_color));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightRandomColor()
|
|
|
|
{
|
|
|
|
uint8_t light_update = 0;
|
|
|
|
for (byte i = 0; i < LST_RGB; i++) {
|
|
|
|
if (light_new_color[i] != light_current_color[i]) {
|
|
|
|
light_update = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!light_update) {
|
|
|
|
light_wheel = random(255);
|
|
|
|
LightWheel(light_wheel);
|
|
|
|
memcpy(light_current_color, light_entry_color, sizeof(light_current_color));
|
|
|
|
}
|
|
|
|
LightFade();
|
|
|
|
}
|
|
|
|
|
2018-01-05 11:26:19 +00:00
|
|
|
void LightSetPower()
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2018-01-20 11:12:39 +00:00
|
|
|
// light_power = XdrvMailbox.index;
|
|
|
|
light_power = bitRead(XdrvMailbox.index, light_device -1);
|
2017-10-23 11:18:15 +01:00
|
|
|
if (light_wakeup_active) {
|
|
|
|
light_wakeup_active--;
|
2017-07-03 10:45:15 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
if (light_power) {
|
|
|
|
light_update = 1;
|
2017-09-18 17:06:46 +01:00
|
|
|
}
|
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-08-12 16:55:20 +01:00
|
|
|
uint8_t cur_col[5];
|
2018-02-03 22:25:05 +00:00
|
|
|
uint16_t light_still_on = 0;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
strip_timer_counter++;
|
|
|
|
if (!light_power) { // Power Off
|
2017-10-18 17:22:34 +01:00
|
|
|
sleep = Settings.sleep;
|
2017-10-23 11:18:15 +01:00
|
|
|
strip_timer_counter = 0;
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
2017-10-26 15:33:33 +01:00
|
|
|
light_still_on += light_new_color[i];
|
|
|
|
}
|
2017-10-27 11:12:07 +01:00
|
|
|
if (light_still_on && Settings.light_fade && (Settings.light_scheme < LS_MAX)) {
|
2017-10-26 15:33:33 +01:00
|
|
|
uint8_t speed = Settings.light_speed;
|
|
|
|
if (speed > 6) {
|
|
|
|
speed = 6;
|
|
|
|
}
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
if (light_new_color[i] > 0) {
|
|
|
|
light_new_color[i] -= (light_new_color[i] >> speed) +1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
light_new_color[i] = 0;
|
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-09-16 16:34:03 +01:00
|
|
|
sleep = 0;
|
2017-10-26 15:33:33 +01:00
|
|
|
switch (Settings.light_scheme) {
|
2017-10-25 13:27:30 +01:00
|
|
|
case LS_POWER:
|
2017-10-26 15:33:33 +01:00
|
|
|
LightSetDimmer(Settings.light_dimmer);
|
2017-10-25 13:27:30 +01:00
|
|
|
LightFade();
|
2017-09-16 16:34:03 +01:00
|
|
|
break;
|
2017-10-25 13:27:30 +01:00
|
|
|
case LS_WAKEUP:
|
2017-10-23 11:18:15 +01:00
|
|
|
if (2 == light_wakeup_active) {
|
|
|
|
light_wakeup_active = 1;
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
light_new_color[i] = 0;
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_counter = 0;
|
|
|
|
light_wakeup_dimmer = 0;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_counter++;
|
2017-10-26 15:33:33 +01:00
|
|
|
if (light_wakeup_counter > ((Settings.light_wakeup * STATES) / Settings.light_dimmer)) {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_counter = 0;
|
|
|
|
light_wakeup_dimmer++;
|
2017-10-26 15:33:33 +01:00
|
|
|
if (light_wakeup_dimmer <= Settings.light_dimmer) {
|
2017-10-23 11:18:15 +01:00
|
|
|
LightSetDimmer(light_wakeup_dimmer);
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
light_new_color[i] = light_current_color[i];
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-12-29 13:35:05 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUP "\":\"" D_JSON_DONE "\"}"));
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(TELE, PSTR(D_CMND_WAKEUP));
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_active = 0;
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_scheme = LS_POWER;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-10-25 13:27:30 +01:00
|
|
|
case LS_CYCLEUP:
|
|
|
|
LightCycleColor(1);
|
|
|
|
break;
|
|
|
|
case LS_CYCLEDN:
|
|
|
|
LightCycleColor(-1);
|
|
|
|
break;
|
|
|
|
case LS_RANDOM:
|
|
|
|
LightRandomColor();
|
|
|
|
break;
|
2017-09-16 16:34:03 +01:00
|
|
|
#ifdef USE_WS2812 // ************************************************************************
|
|
|
|
default:
|
2017-10-25 13:27:30 +01:00
|
|
|
if (LT_WS2812 == light_type) {
|
2017-10-26 15:33:33 +01:00
|
|
|
Ws2812ShowScheme(Settings.light_scheme -LS_MAX);
|
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-26 15:33:33 +01:00
|
|
|
if ((Settings.light_scheme < LS_MAX) || !light_power) {
|
2017-10-23 11:18:15 +01:00
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
if (light_last_color[i] != light_new_color[i]) {
|
|
|
|
light_update = 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
if (light_update) {
|
|
|
|
light_update = 0;
|
|
|
|
for (byte i = 0; i < light_subtype; i++) {
|
|
|
|
light_last_color[i] = light_new_color[i];
|
2018-09-29 15:55:53 +01:00
|
|
|
cur_col[i] = light_last_color[i]*Settings.rgbwwTable[i]/255;
|
|
|
|
cur_col[i] = (Settings.light_correction) ? ledTable[cur_col[i]] : cur_col[i];
|
2017-10-25 13:27:30 +01:00
|
|
|
if (light_type < LT_PWM6) {
|
2017-09-16 16:34:03 +01:00
|
|
|
if (pin[GPIO_PWM1 +i] < 99) {
|
2017-11-08 10:57:16 +00:00
|
|
|
if (cur_col[i] > 0xFC) {
|
|
|
|
cur_col[i] = 0xFC; // Fix unwanted blinking and PWM watchdog errors for values close to pwm_range (H801, Arilux and BN-SZ01)
|
|
|
|
}
|
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-25 13:27:30 +01:00
|
|
|
if (LT_WS2812 == light_type) {
|
2017-11-11 15:02:18 +00:00
|
|
|
Ws2812SetColor(0, cur_col[0], cur_col[1], cur_col[2], cur_col[3]);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
#endif // USE_ES2812 ************************************************************************
|
2017-10-25 13:27:30 +01:00
|
|
|
if (light_type > LT_WS2812) {
|
2017-10-18 17:22:34 +01:00
|
|
|
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-10-23 11:18:15 +01:00
|
|
|
float light_hue = 0.0;
|
|
|
|
float light_saturation = 0.0;
|
|
|
|
float light_brightness = 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-26 15:33:33 +01:00
|
|
|
LightSetDimmer(Settings.light_dimmer);
|
2017-09-18 17:06:46 +01:00
|
|
|
|
|
|
|
// convert colors to float between (0.0 - 1.0)
|
2017-10-23 11:18:15 +01:00
|
|
|
float r = light_current_color[0] / 255.0f;
|
|
|
|
float g = light_current_color[1] / 255.0f;
|
|
|
|
float b = light_current_color[2] / 255.0f;
|
2017-09-18 17:06:46 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
light_hue = 0.0;
|
|
|
|
light_brightness = max;
|
|
|
|
light_saturation = (0.0f == light_brightness) ? 0 : (d / light_brightness);
|
2017-09-18 17:06:46 +01:00
|
|
|
|
|
|
|
if (d != 0.0f)
|
|
|
|
{
|
|
|
|
if (r == max) {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_hue = (g - b) / d + (g < b ? 6.0f : 0.0f);
|
2017-09-18 17:06:46 +01:00
|
|
|
} else if (g == max) {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_hue = (b - r) / d + 2.0f;
|
2017-09-18 17:06:46 +01:00
|
|
|
} else {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_hue = (r - g) / d + 4.0f;
|
2017-09-18 17:06:46 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_hue /= 6.0f;
|
2017-09-18 17:06:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void LightHsbToRgb()
|
2017-09-18 17:06:46 +01:00
|
|
|
{
|
|
|
|
float r;
|
|
|
|
float g;
|
|
|
|
float b;
|
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
float h = light_hue;
|
|
|
|
float s = light_saturation;
|
|
|
|
float v = light_brightness;
|
2017-09-18 17:06:46 +01:00
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
if (0.0f == light_saturation) {
|
|
|
|
r = g = b = v; // Achromatic or black
|
2017-09-18 17:06:46 +01:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 11:18:15 +01:00
|
|
|
light_current_color[0] = (uint8_t)(r * 255.0f);
|
|
|
|
light_current_color[1] = (uint8_t)(g * 255.0f);
|
|
|
|
light_current_color[2] = (uint8_t)(b * 255.0f);
|
2018-09-08 07:49:08 +01:00
|
|
|
light_current_color[3] = 0;
|
|
|
|
light_current_color[4] = 0;
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-09-08 07:49:08 +01:00
|
|
|
void LightGetHsb(float *hue, float *sat, float *bri, bool gotct)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2018-09-08 07:49:08 +01:00
|
|
|
if (light_subtype > LST_COLDWARM && !gotct) {
|
2017-10-18 17:22:34 +01:00
|
|
|
LightRgbToHsb();
|
2017-10-23 11:18:15 +01:00
|
|
|
*hue = light_hue;
|
|
|
|
*sat = light_saturation;
|
|
|
|
*bri = light_brightness;
|
2017-08-12 16:55:20 +01:00
|
|
|
} else {
|
|
|
|
*hue = 0;
|
|
|
|
*sat = 0;
|
2017-10-26 15:33:33 +01:00
|
|
|
*bri = (0.01f * (float)Settings.light_dimmer);
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2017-07-15 14:07:30 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 07:49:08 +01:00
|
|
|
void LightSetHsb(float hue, float sat, float bri, uint16_t ct, bool gotct)
|
2017-07-15 14:07:30 +01:00
|
|
|
{
|
2017-10-25 13:27:30 +01:00
|
|
|
if (light_subtype > LST_COLDWARM) {
|
2018-09-08 07:49:08 +01:00
|
|
|
if ((LST_RGBWC == light_subtype) && (gotct)) {
|
|
|
|
uint8_t tmp = (uint8_t)(bri * 100);
|
|
|
|
Settings.light_dimmer = tmp;
|
|
|
|
if (ct > 0) {
|
|
|
|
LightSetColorTemp(ct);
|
|
|
|
}
|
2017-08-16 16:05:36 +01:00
|
|
|
} else {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_hue = hue;
|
|
|
|
light_saturation = sat;
|
|
|
|
light_brightness = 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();
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_COLOR));
|
2017-08-12 16:55:20 +01:00
|
|
|
} else {
|
|
|
|
uint8_t tmp = (uint8_t)(bri * 100);
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_dimmer = tmp;
|
2017-10-25 13:27:30 +01:00
|
|
|
if (LST_COLDWARM == light_subtype) {
|
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();
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_COLOR));
|
2017-08-15 21:09:52 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
LightPreparePower();
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, 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-23 11:18:15 +01:00
|
|
|
boolean LightColorEntry(char *buffer, uint8_t buffer_length)
|
|
|
|
{
|
|
|
|
char scolor[10];
|
|
|
|
char *p;
|
|
|
|
char *str;
|
2017-10-24 13:57:10 +01:00
|
|
|
uint8_t entry_type = 0; // Invalid
|
2017-11-17 16:52:31 +00:00
|
|
|
uint8_t value = light_fixed_color_index;
|
2017-10-23 11:18:15 +01:00
|
|
|
|
2017-10-24 13:57:10 +01:00
|
|
|
if (buffer[0] == '#') { // Optional hexadecimal entry
|
2017-10-23 11:18:15 +01:00
|
|
|
buffer++;
|
|
|
|
buffer_length--;
|
|
|
|
}
|
2017-11-17 16:52:31 +00:00
|
|
|
|
|
|
|
if (light_subtype >= LST_RGB) {
|
|
|
|
char option = (1 == buffer_length) ? buffer[0] : '\0';
|
|
|
|
if (('+' == option) && (light_fixed_color_index < MAX_FIXED_COLOR)) {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
else if (('-' == option) && (light_fixed_color_index > 1)) {
|
|
|
|
value--;
|
|
|
|
} else {
|
|
|
|
value = atoi(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:07:45 +00:00
|
|
|
memset(&light_entry_color, 0x00, sizeof(light_entry_color));
|
2017-10-24 13:57:10 +01:00
|
|
|
if (strstr(buffer, ",")) { // Decimal entry
|
2017-10-23 11:18:15 +01:00
|
|
|
int8_t i = 0;
|
2017-10-24 13:57:10 +01:00
|
|
|
for (str = strtok_r(buffer, ",", &p); str && i < 6; str = strtok_r(NULL, ",", &p)) {
|
|
|
|
if (i < 5) {
|
|
|
|
light_entry_color[i++] = atoi(str);
|
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2018-01-18 15:19:28 +00:00
|
|
|
entry_type = 2; // Decimal
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2018-04-02 14:00:26 +01:00
|
|
|
else if (((2 * light_subtype) == buffer_length) || (buffer_length > 3)) { // Hexadecimal entry
|
|
|
|
for (byte i = 0; i < buffer_length / 2; i++) {
|
2017-10-23 11:18:15 +01:00
|
|
|
strlcpy(scolor, buffer + (i *2), 3);
|
|
|
|
light_entry_color[i] = (uint8_t)strtol(scolor, &p, 16);
|
|
|
|
}
|
2017-10-24 13:57:10 +01:00
|
|
|
entry_type = 1; // Hexadecimal
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2017-11-17 16:52:31 +00:00
|
|
|
else if ((light_subtype >= LST_RGB) && (value > 0) && (value <= MAX_FIXED_COLOR)) {
|
|
|
|
light_fixed_color_index = value;
|
2017-11-15 22:07:45 +00:00
|
|
|
memcpy_P(&light_entry_color, &kFixedColor[value -1], 3);
|
|
|
|
entry_type = 1; // Hexadecimal
|
|
|
|
}
|
|
|
|
else if ((value > 199) && (value <= 199 + MAX_FIXED_COLD_WARM)) {
|
2018-09-04 21:43:27 +01:00
|
|
|
if (LST_RGBW == light_subtype) {
|
|
|
|
memcpy_P(&light_entry_color[3], &kFixedWhite[value -200], 1);
|
|
|
|
entry_type = 1; // Hexadecimal
|
|
|
|
}
|
|
|
|
else if (LST_COLDWARM == light_subtype) {
|
2017-11-17 16:52:31 +00:00
|
|
|
memcpy_P(&light_entry_color, &kFixedColdWarm[value -200], 2);
|
|
|
|
entry_type = 1; // Hexadecimal
|
|
|
|
}
|
|
|
|
else if (LST_RGBWC == light_subtype) {
|
|
|
|
memcpy_P(&light_entry_color[3], &kFixedColdWarm[value -200], 2);
|
|
|
|
entry_type = 1; // Hexadecimal
|
|
|
|
}
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
if (entry_type) {
|
|
|
|
Settings.flag.decimal_text = entry_type -1;
|
|
|
|
}
|
|
|
|
return (entry_type);
|
|
|
|
}
|
|
|
|
|
2017-10-26 15:33:33 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-01-05 11:26:19 +00:00
|
|
|
//boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t XdrvMailbox.data_len, int16_t XdrvMailbox.payload)
|
|
|
|
boolean LightCommand()
|
2017-02-19 16:49:17 +00:00
|
|
|
{
|
2017-10-29 17:18:46 +00:00
|
|
|
char command [CMDSZ];
|
2017-04-25 17:24:42 +01:00
|
|
|
boolean serviced = true;
|
|
|
|
boolean coldim = false;
|
2017-10-23 11:18:15 +01:00
|
|
|
boolean valid_entry = false;
|
|
|
|
char scolor[25];
|
2018-01-05 11:26:19 +00:00
|
|
|
char option = (1 == XdrvMailbox.data_len) ? XdrvMailbox.data[0] : '\0';
|
2017-10-23 11:18:15 +01:00
|
|
|
|
2018-01-05 11:26:19 +00:00
|
|
|
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kLightCommands);
|
2018-05-09 09:49:43 +01:00
|
|
|
if (-1 == command_code) {
|
|
|
|
serviced = false; // Unknown command
|
|
|
|
}
|
|
|
|
else if ((CMND_COLOR == command_code) && (light_subtype > LST_SINGLE) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 6)) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
valid_entry = LightColorEntry(XdrvMailbox.data, XdrvMailbox.data_len);
|
2017-10-23 11:18:15 +01:00
|
|
|
if (valid_entry) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if (XdrvMailbox.index <= 2) { // Color(1), 2
|
2017-10-23 11:18:15 +01:00
|
|
|
memcpy(light_current_color, light_entry_color, sizeof(light_current_color));
|
2017-12-27 13:10:55 +00:00
|
|
|
uint8_t dimmer = Settings.light_dimmer;
|
2017-10-23 11:18:15 +01:00
|
|
|
LightSetColor();
|
2018-01-05 11:26:19 +00:00
|
|
|
if (2 == XdrvMailbox.index) {
|
2017-12-27 13:10:55 +00:00
|
|
|
Settings.light_dimmer = dimmer;
|
|
|
|
}
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_scheme = 0;
|
2017-10-23 11:18:15 +01:00
|
|
|
coldim = true;
|
2018-03-10 16:58:54 +00:00
|
|
|
} else { // Color3, 4, 5 and 6
|
2017-10-25 13:27:30 +01:00
|
|
|
for (byte i = 0; i < LST_RGB; i++) {
|
2018-01-05 11:26:19 +00:00
|
|
|
Settings.ws_color[XdrvMailbox.index -3][i] = light_entry_color[i];
|
2017-10-24 13:57:10 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 11:57:08 +01:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
if (!valid_entry && (XdrvMailbox.index <= 2)) {
|
2017-11-11 11:33:30 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, LightGetColor(0, scolor));
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
if (XdrvMailbox.index >= 3) {
|
2017-10-23 11:18:15 +01:00
|
|
|
scolor[0] = '\0';
|
2017-10-25 13:27:30 +01:00
|
|
|
for (byte i = 0; i < LST_RGB; i++) {
|
2017-10-23 11:18:15 +01:00
|
|
|
if (Settings.flag.decimal_text) {
|
2018-01-05 11:26:19 +00:00
|
|
|
snprintf_P(scolor, 25, PSTR("%s%s%d"), scolor, (i > 0) ? "," : "", Settings.ws_color[XdrvMailbox.index -3][i]);
|
2017-10-23 11:18:15 +01:00
|
|
|
} else {
|
2018-01-05 11:26:19 +00:00
|
|
|
snprintf_P(scolor, 25, PSTR("%s%02X"), scolor, Settings.ws_color[XdrvMailbox.index -3][i]);
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2017-08-12 16:55:20 +01:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, XdrvMailbox.index, scolor);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 15:28:18 +00:00
|
|
|
else if ((CMND_CHANNEL == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= light_subtype ) ) {
|
|
|
|
// Set "Channel" directly - this allows Color and Direct PWM control to coexist
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 100)) {
|
|
|
|
uint8_t level = XdrvMailbox.payload;
|
|
|
|
light_current_color[XdrvMailbox.index-1] = round(level * 2.55);
|
|
|
|
LightSetColor();
|
|
|
|
coldim = true;
|
|
|
|
}
|
2018-03-21 08:56:39 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, XdrvMailbox.index, round(light_current_color[XdrvMailbox.index -1] / 2.55));
|
2018-03-20 15:28:18 +00:00
|
|
|
}
|
|
|
|
else if ((CMND_HSBCOLOR == command_code) && ( light_subtype >= LST_RGB)) {
|
2018-08-29 15:05:27 +01:00
|
|
|
bool validHSB = (XdrvMailbox.data_len > 0);
|
|
|
|
if (validHSB) {
|
|
|
|
uint16_t HSB[3];
|
|
|
|
if (strstr(XdrvMailbox.data, ",")) { // Command with 3 comma separated parameters, Hue (0<H<360), Saturation (0<S<100) AND Brightness (0<B<100)
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
char *substr;
|
2018-03-20 15:28:18 +00:00
|
|
|
|
2018-08-29 15:05:27 +01:00
|
|
|
if (0 == i) {
|
|
|
|
substr = strtok(XdrvMailbox.data, ",");
|
|
|
|
} else {
|
|
|
|
substr = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
if (substr != NULL) {
|
|
|
|
HSB[i] = atoi(substr);
|
|
|
|
} else {
|
|
|
|
validHSB = false;
|
|
|
|
}
|
2018-08-28 02:46:04 +01:00
|
|
|
}
|
2018-08-29 15:05:27 +01:00
|
|
|
} else { // Command with only 1 parameter, Hue (0<H<360), Saturation (0<S<100) OR Brightness (0<B<100)
|
|
|
|
float hsb[3];
|
|
|
|
|
2018-09-08 07:49:08 +01:00
|
|
|
LightGetHsb(&hsb[0],&hsb[1],&hsb[2], false);
|
2018-08-29 15:05:27 +01:00
|
|
|
HSB[0] = round(hsb[0] * 360);
|
|
|
|
HSB[1] = round(hsb[1] * 100);
|
|
|
|
HSB[2] = round(hsb[2] * 100);
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index < 4)) {
|
|
|
|
HSB[XdrvMailbox.index -1] = XdrvMailbox.payload;
|
2018-08-28 02:46:04 +01:00
|
|
|
} else {
|
|
|
|
validHSB = false;
|
|
|
|
}
|
2018-03-20 15:28:18 +00:00
|
|
|
}
|
2018-08-29 15:05:27 +01:00
|
|
|
if (validHSB) {
|
|
|
|
// Translate to fractional elements as required by LightHsbToRgb
|
|
|
|
// Keep the results <=1 in the event someone passes something out of range.
|
|
|
|
LightSetHsb(( (HSB[0]>360) ? (HSB[0] % 360) : HSB[0] ) /360.0,
|
|
|
|
( (HSB[1]>100) ? (HSB[1] % 100) : HSB[1] ) /100.0,
|
|
|
|
( (HSB[2]>100) ? (HSB[2] % 100) : HSB[2] ) /100.0,
|
2018-09-08 07:49:08 +01:00
|
|
|
0,
|
|
|
|
false);
|
2018-03-20 15:28:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LightState(0);
|
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
#ifdef USE_WS2812 // ***********************************************************************
|
2018-01-05 11:26:19 +00:00
|
|
|
else if ((CMND_LED == command_code) && (LT_WS2812 == light_type) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= Settings.light_pixels)) {
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
2018-03-31 18:56:16 +01:00
|
|
|
char *p;
|
|
|
|
uint16_t idx = XdrvMailbox.index;
|
2018-06-16 14:23:11 +01:00
|
|
|
Ws2812ForceSuspend();
|
2018-04-01 10:04:00 +01:00
|
|
|
for (char *color = strtok_r(XdrvMailbox.data, " ", &p); color; color = strtok_r(NULL, " ", &p)) {
|
2018-03-31 18:56:16 +01:00
|
|
|
if (LightColorEntry(color, strlen(color))) {
|
|
|
|
Ws2812SetColor(idx, light_entry_color[0], light_entry_color[1], light_entry_color[2], light_entry_color[3]);
|
|
|
|
idx++;
|
|
|
|
if (idx >= Settings.light_pixels) break;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2018-06-16 14:23:11 +01:00
|
|
|
|
|
|
|
Ws2812ForceUpdate();
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, XdrvMailbox.index, Ws2812GetColor(XdrvMailbox.index, scolor));
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if ((CMND_PIXELS == command_code) && (LT_WS2812 == light_type)) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= WS2812_MAX_LEDS)) {
|
|
|
|
Settings.light_pixels = XdrvMailbox.payload;
|
2018-03-10 16:58:54 +00:00
|
|
|
Settings.light_rotation = 0;
|
2017-10-18 17:22:34 +01:00
|
|
|
Ws2812Clear();
|
2017-10-23 11:18:15 +01:00
|
|
|
light_update = 1;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_pixels);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2018-03-10 16:58:54 +00:00
|
|
|
else if ((CMND_ROTATION == command_code) && (LT_WS2812 == light_type)) {
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < Settings.light_pixels)) {
|
|
|
|
Settings.light_rotation = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_rotation);
|
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
else if ((CMND_WIDTH == command_code) && (LT_WS2812 == light_type) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 4)) {
|
|
|
|
if (1 == XdrvMailbox.index) {
|
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 4)) {
|
|
|
|
Settings.light_width = XdrvMailbox.payload;
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_width);
|
2017-10-23 11:18:15 +01:00
|
|
|
} else {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload < 32)) {
|
|
|
|
Settings.ws_width[XdrvMailbox.index -2] = XdrvMailbox.payload;
|
2017-10-23 11:18:15 +01:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, XdrvMailbox.index, Settings.ws_width[XdrvMailbox.index -2]);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 13:27:30 +01:00
|
|
|
#endif // USE_WS2812 ************************************************************************
|
2017-10-29 17:18:46 +00:00
|
|
|
else if ((CMND_SCHEME == command_code) && (light_subtype >= LST_RGB)) {
|
2017-12-29 13:55:39 +00:00
|
|
|
uint8_t max_scheme = (LT_WS2812 == light_type) ? LS_MAX + WS2812_SCHEMES : LS_MAX -1;
|
2017-11-15 22:07:45 +00:00
|
|
|
if (('+' == option) && (Settings.light_scheme < max_scheme)) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = Settings.light_scheme + ((0 == Settings.light_scheme) ? 2 : 1); // Skip wakeup
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
|
|
|
else if (('-' == option) && (Settings.light_scheme > 0)) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = Settings.light_scheme - ((2 == Settings.light_scheme) ? 2 : 1); // Skip wakeup
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= max_scheme)) {
|
|
|
|
Settings.light_scheme = XdrvMailbox.payload;
|
2017-10-26 15:33:33 +01:00
|
|
|
if (LS_WAKEUP == Settings.light_scheme) {
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_active = 3;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-25 13:27:30 +01:00
|
|
|
LightPowerOn();
|
2017-10-23 11:18:15 +01:00
|
|
|
strip_timer_counter = 0;
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_scheme);
|
2017-09-16 16:34:03 +01:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_WAKEUP == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 100)) {
|
|
|
|
Settings.light_dimmer = XdrvMailbox.payload;
|
2017-09-18 17:06:46 +01:00
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_active = 3;
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_scheme = LS_WAKEUP;
|
2017-10-25 13:27:30 +01:00
|
|
|
LightPowerOn();
|
2018-01-06 16:34:42 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_STARTED);
|
2017-09-18 17:06:46 +01:00
|
|
|
}
|
2017-11-17 16:52:31 +00:00
|
|
|
else if ((CMND_COLORTEMPERATURE == command_code) && ((LST_COLDWARM == light_subtype) || (LST_RGBWC == light_subtype))) { // ColorTemp
|
|
|
|
if (option != '\0') {
|
|
|
|
uint16_t value = LightGetColorTemp();
|
|
|
|
if ('+' == option) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = (value > 466) ? 500 : value + 34;
|
2017-11-17 16:52:31 +00:00
|
|
|
}
|
|
|
|
else if ('-' == option) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = (value < 187) ? 153 : value - 34;
|
2017-11-17 16:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 153) && (XdrvMailbox.payload <= 500)) { // https://developers.meethue.com/documentation/core-concepts
|
|
|
|
LightSetColorTemp(XdrvMailbox.payload);
|
2017-08-16 16:05:36 +01:00
|
|
|
coldim = true;
|
|
|
|
} else {
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, LightGetColorTemp());
|
2017-08-16 16:05:36 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_DIMMER == command_code) {
|
2017-11-15 22:07:45 +00:00
|
|
|
if ('+' == option) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = (Settings.light_dimmer > 89) ? 100 : Settings.light_dimmer + 10;
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
|
|
|
else if ('-' == option) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = (Settings.light_dimmer < 11) ? 1 : Settings.light_dimmer - 10;
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 100)) {
|
|
|
|
Settings.light_dimmer = XdrvMailbox.payload;
|
2017-11-08 10:57:16 +00:00
|
|
|
light_update = 1;
|
2017-03-12 17:36:33 +00:00
|
|
|
coldim = true;
|
|
|
|
} else {
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_dimmer);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_LEDTABLE == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
|
|
|
switch (XdrvMailbox.payload) {
|
2017-02-19 16:49:17 +00:00
|
|
|
case 0: // Off
|
|
|
|
case 1: // On
|
2018-01-05 11:26:19 +00:00
|
|
|
Settings.light_correction = XdrvMailbox.payload;
|
2017-02-19 16:49:17 +00:00
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_correction ^= 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-10-23 11:18:15 +01:00
|
|
|
light_update = 1;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.light_correction));
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2018-09-29 15:55:53 +01:00
|
|
|
else if (CMND_RGBWWTABLE == command_code) {
|
|
|
|
bool validtable = (XdrvMailbox.data_len > 0);
|
|
|
|
char scolor[25];
|
|
|
|
if (validtable) {
|
|
|
|
uint16_t HSB[3];
|
|
|
|
if (strstr(XdrvMailbox.data, ",")) { // Command with up to 5 comma separated parameters
|
|
|
|
for (int i = 0; i < LST_RGBWC; i++) {
|
|
|
|
char *substr;
|
|
|
|
|
|
|
|
if (0 == i) {
|
|
|
|
substr = strtok(XdrvMailbox.data, ",");
|
|
|
|
} else {
|
|
|
|
substr = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
if (substr != NULL) {
|
|
|
|
Settings.rgbwwTable[i] = atoi(substr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
light_update = 1;
|
|
|
|
}
|
|
|
|
scolor[0] = '\0';
|
|
|
|
for (byte i = 0; i < LST_RGBWC; i++) {
|
|
|
|
snprintf_P(scolor, 25, PSTR("%s%s%d"), scolor, (i > 0) ? "," : "", Settings.rgbwwTable[i]);
|
|
|
|
}
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, XdrvMailbox.index, scolor);
|
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_FADE == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
switch (XdrvMailbox.payload) {
|
2017-06-06 22:23:23 +01:00
|
|
|
case 0: // Off
|
|
|
|
case 1: // On
|
2018-01-05 11:26:19 +00:00
|
|
|
Settings.light_fade = XdrvMailbox.payload;
|
2017-06-06 22:23:23 +01:00
|
|
|
break;
|
|
|
|
case 2: // Toggle
|
2017-10-26 15:33:33 +01:00
|
|
|
Settings.light_fade ^= 1;
|
2017-06-06 22:23:23 +01:00
|
|
|
break;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.light_fade));
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_SPEED == command_code) { // 1 - fast, 20 - very slow
|
2017-11-15 22:07:45 +00:00
|
|
|
if (('+' == option) && (Settings.light_speed > 1)) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = Settings.light_speed -1;
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
|
|
|
else if (('-' == option) && (Settings.light_speed < STATES)) {
|
2018-01-05 11:26:19 +00:00
|
|
|
XdrvMailbox.payload = Settings.light_speed +1;
|
2017-11-15 22:07:45 +00:00
|
|
|
}
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= STATES)) {
|
|
|
|
Settings.light_speed = XdrvMailbox.payload;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_speed);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_WAKEUPDURATION == command_code) {
|
2018-01-05 11:26:19 +00:00
|
|
|
if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload < 3001)) {
|
|
|
|
Settings.light_wakeup = XdrvMailbox.payload;
|
2017-10-23 11:18:15 +01:00
|
|
|
light_wakeup_active = 0;
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-11-07 14:57:24 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_wakeup);
|
2017-02-19 16:49:17 +00:00
|
|
|
}
|
2017-10-29 17:18:46 +00:00
|
|
|
else if (CMND_UNDOCA == command_code) { // Theos legacy status
|
2017-10-23 11:18:15 +01:00
|
|
|
LightGetColor(1, scolor);
|
2017-09-08 16:14:39 +01:00
|
|
|
scolor[6] = '\0'; // RGB only
|
2017-11-11 11:33:30 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,%d,%d,%d,%d,%d"),
|
2017-10-26 15:33:33 +01:00
|
|
|
scolor, Settings.light_fade, Settings.light_correction, Settings.light_scheme, Settings.light_speed, Settings.light_width);
|
2018-01-18 15:19:28 +00:00
|
|
|
MqttPublishPrefixTopic_P(STAT, XdrvMailbox.topic);
|
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
|
|
|
|
}
|
2018-05-09 09:49:43 +01:00
|
|
|
|
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
|
|
|
}
|
2018-05-28 10:35:23 +01:00
|
|
|
|
2017-02-19 16:49:17 +00:00
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2018-01-05 11:26:19 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-05-28 10:35:23 +01:00
|
|
|
#define XDRV_04
|
2018-01-05 11:26:19 +00:00
|
|
|
|
2018-05-28 10:35:23 +01:00
|
|
|
boolean Xdrv04(byte function)
|
2018-01-05 11:26:19 +00:00
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if (light_type) {
|
|
|
|
switch (function) {
|
2018-06-04 17:10:38 +01:00
|
|
|
case FUNC_PRE_INIT:
|
2018-01-05 11:26:19 +00:00
|
|
|
LightInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_50_MSECOND:
|
|
|
|
LightAnimate();
|
|
|
|
#ifdef USE_ARILUX_RF
|
2018-02-07 16:50:02 +00:00
|
|
|
if (pin[GPIO_ARIRFRCV] < 99) AriluxRfHandler();
|
2018-01-05 11:26:19 +00:00
|
|
|
#endif // USE_ARILUX_RF
|
|
|
|
break;
|
2018-02-07 16:50:02 +00:00
|
|
|
#ifdef USE_ARILUX_RF
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
if (10 == uptime) AriluxRfInit(); // Needs rest before enabling RF interrupts
|
|
|
|
break;
|
|
|
|
#endif // USE_ARILUX_RF
|
2018-01-05 11:26:19 +00:00
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = LightCommand();
|
|
|
|
break;
|
|
|
|
case FUNC_SET_POWER:
|
|
|
|
LightSetPower();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|