Tasmota/sonoff/xdrv_snfled.ino

921 lines
30 KiB
Arduino
Raw Normal View History

/*
xdrv_snfled.ino - PWM, WS2812 and sonoff led support for Sonoff-Tasmota
Copyright (C) 2017 Theo Arends
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.
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-10-27 11:12:07 +01:00
* PWM, WS2812, Sonoff B1, AiLight, Sonoff Led and BN-SZ01, H801, MagicHome and Arilux
*
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)
* 3 PWM3 RGB no (H801, MagicHome and Arilux)
* 4 PWM4 RGBW no (H801, MagicHome and Arilux)
* 5 PWM5 RGBCW yes (H801, Arilux)
* 9 reserved no
* 10 reserved yes
* 11 +WS2812 RGB no
* 12 AiLight RGBW no
* 13 Sonoff B1 RGBCW yes
*
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
*
\*********************************************************************************************/
enum LightCommands {
CMND_COLOR, CMND_COLORTEMPERATURE, CMND_DIMMER, CMND_LED, CMND_LEDTABLE, CMND_FADE,
CMND_PIXELS, CMND_SCHEME, CMND_SPEED, CMND_WAKEUP, CMND_WAKEUPDURATION, CMND_WIDTH, CMND_UNDOCA };
const char kLightCommands[] PROGMEM =
D_CMND_COLOR "|" D_CMND_COLORTEMPERATURE "|" D_CMND_DIMMER "|" D_CMND_LED "|" D_CMND_LEDTABLE "|" D_CMND_FADE "|"
D_CMND_PIXELS "|" D_CMND_SCHEME "|" D_CMND_SPEED "|" D_CMND_WAKEUP "|" D_CMND_WAKEUPDURATION "|" D_CMND_WIDTH "|UNDOCA" ;
uint8_t ledTable[] = {
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 };
uint8_t light_entry_color[5];
uint8_t light_current_color[5];
uint8_t light_new_color[5];
uint8_t light_last_color[5];
uint8_t light_wheel = 0;
uint8_t light_subtype = 0;
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;
unsigned long strip_timer_counter = 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-08-13 10:19:34 +01:00
extern "C" {
void os_delay_us(unsigned int);
}
uint8_t light_pdi_pin;
uint8_t light_pdcki_pin;
2017-08-12 16:55:20 +01:00
void LightDiPulse(uint8_t times)
{
2017-08-13 10:19:34 +01:00
for (uint8_t i = 0; i < times; i++) {
digitalWrite(light_pdi_pin, HIGH);
digitalWrite(light_pdi_pin, LOW);
}
}
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(light_pdcki_pin, HIGH);
digitalWrite(light_pdcki_pin, LOW);
2017-08-12 16:55:20 +01:00
}
}
void LightMy92x1Write(uint8_t data)
2017-08-12 16:55:20 +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;
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;
}
}
void LightMy92x1Init()
2017-08-16 16:05:36 +01:00
{
uint8_t chips = light_type -11; // 1 (AiLight) or 2 (Sonoff B1)
2017-08-12 16:55:20 +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.
LightDiPulse(12);
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
}
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.
LightDiPulse(16);
os_delay_us(12); // TStop > 12us.
2017-08-12 16:55:20 +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 };
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.
2017-08-16 16:05:36 +01:00
for (uint8_t channel = 0; channel < channels[didx]; channel++) {
LightMy92x1Write(duty[didx][channel]); // Send 8bit Data
}
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-12 16:55:20 +01:00
/********************************************************************************************/
void LightInit(void)
{
uint8_t max_scheme = LS_MAX -1;
if (light_type < LT_PWM6) { // PWM
for (byte i = 0; i < light_type; i++) {
Settings.pwm_value[i] = 0; // Disable direct PWM control
}
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
}
if (SONOFF_LED == Settings.module) { // Fix Sonoff Led instabilities
if (!my_module.gp.io[4]) {
pinMode(4, OUTPUT); // Stop floating outputs
digitalWrite(4, LOW);
}
if (!my_module.gp.io[5]) {
pinMode(5, OUTPUT); // Stop floating outputs
digitalWrite(5, LOW);
}
if (!my_module.gp.io[14]) {
pinMode(14, OUTPUT); // Stop floating outputs
digitalWrite(14, LOW);
}
2017-08-12 16:55:20 +01:00
}
}
#ifdef USE_WS2812 // ************************************************************************
else if (LT_WS2812 == light_type) {
Ws2812Init();
max_scheme = LS_MAX +7;
}
#endif // USE_WS2812 ************************************************************************
else {
light_pdi_pin = pin[GPIO_DI];
light_pdcki_pin = pin[GPIO_DCKI];
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
LightMy92x1Init();
}
light_subtype = light_type &7;
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;
}
light_power = 0;
light_update = 1;
light_wakeup_active = 0;
}
void LightSetColorTemp(uint16_t ct)
2017-08-16 16:05:36 +01:00
{
/* Color Temperature (https://developers.meethue.com/documentation/core-concepts)
*
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;
if (LST_RGBWC == light_subtype) {
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 {
Settings.light_color[0] = (uint8_t)icold;
Settings.light_color[1] = (uint8_t)iwarm;
2017-08-16 16:05:36 +01:00
}
}
uint16_t LightGetColorTemp()
2017-08-16 16:05:36 +01:00
{
uint8_t ct_idx = 0;
if (LST_RGBWC == light_subtype) {
2017-08-16 16:05:36 +01:00
ct_idx = 3;
}
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 {
my_ct = Settings.light_color[ct_idx];
2017-08-16 16:05:36 +01:00
return 499 - ((my_ct * 136) / 100);
}
}
void LightSetDimmer(uint8_t myDimmer)
{
2017-08-12 16:55:20 +01:00
float temp;
if ((SONOFF_BN == Settings.module) && (100 == myDimmer)) {
myDimmer = 99; // BN-SZ01 starts flickering at dimmer = 100
2017-08-12 16:55:20 +01:00
}
float dimmer = 100 / (float)myDimmer;
for (byte i = 0; i < light_subtype; i++) {
temp = (float)Settings.light_color[i] / dimmer;
light_current_color[i] = (uint8_t)temp;
2017-08-12 16:55:20 +01:00
}
}
void LightSetColor()
2017-08-12 16:55:20 +01:00
{
uint8_t highest = 0;
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-08-12 16:55:20 +01:00
float mDim = (float)highest / 2.55;
Settings.light_dimmer = (uint8_t)mDim;
float dimmer = 100 / mDim;
for (byte i = 0; i < light_subtype; i++) {
float temp = (float)light_current_color[i] * dimmer;
Settings.light_color[i] = (uint8_t)temp;
2017-08-12 16:55:20 +01:00
}
}
char* LightGetColor(uint8_t type, char* scolor)
2017-08-12 16:55:20 +01:00
{
LightSetDimmer(Settings.light_dimmer);
2017-08-12 16:55:20 +01:00
scolor[0] = '\0';
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;
}
void LightPowerOn()
{
if (Settings.light_dimmer && !(light_power)) {
ExecuteCommandPower(devices_present, 1);
}
}
void LightPreparePower()
{
char scolor[25];
char scommand[16];
if (Settings.light_dimmer && !(light_power)) {
ExecuteCommandPower(devices_present, 7); // No publishPowerState
}
else if (!Settings.light_dimmer && light_power) {
ExecuteCommandPower(devices_present, 6); // No publishPowerState
}
#ifdef USE_DOMOTICZ
DomoticzUpdatePowerState(devices_present);
#endif // USE_DOMOTICZ
GetPowerDevice(scommand, devices_present, sizeof(scommand));
if (light_subtype > LST_SINGLE) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\", \"" D_CMND_DIMMER "\":%d, \"" D_CMND_COLOR "\":\"%s\"}"),
scommand, GetStateText(light_power), Settings.light_dimmer, LightGetColor(0, scolor));
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\", \"" D_CMND_DIMMER "\":%d}"),
scommand, GetStateText(light_power), Settings.light_dimmer);
}
}
void LightFade()
{
if (0 == Settings.light_fade) {
for (byte i = 0; i < light_subtype; i++) {
light_new_color[i] = light_current_color[i];
}
} else {
uint8_t shift = Settings.light_speed;
if (Settings.light_speed > 6) {
shift = (strip_timer_counter % (Settings.light_speed -6)) ? 0 : 8;
}
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;
float dimmer = 100 / (float)Settings.light_dimmer;
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)
{
if (strip_timer_counter % (Settings.light_speed * 2)) {
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();
}
void LightSetPower(uint8_t mpower)
{
light_power = mpower;
if (light_wakeup_active) {
light_wakeup_active--;
}
if (light_power) {
light_update = 1;
}
LightAnimate();
}
void LightAnimate()
{
2017-08-12 16:55:20 +01:00
uint8_t cur_col[5];
uint16_t light_still_on;
strip_timer_counter++;
if (!light_power) { // Power Off
sleep = Settings.sleep;
strip_timer_counter = 0;
for (byte i = 0; i < light_subtype; i++) {
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)) {
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
}
}
else {
sleep = 0;
switch (Settings.light_scheme) {
case LS_POWER:
LightSetDimmer(Settings.light_dimmer);
LightFade();
break;
case LS_WAKEUP:
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
}
light_wakeup_counter = 0;
light_wakeup_dimmer = 0;
}
light_wakeup_counter++;
if (light_wakeup_counter > ((Settings.light_wakeup * STATES) / Settings.light_dimmer)) {
light_wakeup_counter = 0;
light_wakeup_dimmer++;
if (light_wakeup_dimmer <= Settings.light_dimmer) {
LightSetDimmer(light_wakeup_dimmer);
for (byte i = 0; i < light_subtype; i++) {
light_new_color[i] = light_current_color[i];
}
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUP "\":\"" D_DONE "\"}"));
MqttPublishPrefixTopic_P(2, PSTR(D_CMND_WAKEUP));
light_wakeup_active = 0;
Settings.light_scheme = LS_POWER;
}
}
break;
case LS_CYCLEUP:
LightCycleColor(1);
break;
case LS_CYCLEDN:
LightCycleColor(-1);
break;
case LS_RANDOM:
LightRandomColor();
break;
#ifdef USE_WS2812 // ************************************************************************
default:
if (LT_WS2812 == light_type) {
Ws2812ShowScheme(Settings.light_scheme -LS_MAX);
}
#endif // USE_WS2812 ************************************************************************
2017-08-12 16:55:20 +01:00
}
}
if ((Settings.light_scheme < LS_MAX) || !light_power) {
for (byte i = 0; i < light_subtype; i++) {
if (light_last_color[i] != light_new_color[i]) {
light_update = 1;
}
}
if (light_update) {
light_update = 0;
for (byte i = 0; i < light_subtype; i++) {
light_last_color[i] = light_new_color[i];
cur_col[i] = (Settings.light_correction) ? ledTable[light_last_color[i]] : light_last_color[i];
if (light_type < LT_PWM6) {
if (pin[GPIO_PWM1 +i] < 99) {
uint16_t curcol = cur_col[i] * (Settings.pwm_range / 255);
// snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "Cur_Col%d %d, CurCol %d"), i, cur_col[i], curcol);
// AddLog(LOG_LEVEL_DEBUG);
analogWrite(pin[GPIO_PWM1 +i], bitRead(pwm_inverted, i) ? Settings.pwm_range - curcol : curcol);
}
}
}
#ifdef USE_WS2812 // ************************************************************************
if (LT_WS2812 == light_type) {
Ws2812SetColor(0, cur_col[0], cur_col[1], cur_col[2]);
}
#endif // USE_ES2812 ************************************************************************
if (light_type > LT_WS2812) {
LightMy92x1Duty(cur_col[0], cur_col[1], cur_col[2], cur_col[3], cur_col[4]);
}
2017-08-12 16:55:20 +01:00
}
}
}
/*********************************************************************************************\
* Hue support
\*********************************************************************************************/
float light_hue = 0.0;
float light_saturation = 0.0;
float light_brightness = 0.0;
void LightRgbToHsb()
{
LightSetDimmer(Settings.light_dimmer);
// convert colors to float between (0.0 - 1.0)
float r = light_current_color[0] / 255.0f;
float g = light_current_color[1] / 255.0f;
float b = light_current_color[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;
light_hue = 0.0;
light_brightness = max;
light_saturation = (0.0f == light_brightness) ? 0 : (d / light_brightness);
if (d != 0.0f)
{
if (r == max) {
light_hue = (g - b) / d + (g < b ? 6.0f : 0.0f);
} else if (g == max) {
light_hue = (b - r) / d + 2.0f;
} else {
light_hue = (r - g) / d + 4.0f;
}
light_hue /= 6.0f;
}
}
void LightHsbToRgb()
{
float r;
float g;
float b;
float h = light_hue;
float s = light_saturation;
float v = light_brightness;
if (0.0f == light_saturation) {
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;
}
}
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);
}
2017-08-12 16:55:20 +01:00
/********************************************************************************************/
void LightReplaceHsb(String *response)
{
if (light_subtype > LST_COLDWARM) {
LightRgbToHsb();
response->replace("{h}", String((uint16_t)(65535.0f * light_hue)));
response->replace("{s}", String((uint8_t)(254.0f * light_saturation)));
response->replace("{b}", String((uint8_t)(254.0f * light_brightness)));
2017-08-12 16:55:20 +01:00
} else {
response->replace("{h}", "0");
response->replace("{s}", "0");
// response->replace("{b}", String((uint8_t)(2.54f * (float)Settings.light_dimmer)));
response->replace("{b}", String((uint8_t)(0.01f * (float)Settings.light_dimmer)));
2017-08-12 16:55:20 +01:00
}
}
void LightGetHsb(float *hue, float *sat, float *bri)
{
if (light_subtype > LST_COLDWARM) {
LightRgbToHsb();
*hue = light_hue;
*sat = light_saturation;
*bri = light_brightness;
2017-08-12 16:55:20 +01:00
} else {
*hue = 0;
*sat = 0;
// *bri = (2.54f * (float)Settings.light_dimmer);
*bri = (0.01f * (float)Settings.light_dimmer);
2017-08-12 16:55:20 +01:00
}
}
void LightSetHsb(float hue, float sat, float bri, uint16_t ct)
{
if (light_subtype > LST_COLDWARM) {
if ((LST_RGBWC == light_subtype) && (ct > 0)) {
LightSetColorTemp(ct);
2017-08-16 16:05:36 +01:00
} else {
light_hue = hue;
light_saturation = sat;
light_brightness = bri;
LightHsbToRgb();
LightSetColor();
2017-08-16 16:05:36 +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);
Settings.light_dimmer = tmp;
if (LST_COLDWARM == light_subtype) {
if (ct > 0) {
LightSetColorTemp(ct);
}
LightPreparePower();
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_COLOR));
} else {
LightPreparePower();
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_DIMMER));
}
2017-08-12 16:55:20 +01:00
}
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
boolean LightColorEntry(char *buffer, uint8_t buffer_length)
{
char scolor[10];
char *p;
char *str;
uint8_t entry_type = 0; // Invalid
if (buffer[0] == '#') { // Optional hexadecimal entry
buffer++;
buffer_length--;
}
if (strstr(buffer, ",")) { // Decimal entry
int8_t i = 0;
for (str = strtok_r(buffer, ",", &p); str && i < 6; str = strtok_r(NULL, ",", &p)) {
if (i < 5) {
light_entry_color[i++] = atoi(str);
}
}
entry_type = (light_subtype == i) ? 2 : 0; // Decimal
}
else if ((2 * light_subtype) == buffer_length) { // Hexadecimal entry
for (byte i = 0; i < light_subtype; i++) {
strlcpy(scolor, buffer + (i *2), 3);
light_entry_color[i] = (uint8_t)strtol(scolor, &p, 16);
}
entry_type = 1; // Hexadecimal
}
if (entry_type) {
Settings.flag.decimal_text = entry_type -1;
}
return (entry_type);
}
/********************************************************************************************/
boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
{
char command [CMDSZ];
boolean serviced = true;
boolean coldim = false;
boolean valid_entry = false;
char scolor[25];
int command_code = GetCommandCode(command, sizeof(command), type, kLightCommands);
if ((CMND_COLOR == command_code) && (light_subtype > LST_SINGLE) && (index > 0) && (index <= 4)) {
if (data_len > 0) {
valid_entry = LightColorEntry(dataBuf, data_len);
if (valid_entry) {
if (1 == index) { // Color(1)
// for (byte i = 0; i < light_subtype; i++) {
// light_current_color[i] = light_entry_color[i];
// }
memcpy(light_current_color, light_entry_color, sizeof(light_current_color));
LightSetColor();
Settings.light_scheme = 0;
coldim = true;
} else { // Color2, 3 and 4
for (byte i = 0; i < LST_RGB; i++) {
Settings.ws_color[index -2][i] = light_entry_color[i];
}
}
}
}
if (!valid_entry && (1 == index)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"), command, LightGetColor(0, scolor));
}
if (index > 1) {
scolor[0] = '\0';
for (byte i = 0; i < LST_RGB; i++) {
if (Settings.flag.decimal_text) {
snprintf_P(scolor, 25, PSTR("%s%s%d"), scolor, (i > 0) ? "," : "", Settings.ws_color[index -2][i]);
} else {
snprintf_P(scolor, 25, PSTR("%s%02X"), scolor, Settings.ws_color[index -2][i]);
}
2017-08-12 16:55:20 +01:00
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, scolor);
}
}
#ifdef USE_WS2812 // ***********************************************************************
else if ((CMND_LED == command_code) && (LT_WS2812 == light_type) && (index > 0) && (index <= Settings.light_pixels)) {
if (data_len > 0) {
if (LightColorEntry(dataBuf, data_len)) {
Ws2812SetColor(index, light_entry_color[0], light_entry_color[1], light_entry_color[2]);
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Ws2812GetColor(index, scolor));
}
else if ((CMND_PIXELS == command_code) && (LT_WS2812 == light_type)) {
if ((payload > 0) && (payload <= WS2812_MAX_LEDS)) {
Settings.light_pixels = payload;
Ws2812Clear();
light_update = 1;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_pixels);
}
else if ((CMND_WIDTH == command_code) && (LT_WS2812 == light_type) && (index > 0) && (index <= 4)) {
if (1 == index) {
if ((payload >= 0) && (payload <= 4)) {
Settings.light_width = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_width);
} else {
if ((payload > 0) && (payload < 32)) {
Settings.ws_width[index -2] = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, index, Settings.ws_width[index -2]);
}
}
#endif // USE_WS2812 ************************************************************************
else if ((CMND_SCHEME == command_code) && (light_subtype >= LST_RGB)) {
uint8_t max_scheme = (LT_WS2812 == light_type) ? LS_MAX +7 : LS_MAX -1;
if ((payload >= 0) && (payload <= max_scheme)) {
Settings.light_scheme = payload;
if (LS_WAKEUP == Settings.light_scheme) {
light_wakeup_active = 3;
}
LightPowerOn();
strip_timer_counter = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_scheme);
}
else if (CMND_WAKEUP == command_code) {
if ((payload >= 0) && (payload <= 100)) {
Settings.light_dimmer = payload;
}
light_wakeup_active = 3;
Settings.light_scheme = LS_WAKEUP;
LightPowerOn();
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_STARTED);
}
else if ((CMND_COLORTEMPERATURE == command_code) && ((2 == light_subtype) || (5 == light_subtype))) { // ColorTemp
2017-08-16 16:05:36 +01:00
if ((payload >= 153) && (payload <= 500)) { // https://developers.meethue.com/documentation/core-concepts
LightSetColorTemp(payload);
2017-08-16 16:05:36 +01:00
coldim = true;
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, LightGetColorTemp());
2017-08-16 16:05:36 +01:00
}
}
else if (CMND_DIMMER == command_code) {
if ((payload >= 0) && (payload <= 100)) {
Settings.light_dimmer = payload;
coldim = true;
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_dimmer);
}
}
else if (CMND_LEDTABLE == command_code) {
if ((payload >= 0) && (payload <= 2)) {
switch (payload) {
case 0: // Off
case 1: // On
Settings.light_correction = payload;
break;
case 2: // Toggle
Settings.light_correction ^= 1;
break;
}
light_update = 1;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.light_correction));
}
else if (CMND_FADE == command_code) {
switch (payload) {
case 0: // Off
case 1: // On
Settings.light_fade = payload;
break;
case 2: // Toggle
Settings.light_fade ^= 1;
break;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.light_fade));
}
else if (CMND_SPEED == command_code) { // 1 - fast, 20 - very slow
if ((payload > 0) && (payload <= STATES)) {
Settings.light_speed = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_speed);
}
else if (CMND_WAKEUPDURATION == command_code) {
if ((payload > 0) && (payload < 3001)) {
Settings.light_wakeup = payload;
light_wakeup_active = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_wakeup);
}
else if (CMND_UNDOCA == command_code) { // Theos legacy status
LightGetColor(1, scolor);
scolor[6] = '\0'; // RGB only
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, %d, %d, %d, %d, %d"),
scolor, Settings.light_fade, Settings.light_correction, Settings.light_scheme, Settings.light_speed, Settings.light_width);
MqttPublishPrefixTopic_P(1, type);
mqtt_data[0] = '\0';
}
else {
serviced = false; // Unknown command
}
if (coldim) {
LightPreparePower();
}
return serviced;
}