Tasmota/sonoff/xdrv_snfled.ino

587 lines
17 KiB
Arduino
Raw Normal View History

/*
xdrv_snfled.ino - 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/>.
*/
/*********************************************************************************************\
* Sonoff B1, AiLight, Sonoff Led and BN-SZ01
\*********************************************************************************************/
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 };
2017-08-12 16:55:20 +01:00
uint8_t sl_dcolor[5];
uint8_t sl_tcolor[5];
uint8_t sl_lcolor[5];
uint8_t sl_power;
uint8_t sl_any;
uint8_t sl_wakeupActive = 0;
uint8_t sl_wakeupDimmer = 0;
uint16_t sl_wakeupCntr = 0;
2017-08-12 16:55:20 +01:00
/*********************************************************************************************\
* Sonoff B1 (my9231) and AiLight (my9291) based on 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 sl_pdi;
uint8_t sl_pdcki;
2017-08-12 16:55:20 +01:00
2017-08-13 10:19:34 +01:00
void sl_di_pulse(uint8_t times)
{
2017-08-13 10:19:34 +01:00
for (uint8_t i = 0; i < times; i++) {
digitalWrite(sl_pdi, HIGH);
digitalWrite(sl_pdi, LOW);
}
}
2017-08-13 10:19:34 +01:00
void sl_dcki_pulse(uint8_t times)
2017-08-12 16:55:20 +01:00
{
2017-08-13 10:19:34 +01:00
for (uint8_t i = 0; i < times; i++) {
digitalWrite(sl_pdcki, HIGH);
digitalWrite(sl_pdcki, LOW);
2017-08-12 16:55:20 +01:00
}
}
void sl_my92x1_command(uint8_t chips, uint8_t command)
2017-08-12 16:55:20 +01:00
{
uint8_t command_data;
// ets_intr_lock();
2017-08-13 10:19:34 +01:00
os_delay_us(12); // TStop > 12us.
2017-08-12 16:55:20 +01:00
// Send 12 DI pulse, after 6 pulse's falling edge store duty data, and 12
// pulse's rising edge convert to command mode.
sl_di_pulse(12);
os_delay_us(12); // Delay >12us, begin send CMD data
for (uint8_t n = 0; n < chips; n++) { // Send CMD data
2017-08-12 16:55:20 +01:00
command_data = command;
2017-08-13 10:19:34 +01:00
for (uint8_t i = 0; i < 4; i++) { // Send byte
digitalWrite(sl_pdcki, LOW);
digitalWrite(sl_pdi, (command_data & 0x80));
digitalWrite(sl_pdcki, HIGH);
2017-08-12 16:55:20 +01:00
command_data = command_data << 1;
2017-08-13 10:19:34 +01:00
digitalWrite(sl_pdi, (command_data & 0x80));
digitalWrite(sl_pdcki, LOW);
digitalWrite(sl_pdi, LOW);
2017-08-12 16:55:20 +01:00
command_data = command_data << 1;
}
}
2017-08-13 10:19:34 +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.
sl_di_pulse(16);
2017-08-13 10:19:34 +01:00
os_delay_us(12); // TStop > 12us.
2017-08-12 16:55:20 +01:00
// ets_intr_unlock();
}
void sl_my9231_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t duty_w, uint16_t duty_c)
2017-08-12 16:55:20 +01:00
{
uint16_t duty_current = 0;
2017-08-13 10:19:34 +01:00
uint16_t duty[6] = { duty_r, duty_g, duty_b, duty_w, duty_c, 0 }; // Definition for RGBWC channels
2017-08-12 16:55:20 +01:00
// ets_intr_lock();
2017-08-13 10:19:34 +01:00
os_delay_us(12); // TStop > 12us.
for (uint8_t channel = 0; channel < 6; channel++) { // RGBWC0 6CH
duty_current = duty[channel]; // RGBWC Channel
for (uint8_t i = 0; i < 4; i++) { // Send 8bit Data
digitalWrite(sl_pdcki, LOW);
digitalWrite(sl_pdi, (duty_current & 0x80));
digitalWrite(sl_pdcki, HIGH);
2017-08-12 16:55:20 +01:00
duty_current = duty_current << 1;
2017-08-13 10:19:34 +01:00
digitalWrite(sl_pdi, (duty_current & 0x80));
digitalWrite(sl_pdcki, LOW);
digitalWrite(sl_pdi, LOW);
2017-08-12 16:55:20 +01:00
duty_current = duty_current << 1;
}
}
2017-08-13 10:19:34 +01:00
os_delay_us(12); // TStart > 12us. Ready for send DI pulse.
sl_di_pulse(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
// ets_intr_unlock();
}
void sl_my9291_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t duty_w)
{
uint16_t duty_current = 0;
uint16_t duty[4] = { duty_r, duty_g, duty_b, duty_w }; // Definition for RGBW channels
// ets_intr_lock();
os_delay_us(12); // TStop > 12us.
for (uint8_t channel = 0; channel < 4; channel++) { // RGBW 4CH
duty_current = duty[channel]; // RGBW Channel
for (uint8_t i = 0; i < 4; i++) { // Send 8bit Data
digitalWrite(sl_pdcki, LOW);
digitalWrite(sl_pdi, (duty_current & 0x80));
digitalWrite(sl_pdcki, HIGH);
duty_current = duty_current << 1;
digitalWrite(sl_pdi, (duty_current & 0x80));
digitalWrite(sl_pdcki, LOW);
digitalWrite(sl_pdi, LOW);
duty_current = duty_current << 1;
}
}
os_delay_us(12); // TStart > 12us. Ready for send DI pulse.
sl_di_pulse(8); // Send 8 DI pulse. After 8 pulse falling edge, store old data.
os_delay_us(12); // TStop > 12us.
// ets_intr_unlock();
}
2017-08-12 16:55:20 +01:00
/********************************************************************************************/
void sl_init(void)
{
2017-08-12 16:55:20 +01:00
pin[GPIO_WS2812] = 99; // I do not allow both Sonoff Led AND WS2812 led
if (sfl_flg < 4) {
2017-08-12 16:55:20 +01:00
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);
}
sysCfg.pwmvalue[0] = 0; // We use dimmer / led_color
if (2 == sfl_flg) {
sysCfg.pwmvalue[1] = 0; // We use led_color
}
} else {
2017-08-13 10:19:34 +01:00
sl_pdi = pin[GPIO_DI];
sl_pdcki = pin[GPIO_DCKI];
pinMode(sl_pdi, OUTPUT);
pinMode(sl_pdcki, OUTPUT);
digitalWrite(sl_pdi, LOW);
digitalWrite(sl_pdcki, LOW);
2017-08-12 16:55:20 +01:00
if (4 == sfl_flg) {
// Clear all duty register
sl_dcki_pulse(32); // 1 * 32 bits
sl_my92x1_command(1, 0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM
} else if (5 == sfl_flg) {
// Clear all duty register
sl_dcki_pulse(48); // 2 * 24 bits
sl_my92x1_command(2, 0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM
}
}
2017-08-12 16:55:20 +01:00
sl_power = 0;
sl_any = 0;
sl_wakeupActive = 0;
}
2017-08-12 16:55:20 +01:00
void sl_setDim(uint8_t myDimmer)
{
2017-08-12 16:55:20 +01:00
float temp;
if ((1 == sfl_flg) && (100 == myDimmer)) {
myDimmer = 99; // BN-SZ01 starts flickering at dimmer = 100
}
float newDim = 100 / (float)myDimmer;
for (byte i = 0; i < sfl_flg; i++) {
temp = (float)sysCfg.led_color[i] / newDim;
sl_dcolor[i] = (uint8_t)temp;
}
}
void sl_setColor()
{
uint8_t highest = 0;
float temp;
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
if (highest < sl_dcolor[i]) {
highest = sl_dcolor[i];
}
}
2017-08-12 16:55:20 +01:00
float mDim = (float)highest / 2.55;
sysCfg.led_dimmer[0] = (uint8_t)mDim;
float newDim = 100 / mDim;
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
temp = (float)sl_dcolor[i] * newDim;
sysCfg.led_color[i] = (uint8_t)temp;
}
}
char* sl_getColor(char* scolor)
{
sl_setDim(sysCfg.led_dimmer[0]);
scolor[0] = '\0';
for (byte i = 0; i < sfl_flg; i++) {
snprintf_P(scolor, 11, PSTR("%s%02X"), scolor, sl_dcolor[i]);
}
return scolor;
}
void sl_prepPower(char *svalue, uint16_t ssvalue)
{
2017-08-12 16:55:20 +01:00
char scolor[11];
// do_cmnd_power(index, (sysCfg.led_dimmer[0]>0));
if (sysCfg.led_dimmer[0] && !(power&1)) {
do_cmnd_power(1, 7); // No publishPowerState
}
else if (!sysCfg.led_dimmer[0] && (power&1)) {
do_cmnd_power(1, 6); // No publishPowerState
}
#ifdef USE_DOMOTICZ
mqtt_publishDomoticzPowerState(1);
#endif // USE_DOMOTICZ
2017-08-12 16:55:20 +01:00
if (sfl_flg > 1) {
snprintf_P(svalue, ssvalue, PSTR("{\"POWER\":\"%s\", \"Dimmer\":%d, \"Color\":\"%s\"}"),
getStateText(power &1), sysCfg.led_dimmer[0], sl_getColor(scolor));
} else {
snprintf_P(svalue, ssvalue, PSTR("{\"POWER\":\"%s\", \"Dimmer\":%d}"),
getStateText(power &1), sysCfg.led_dimmer[0]);
}
}
void sl_setPower(uint8_t power)
{
sl_power = power &1;
if (sl_wakeupActive) {
sl_wakeupActive--;
}
sl_animate();
}
void sl_animate()
{
// {"Wakeup":"Done"}
char svalue[32]; // was MESSZ
uint8_t fadeValue;
2017-08-12 16:55:20 +01:00
uint8_t cur_col[5];
if (0 == sl_power) { // Power Off
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
sl_tcolor[i] = 0;
}
}
else {
if (!sl_wakeupActive) { // Power On
sl_setDim(sysCfg.led_dimmer[0]);
if (0 == sysCfg.led_fade) {
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
sl_tcolor[i] = sl_dcolor[i];
}
2017-08-12 16:55:20 +01:00
} else {
for (byte i = 0; i < sfl_flg; i++) {
if (sl_tcolor[i] != sl_dcolor[i]) {
if (sl_tcolor[i] < sl_dcolor[i]) {
sl_tcolor[i] += ((sl_dcolor[i] - sl_tcolor[i]) >> sysCfg.led_speed) +1;
}
if (sl_tcolor[i] > sl_dcolor[i]) {
sl_tcolor[i] -= ((sl_tcolor[i] - sl_dcolor[i]) >> sysCfg.led_speed) +1;
}
}
}
}
} else { // Power On using wake up duration
if (2 == sl_wakeupActive) {
sl_wakeupActive = 1;
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
sl_tcolor[i] = 0;
}
sl_wakeupCntr = 0;
sl_wakeupDimmer = 0;
}
sl_wakeupCntr++;
if (sl_wakeupCntr > ((sysCfg.led_wakeup * STATES) / sysCfg.led_dimmer[0])) {
sl_wakeupCntr = 0;
sl_wakeupDimmer++;
if (sl_wakeupDimmer <= sysCfg.led_dimmer[0]) {
sl_setDim(sl_wakeupDimmer);
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
sl_tcolor[i] = sl_dcolor[i];
}
} else {
snprintf_P(svalue, sizeof(svalue), PSTR("{\"Wakeup\":\"Done\"}"));
mqtt_publish_topic_P(2, PSTR("WAKEUP"), svalue);
sl_wakeupActive = 0;
}
}
}
}
2017-08-12 16:55:20 +01:00
for (byte i = 0; i < sfl_flg; i++) {
if (sl_lcolor[i] != sl_tcolor[i]) {
sl_any = 1;
}
}
if (sl_any) {
sl_any = 0;
for (byte i = 0; i < sfl_flg; i++) {
2017-08-12 16:55:20 +01:00
sl_lcolor[i] = sl_tcolor[i];
cur_col[i] = (sysCfg.led_table) ? ledTable[sl_lcolor[i]] : sl_lcolor[i];
if (sfl_flg < 4) {
2017-08-12 16:55:20 +01:00
if (pin[GPIO_PWM1 +i] < 99) {
analogWrite(pin[GPIO_PWM1 +i], cur_col[i] * (PWM_RANGE / 255));
}
}
}
if (4 == sfl_flg) {
sl_my9291_duty(cur_col[0], cur_col[1], cur_col[2], cur_col[3]);
}
else if (5 == sfl_flg) {
sl_my9231_duty(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
\*********************************************************************************************/
2017-08-12 16:55:20 +01:00
void sl_rgb2hsb(float *hue, float *sat, float *bri)
{
sl_setDim(sysCfg.led_dimmer[0]);
float r = (float)(sl_dcolor[0] / 255.0f);
float g = (float)(sl_dcolor[1] / 255.0f);
float b = (float)(sl_dcolor[2] / 255.0f);
float max = fmax(fmax(r, g), b);
float min = fmin(fmin(r, g), b);
*bri = (max + min) / 2.0f;
if (max == min) {
*hue = *sat = 0.0f;
} else {
float d = max - min;
*sat = (*bri > 0.5f) ? d / (2.0f - max - min) : d / (max + min);
if (r > g && r > b) {
*hue = (g - b) / d + (g < b ? 6.0f : 0.0f);
}
else if (g > b) {
*hue = (b - r) / d + 2.0f;
}
else {
*hue = (r - g) / d + 4.0f;
}
*hue /= 6.0f;
}
}
float sl_hue2rgb(float p, float q, float t)
{
if (t < 0.0f) {
t += 1.0f;
}
if (t > 1.0f) {
t -= 1.0f;
}
if (t < 1.0f / 6.0f) {
return p + (q - p) * 6.0f * t;
}
if (t < 1.0f / 2.0f) {
return q;
}
if (t < 2.0f / 3.0f) {
return p + (q - p) * (2.0f / 3.0f - t) * 6.0f;
}
return p;
}
void sl_hsb2rgb(float hue, float sat, float bri)
{
float r;
float g;
float b;
if (sat == 0.0f) {
r = g = b = bri;
} else {
float q = bri < 0.5f ? bri * (1.0f + sat) : bri + sat - bri * sat;
float p = 2.0f * bri - q;
r = sl_hue2rgb(p, q, hue + 1.0f / 3.0f);
g = sl_hue2rgb(p, q, hue);
b = sl_hue2rgb(p, q, hue - 1.0f / 3.0f);
}
sl_dcolor[0] = (uint8_t)(r * 255 + 0.5f);
sl_dcolor[1] = (uint8_t)(g * 255 + 0.5f);
sl_dcolor[2] = (uint8_t)(b * 255 + 0.5f);
sl_setColor();
}
/********************************************************************************************/
void sl_replaceHSB(String *response)
{
2017-08-12 16:55:20 +01:00
float hue;
float sat;
float bri;
if (sfl_flg > 2) {
2017-08-12 16:55:20 +01:00
sl_rgb2hsb(&hue, &sat, &bri);
response->replace("{h}", String((uint16_t)(65535.0f * hue)));
response->replace("{s}", String((uint8_t)(254.0f * sat)));
response->replace("{b}", String((uint8_t)(254.0f * bri)));
} else {
response->replace("{h}", "0");
response->replace("{s}", "0");
response->replace("{b}", String((uint8_t)(2.54f * (float)sysCfg.led_dimmer[0])));
}
}
void sl_getHSB(float *hue, float *sat, float *bri)
{
if (sfl_flg > 2) {
2017-08-12 16:55:20 +01:00
sl_rgb2hsb(hue, sat, bri);
} else {
*hue = 0;
*sat = 0;
*bri = (2.54f * (float)sysCfg.led_dimmer[0]);
}
}
void sl_setHSB(float hue, float sat, float bri)
{
char svalue[MESSZ];
/*
2017-08-12 16:55:20 +01:00
char log[LOGSZ];
char stemp1[10];
char stemp2[10];
char stemp3[10];
dtostrf(hue, 1, 3, stemp1);
dtostrf(sat, 1, 3, stemp2);
dtostrf(bri, 1, 3, stemp3);
snprintf_P(log, sizeof(log), PSTR("LED: Hue %s, Sat %s, Bri %s"), stemp1, stemp2, stemp3);
addLog(LOG_LEVEL_DEBUG, log);
*/
if (sfl_flg > 2) {
2017-08-12 16:55:20 +01:00
sl_hsb2rgb(hue, sat, bri);
sl_prepPower(svalue, sizeof(svalue));
mqtt_publish_topic_P(5, "COLOR", svalue);
} else {
uint8_t tmp = (uint8_t)(bri * 100);
sysCfg.led_dimmer[0] = tmp;
sl_prepPower(svalue, sizeof(svalue));
mqtt_publish_topic_P(5, "DIMMER", svalue);
}
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
boolean sl_command(char *type, uint16_t index, char *dataBufUc, uint16_t data_len, int16_t payload, char *svalue, uint16_t ssvalue)
{
boolean serviced = true;
boolean coldim = false;
2017-08-12 16:55:20 +01:00
char scolor[11];
char *p;
2017-08-12 16:55:20 +01:00
if ((sfl_flg > 1) && !strcmp_P(type,PSTR("COLOR"))) {
if ((2 * sfl_flg) == data_len) {
for (byte i = 0; i < sfl_flg; i++) {
strlcpy(scolor, dataBufUc + (i *2), 3);
sl_dcolor[i] = (uint8_t)strtol(scolor, &p, 16);
}
sl_setColor();
coldim = true;
} else {
2017-08-12 16:55:20 +01:00
snprintf_P(svalue, ssvalue, PSTR("{\"Color\":\"%s\"}"), sl_getColor(scolor));
}
}
else if (!strcmp_P(type,PSTR("DIMMER"))) {
if ((payload >= 0) && (payload <= 100)) {
sysCfg.led_dimmer[0] = payload;
coldim = true;
} else {
snprintf_P(svalue, ssvalue, PSTR("{\"Dimmer\":%d}"), sysCfg.led_dimmer[0]);
}
}
else if (!strcmp_P(type,PSTR("LEDTABLE"))) {
if ((payload >= 0) && (payload <= 2)) {
switch (payload) {
case 0: // Off
case 1: // On
sysCfg.led_table = payload;
break;
case 2: // Toggle
sysCfg.led_table ^= 1;
break;
}
sl_any = 1;
}
snprintf_P(svalue, ssvalue, PSTR("{\"LedTable\":\"%s\"}"), getStateText(sysCfg.led_table));
}
else if (!strcmp_P(type,PSTR("FADE"))) {
switch (payload) {
case 0: // Off
case 1: // On
sysCfg.led_fade = payload;
break;
case 2: // Toggle
sysCfg.led_fade ^= 1;
break;
}
snprintf_P(svalue, ssvalue, PSTR("{\"Fade\":\"%s\"}"), getStateText(sysCfg.led_fade));
}
else if (!strcmp_P(type,PSTR("SPEED"))) { // 1 - fast, 8 - slow
if ((payload > 0) && (payload <= 8)) {
sysCfg.led_speed = payload;
}
snprintf_P(svalue, ssvalue, PSTR("{\"Speed\":%d}"), sysCfg.led_speed);
}
else if (!strcmp_P(type,PSTR("WAKEUPDURATION"))) {
if ((payload > 0) && (payload < 3001)) {
sysCfg.led_wakeup = payload;
sl_wakeupActive = 0;
}
snprintf_P(svalue, ssvalue, PSTR("{\"WakeUpDuration\":%d}"), sysCfg.led_wakeup);
}
else if (!strcmp_P(type,PSTR("WAKEUP"))) {
sl_wakeupActive = 3;
do_cmnd_power(1, 1);
snprintf_P(svalue, ssvalue, PSTR("{\"Wakeup\":\"Started\"}"));
}
else {
serviced = false; // Unknown command
}
if (coldim) {
sl_prepPower(svalue, ssvalue);
}
return serviced;
}