From 941deba8152118074f006cbe138c1891addfb37b Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Sun, 13 Aug 2017 11:19:34 +0200 Subject: [PATCH 1/5] Still testing Sonoff B1 --- sonoff/_releasenotes.ino | 2 +- sonoff/sonoff.ino | 2 +- sonoff/xdrv_snfled.ino | 120 ++++++++++++++++----------------------- 3 files changed, 52 insertions(+), 72 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index daa933601..f85a28622 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,4 +1,4 @@ -/* 5.5.2c +/* 5.5.2d * Fix Sonoff Pow intermittent exception 0 * Change Sonoff Pow sending Domoticz telemetry data only * Add Sonoff B1 support (experimental) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 8d99a51f3..86d85ae11 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x05050203 // 5.5.2c +#define VERSION 0x05050204 // 5.5.2d enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; enum week_t {Last, First, Second, Third, Fourth}; diff --git a/sonoff/xdrv_snfled.ino b/sonoff/xdrv_snfled.ino index d44139255..b8d7a74a0 100644 --- a/sonoff/xdrv_snfled.ino +++ b/sonoff/xdrv_snfled.ino @@ -50,24 +50,29 @@ uint8_t sl_wakeupDimmer = 0; uint16_t sl_wakeupCntr = 0; /*********************************************************************************************\ - * Sonoff B1 based on OpenLight https://github.com/icamgo/noduino-sdk + * Sonoff B1 based on OpenLight https://github.com/icamgo/noduino-sdk (my9231 and my9291) \*********************************************************************************************/ -uint8_t sl_last_command; +extern "C" { + void os_delay_us(unsigned int); +} -void sl_di_pulse(byte times) +uint8_t sl_pdi; +uint8_t sl_pdcki; + +void sl_di_pulse(uint8_t times) { - for (byte i = 0; i < times; i++) { - digitalWrite(pin[GPIO_DI], HIGH); - digitalWrite(pin[GPIO_DI], LOW); + for (uint8_t i = 0; i < times; i++) { + digitalWrite(sl_pdi, HIGH); + digitalWrite(sl_pdi, LOW); } } -void sl_dcki_pulse(byte times) +void sl_dcki_pulse(uint8_t times) { - for (byte i = 0; i < times; i++) { - digitalWrite(pin[GPIO_DCKI], HIGH); - digitalWrite(pin[GPIO_DCKI], LOW); + for (uint8_t i = 0; i < times; i++) { + digitalWrite(sl_pdcki, HIGH); + digitalWrite(sl_pdcki, LOW); } } @@ -75,84 +80,57 @@ void sl_send_command(uint8_t command) { uint8_t command_data; - sl_last_command = command; - // ets_intr_lock(); - delayMicroseconds(12); // TStop > 12us. + os_delay_us(12); // TStop > 12us. // 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); - delayMicroseconds(12); // Delay >12us, begin send CMD data - - for (byte n = 0; n < 2; n++) { // Send CMD data + os_delay_us(12); // Delay >12us, begin send CMD data + for (uint8_t n = 0; n < 2; n++) { // Send CMD data command_data = command; - - for (byte i = 0; i < 4; i++) { // Send byte - digitalWrite(pin[GPIO_DCKI], LOW); - if (command_data & 0x80) { - digitalWrite(pin[GPIO_DI], HIGH); - } else { - digitalWrite(pin[GPIO_DI], LOW); - } - -// digitalWrite(pin[GPIO_DI], (command_data & 0x80)); - - digitalWrite(pin[GPIO_DCKI], HIGH); + for (uint8_t i = 0; i < 4; i++) { // Send byte + digitalWrite(sl_pdcki, LOW); + digitalWrite(sl_pdi, (command_data & 0x80)); + digitalWrite(sl_pdcki, HIGH); command_data = command_data << 1; - if (command_data & 0x80) { - digitalWrite(pin[GPIO_DI], HIGH); - } else { - digitalWrite(pin[GPIO_DI], LOW); - } - digitalWrite(pin[GPIO_DCKI], LOW); - digitalWrite(pin[GPIO_DI], LOW); + digitalWrite(sl_pdi, (command_data & 0x80)); + digitalWrite(sl_pdcki, LOW); + digitalWrite(sl_pdi, LOW); command_data = command_data << 1; } } - - delayMicroseconds(12); // TStart > 12us. Delay 12 us. + os_delay_us(12); // TStart > 12us. Delay 12 us. // 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); - delayMicroseconds(12); // TStop > 12us. + os_delay_us(12); // TStop > 12us. // ets_intr_unlock(); } void sl_send_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t duty_w, uint16_t duty_c) { - uint8_t bit_length = 8; uint16_t duty_current = 0; - uint16_t duty[8] = { duty_r, duty_g, duty_b, 0, duty_w, duty_c, 0, 0 }; // Definition for RGBWC channels + uint16_t duty[6] = { duty_r, duty_g, duty_b, duty_w, duty_c, 0 }; // Definition for RGBWC channels // ets_intr_lock(); - delayMicroseconds(12); // TStop > 12us. - - for (byte channel = 0; channel < 8; channel++) { // RGB0WC00 8CH - duty_current = duty[channel]; // RGBWC Channel - for (byte i = 0; i < bit_length / 2; i++) { // Send 8bit/12bit/14bit/16bit Data - digitalWrite(pin[GPIO_DCKI], LOW); - if (duty_current & (0x01 << (bit_length - 1))) { - digitalWrite(pin[GPIO_DI], HIGH); - } else { - digitalWrite(pin[GPIO_DI], LOW); - } - digitalWrite(pin[GPIO_DCKI], HIGH); + 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); duty_current = duty_current << 1; - if (duty_current & (0x01 << (bit_length - 1))) { - digitalWrite(pin[GPIO_DI], HIGH); - } else { - digitalWrite(pin[GPIO_DI], LOW); - } - digitalWrite(pin[GPIO_DCKI], LOW); - digitalWrite(pin[GPIO_DI], LOW); + digitalWrite(sl_pdi, (duty_current & 0x80)); + digitalWrite(sl_pdcki, LOW); + digitalWrite(sl_pdi, LOW); duty_current = duty_current << 1; } } - - delayMicroseconds(12); // TStart > 12us. Ready for send DI pulse. - sl_di_pulse(8); // Send 8 DI pulse. After 8 pulse falling edge, store old data. - delayMicroseconds(12); // TStop > 12us. + 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(); } @@ -179,17 +157,20 @@ void sl_init(void) sysCfg.pwmvalue[1] = 0; // We use led_color } } else { - pinMode(pin[GPIO_DI], OUTPUT); - pinMode(pin[GPIO_DCKI], OUTPUT); - digitalWrite(pin[GPIO_DI], LOW); - digitalWrite(pin[GPIO_DCKI], LOW); + 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); // Clear all duty register sl_dcki_pulse(64); sl_send_command(0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM // Test - sl_send_duty(16, 0, 0, 0, 0); // Red +// sl_send_duty(16, 0, 0, 0, 0); // Red } sl_power = 0; @@ -463,7 +444,6 @@ void sl_setHSB(float hue, float sat, float bri) { char svalue[MESSZ]; -/* char log[LOGSZ]; char stemp1[10]; char stemp2[10]; @@ -473,7 +453,7 @@ void sl_setHSB(float hue, float sat, float bri) 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 (5 == sfl_flg) { sl_hsb2rgb(hue, sat, bri); sl_prepPower(svalue, sizeof(svalue)); From 700e51b0914816d0fe4277e58338122422549efa Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Sun, 13 Aug 2017 13:11:44 +0200 Subject: [PATCH 2/5] Add Sonoff B1 b using different pins --- sonoff/_releasenotes.ino | 2 +- sonoff/sonoff.ino | 4 ++-- sonoff/sonoff_template.h | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index f85a28622..3cdd9e4ec 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,4 +1,4 @@ -/* 5.5.2d +/* 5.5.2e * Fix Sonoff Pow intermittent exception 0 * Change Sonoff Pow sending Domoticz telemetry data only * Add Sonoff B1 support (experimental) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 86d85ae11..5ae0e505e 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x05050204 // 5.5.2d +#define VERSION 0x05050205 // 5.5.2e enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; enum week_t {Last, First, Second, Third, Fourth}; @@ -2633,7 +2633,7 @@ void GPIO_init() else if (SONOFF_LED == sysCfg.module) { sfl_flg = 2; } - else if (SONOFF_B1 == sysCfg.module) { + else if ((SONOFF_B1 == sysCfg.module) || (SONOFF_B1b == sysCfg.module)) { sfl_flg = 5; } else { diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 7ceea9263..0c1a1a569 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -152,6 +152,7 @@ enum module_t { HUAFAN_SS, SONOFF_BRIDGE, SONOFF_B1, + SONOFF_B1b, MAXMODULE }; /********************************************************************************************/ @@ -507,7 +508,22 @@ const mytmplt modules[MAXMODULE] PROGMEM = { GPIO_DCKI, // GPIO14 my9231 DCKI 0, 0, 0 + }, + { "Sonoff B1 b", // Sonoff B1 (ESP8285 - my9231) + GPIO_KEY1, // GPIO00 Pad + GPIO_USER, // GPIO01 Serial RXD and Optional sensor pad + GPIO_USER, // GPIO02 Optional sensor SDA pad + GPIO_USER, // GPIO03 Serial TXD and Optional sensor pad + 0, 0, + 0, 0, 0, // Flash connection + 0, 0, + 0, // Flash connection + 0, + GPIO_DI, // GPIO13 my9231 DI + 0, + GPIO_DCKI, // GPIO15 my9231 DCKI + 0, 0 } - + }; From 5fdc5e890a62871c57a640d8532929e2f28368c8 Mon Sep 17 00:00:00 2001 From: arendst Date: Sun, 13 Aug 2017 15:31:49 +0200 Subject: [PATCH 3/5] Add AiLight and update Sonoff B1 (experimental) --- sonoff/_releasenotes.ino | 4 +-- sonoff/sonoff.ino | 17 +++++---- sonoff/sonoff_template.h | 11 +++--- sonoff/xdrv_snfled.ino | 75 +++++++++++++++++++++++++++++----------- 4 files changed, 71 insertions(+), 36 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 3cdd9e4ec..ac7a5ceb2 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,7 +1,7 @@ -/* 5.5.2e +/* 5.5.2f * Fix Sonoff Pow intermittent exception 0 * Change Sonoff Pow sending Domoticz telemetry data only - * Add Sonoff B1 support (experimental) + * Add Ai-Thinker RGBW led (AiLight) and Sonoff B1 support (experimental) * * 5.5.2 20170808 * Extent max number of WS2812 pixels from 256 to 512 (#667) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 5ae0e505e..5b559f964 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x05050205 // 5.5.2e +#define VERSION 0x05050206 // 5.5.2f enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; enum week_t {Last, First, Second, Third, Fourth}; @@ -2318,7 +2318,7 @@ void stateloop() button_handler(); switch_handler(); - if (sfl_flg) { // Sonoff B1, led or BN-SZ01 + if (sfl_flg) { // Sonoff B1, AiLight, Sonoff led or BN-SZ01 sl_animate(); } @@ -2627,13 +2627,16 @@ void GPIO_init() Maxdevice = 0; Baudrate = 19200; } - else if (SONOFF_BN == sysCfg.module) { + else if (SONOFF_BN == sysCfg.module) { // Single color led (White) sfl_flg = 1; } - else if (SONOFF_LED == sysCfg.module) { + else if (SONOFF_LED == sysCfg.module) { // Dual color led (White warm and cold) sfl_flg = 2; } - else if ((SONOFF_B1 == sysCfg.module) || (SONOFF_B1b == sysCfg.module)) { + else if (AILIGHT == sysCfg.module) { // RGBW led + sfl_flg = 4; + } + else if (SONOFF_B1 == sysCfg.module) { // RGBWC led sfl_flg = 5; } else { @@ -2663,8 +2666,8 @@ void GPIO_init() } } - if (sfl_flg) { // Sonoff B1, Led or BN-SZ01 - if (sfl_flg < 5) { + if (sfl_flg) { // Sonoff B1, AiLight, Sonoff Led or BN-SZ01 + if (sfl_flg < 4) { pwm_idxoffset = sfl_flg; // 1 for BN-SZ01, 2 for Sonoff Led } sl_init(); diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 0c1a1a569..bd7e2cadc 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -152,7 +152,7 @@ enum module_t { HUAFAN_SS, SONOFF_BRIDGE, SONOFF_B1, - SONOFF_B1b, + AILIGHT, MAXMODULE }; /********************************************************************************************/ @@ -506,10 +506,9 @@ const mytmplt modules[MAXMODULE] PROGMEM = { GPIO_DI, // GPIO12 my9231 DI 0, GPIO_DCKI, // GPIO14 my9231 DCKI - 0, - 0, 0 + 0, 0, 0 }, - { "Sonoff B1 b", // Sonoff B1 (ESP8285 - my9231) + { "AiLight", // Ai-Thinker RGBW led (ESP8266 - my9291) GPIO_KEY1, // GPIO00 Pad GPIO_USER, // GPIO01 Serial RXD and Optional sensor pad GPIO_USER, // GPIO02 Optional sensor SDA pad @@ -519,9 +518,9 @@ const mytmplt modules[MAXMODULE] PROGMEM = { 0, 0, 0, // Flash connection 0, - GPIO_DI, // GPIO13 my9231 DI + GPIO_DI, // GPIO13 my9291 DI 0, - GPIO_DCKI, // GPIO15 my9231 DCKI + GPIO_DCKI, // GPIO15 my9291 DCKI 0, 0 } diff --git a/sonoff/xdrv_snfled.ino b/sonoff/xdrv_snfled.ino index b8d7a74a0..8161a077b 100644 --- a/sonoff/xdrv_snfled.ino +++ b/sonoff/xdrv_snfled.ino @@ -18,7 +18,7 @@ */ /*********************************************************************************************\ - * Sonoff B1, Led and BN-SZ01 + * Sonoff B1, AiLight, Sonoff Led and BN-SZ01 \*********************************************************************************************/ uint8_t ledTable[] = { @@ -50,7 +50,7 @@ uint8_t sl_wakeupDimmer = 0; uint16_t sl_wakeupCntr = 0; /*********************************************************************************************\ - * Sonoff B1 based on OpenLight https://github.com/icamgo/noduino-sdk (my9231 and my9291) + * Sonoff B1 (my9231) and AiLight (my9291) based on OpenLight https://github.com/icamgo/noduino-sdk \*********************************************************************************************/ extern "C" { @@ -76,7 +76,7 @@ void sl_dcki_pulse(uint8_t times) } } -void sl_send_command(uint8_t command) +void sl_my92x1_command(uint8_t chips, uint8_t command) { uint8_t command_data; @@ -85,8 +85,8 @@ void sl_send_command(uint8_t command) // 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 < 2; n++) { // Send CMD data + os_delay_us(12); // Delay >12us, begin send CMD data + for (uint8_t n = 0; n < chips; n++) { // Send CMD data command_data = command; for (uint8_t i = 0; i < 4; i++) { // Send byte digitalWrite(sl_pdcki, LOW); @@ -107,7 +107,7 @@ void sl_send_command(uint8_t command) // ets_intr_unlock(); } -void sl_send_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t duty_w, uint16_t duty_c) +void sl_my9231_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t duty_w, uint16_t duty_c) { uint16_t duty_current = 0; @@ -134,12 +134,39 @@ void sl_send_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t du // 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(); +} + /********************************************************************************************/ void sl_init(void) { pin[GPIO_WS2812] = 99; // I do not allow both Sonoff Led AND WS2812 led - if (sfl_flg < 5) { + if (sfl_flg < 4) { if (!my_module.gp.io[4]) { pinMode(4, OUTPUT); // Stop floating outputs digitalWrite(4, LOW); @@ -165,12 +192,15 @@ void sl_init(void) digitalWrite(sl_pdi, LOW); digitalWrite(sl_pdcki, LOW); - // Clear all duty register - sl_dcki_pulse(64); - sl_send_command(0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM - - // Test -// sl_send_duty(16, 0, 0, 0, 0); // Red + 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 + } } sl_power = 0; @@ -320,14 +350,17 @@ void sl_animate() for (byte i = 0; i < sfl_flg; i++) { sl_lcolor[i] = sl_tcolor[i]; cur_col[i] = (sysCfg.led_table) ? ledTable[sl_lcolor[i]] : sl_lcolor[i]; - if (sfl_flg < 5) { + if (sfl_flg < 4) { if (pin[GPIO_PWM1 +i] < 99) { analogWrite(pin[GPIO_PWM1 +i], cur_col[i] * (PWM_RANGE / 255)); } } } - if (5 == sfl_flg) { - sl_send_duty(cur_col[0], cur_col[1], cur_col[2], cur_col[3], cur_col[4]); + 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]); } } } @@ -417,7 +450,7 @@ void sl_replaceHSB(String *response) float sat; float bri; - if (5 == sfl_flg) { + if (sfl_flg > 2) { sl_rgb2hsb(&hue, &sat, &bri); response->replace("{h}", String((uint16_t)(65535.0f * hue))); response->replace("{s}", String((uint8_t)(254.0f * sat))); @@ -431,7 +464,7 @@ void sl_replaceHSB(String *response) void sl_getHSB(float *hue, float *sat, float *bri) { - if (5 == sfl_flg) { + if (sfl_flg > 2) { sl_rgb2hsb(hue, sat, bri); } else { *hue = 0; @@ -443,7 +476,7 @@ void sl_getHSB(float *hue, float *sat, float *bri) void sl_setHSB(float hue, float sat, float bri) { char svalue[MESSZ]; - +/* char log[LOGSZ]; char stemp1[10]; char stemp2[10]; @@ -453,8 +486,8 @@ void sl_setHSB(float hue, float sat, float bri) 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 (5 == sfl_flg) { +*/ + if (sfl_flg > 2) { sl_hsb2rgb(hue, sat, bri); sl_prepPower(svalue, sizeof(svalue)); mqtt_publish_topic_P(5, "COLOR", svalue); From 99c9b1671c2b2625e23ba63592b34445ebb5bb78 Mon Sep 17 00:00:00 2001 From: arendst Date: Tue, 15 Aug 2017 22:09:52 +0200 Subject: [PATCH 4/5] Add Sonoff B1 Support. AiLight is experimental --- sonoff/_releasenotes.ino | 6 +- sonoff/sonoff.ino | 3 +- sonoff/sonoff_template.h | 4 +- sonoff/xdrv_snfled.ino | 155 ++++++++++++++++----------------------- sonoff/xdrv_wemohue.ino | 23 +++++- sonoff/xdrv_ws2812.ino | 68 +++++++++-------- 6 files changed, 128 insertions(+), 131 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index ac7a5ceb2..cc9d81c0b 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,7 +1,9 @@ -/* 5.5.2f +/* 5.5.2g * Fix Sonoff Pow intermittent exception 0 * Change Sonoff Pow sending Domoticz telemetry data only - * Add Ai-Thinker RGBW led (AiLight) and Sonoff B1 support (experimental) + * Add Sonoff B1 support + * Add Ai-Thinker RGBW led (AiLight) (experimental) + * Add NeoPixelBus library to Sonoff Led for Hue support (saves 1k1 code space) * * 5.5.2 20170808 * Extent max number of WS2812 pixels from 256 to 512 (#667) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 5b559f964..97a0c15bc 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x05050206 // 5.5.2f +#define VERSION 0x05050207 // 5.5.2g enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; enum week_t {Last, First, Second, Third, Fourth}; @@ -172,6 +172,7 @@ enum opt_t {P_HOLD_TIME, P_MAX_POWER_RETRY, P_MAX_PARAM8}; // Index in sysCf #include // Ota #include // Webserver, Updater #include // WemoHue, IRremote, Domoticz +#include // Ws2812, Sonoff Led hue support #ifdef USE_WEBSERVER #include // WifiManager, Webserver #include // WifiManager diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index bd7e2cadc..333ed89e9 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -117,9 +117,9 @@ enum fpins_t { GPIO_HLW_CF1, // HLW8012 CF1 voltage / current (Sonoff Pow) GPIO_HLW_CF, // HLW8012 CF power (Sonoff Pow) GPIO_ADC0, // ADC + GPIO_DI, // my92x1 PWM input + GPIO_DCKI, // my92x1 CLK input GPIO_USER, // User configurable - GPIO_DI, // my9231 PWM input - GPIO_DCKI, // my9231 CLK input GPIO_MAX }; /********************************************************************************************/ diff --git a/sonoff/xdrv_snfled.ino b/sonoff/xdrv_snfled.ino index 8161a077b..525c245f4 100644 --- a/sonoff/xdrv_snfled.ino +++ b/sonoff/xdrv_snfled.ino @@ -80,7 +80,6 @@ void sl_my92x1_command(uint8_t chips, uint8_t command) { uint8_t command_data; -// ets_intr_lock(); os_delay_us(12); // TStop > 12us. // Send 12 DI pulse, after 6 pulse's falling edge store duty data, and 12 // pulse's rising edge convert to command mode. @@ -104,19 +103,17 @@ void sl_my92x1_command(uint8_t chips, uint8_t command) // at 16 pulse's falling edge convert to duty mode. sl_di_pulse(16); os_delay_us(12); // TStop > 12us. -// 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) +void sl_my9231_duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t duty_w, uint8_t duty_c) { - uint16_t duty_current = 0; + uint8_t duty_current = 0; - uint16_t duty[6] = { duty_r, duty_g, duty_b, duty_w, duty_c, 0 }; // Definition for RGBWC channels + uint8_t duty[6] = { duty_w, duty_c, 0, duty_g, duty_r, duty_b }; // Definition for RGBWC channels -// ets_intr_lock(); 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 channel = 0; channel < 6; channel++) { // WC0GRB 6CH + duty_current = duty[channel]; for (uint8_t i = 0; i < 4; i++) { // Send 8bit Data digitalWrite(sl_pdcki, LOW); digitalWrite(sl_pdi, (duty_current & 0x80)); @@ -131,16 +128,14 @@ void sl_my9231_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t 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(); } -void sl_my9291_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t duty_w) +void sl_my9291_duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t duty_w) { - uint16_t duty_current = 0; + uint8_t duty_current = 0; - uint16_t duty[4] = { duty_r, duty_g, duty_b, duty_w }; // Definition for RGBW channels + uint8_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 @@ -158,7 +153,6 @@ void sl_my9291_duty(uint16_t duty_r, uint16_t duty_g, uint16_t duty_b, uint16_t 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(); } /********************************************************************************************/ @@ -198,7 +192,7 @@ void sl_init(void) 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_dcki_pulse(64); // 2 * 32 bits sl_my92x1_command(2, 0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM } } @@ -211,7 +205,7 @@ void sl_init(void) void sl_setDim(uint8_t myDimmer) { float temp; - + if ((1 == sfl_flg) && (100 == myDimmer)) { myDimmer = 99; // BN-SZ01 starts flickering at dimmer = 100 } @@ -371,76 +365,17 @@ void sl_animate() void sl_rgb2hsb(float *hue, float *sat, float *bri) { - sl_setDim(sysCfg.led_dimmer[0]); + RgbColor dcolor; - 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(); -} + sl_setDim(sysCfg.led_dimmer[0]); + dcolor.R = sl_dcolor[0]; + dcolor.G = sl_dcolor[1]; + dcolor.B = sl_dcolor[2]; + HsbColor hsb = HsbColor(dcolor); + *hue = hsb.H; + *sat = hsb.S; + *bri = hsb.B; +} /********************************************************************************************/ @@ -458,7 +393,8 @@ void sl_replaceHSB(String *response) } else { response->replace("{h}", "0"); response->replace("{s}", "0"); - response->replace("{b}", String((uint8_t)(2.54f * (float)sysCfg.led_dimmer[0]))); +// response->replace("{b}", String((uint8_t)(2.54f * (float)sysCfg.led_dimmer[0]))); + response->replace("{b}", String((uint8_t)(0.01f * (float)sysCfg.led_dimmer[0]))); } } @@ -469,33 +405,68 @@ void sl_getHSB(float *hue, float *sat, float *bri) } else { *hue = 0; *sat = 0; - *bri = (2.54f * (float)sysCfg.led_dimmer[0]); +// *bri = (2.54f * (float)sysCfg.led_dimmer[0]); + *bri = (0.01f * (float)sysCfg.led_dimmer[0]); } } -void sl_setHSB(float hue, float sat, float bri) +void sl_setHSB(float hue, float sat, float bri, float ct) { char svalue[MESSZ]; + HsbColor hsb; + float my_ct; + /* char log[LOGSZ]; char stemp1[10]; char stemp2[10]; char stemp3[10]; + char stemp4[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); + dtostrf(ct, 1, 3, stemp4); + snprintf_P(log, sizeof(log), PSTR("HUE: Set Hue %s, Sat %s, Bri %s, Ct %s"), stemp1, stemp2, stemp3, stemp4); addLog(LOG_LEVEL_DEBUG, log); */ + if (sfl_flg > 2) { - sl_hsb2rgb(hue, sat, bri); + hsb.H = hue; + hsb.S = sat; + hsb.B = bri; + RgbColor tmp = RgbColor(hsb); + sl_dcolor[0] = tmp.R; + sl_dcolor[1] = tmp.G; + sl_dcolor[2] = tmp.B; + sl_setColor(); 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); + if (2 == sfl_flg) { + if (ct > 0) { + my_ct = ct - 0.306; + if (my_ct > 0.694) { // >500 (Warm) + my_ct = 0.694; + } + float fcold = 367 * (0.694 - my_ct); // 0 - 255 + float fwarm = 367 * my_ct; // 0 - 255 + float fmax = (fwarm > fcold) ? fwarm : fcold; + float fbri = 100 / (fmax / 2.55); // Scale to 255 + if (bri < 1) { + bri = bri + 0.01; // Adjust for sl_setColor + } + sl_dcolor[0] = (uint8_t)(fcold * fbri * bri); // Cold + sl_dcolor[1] = (uint8_t)(fwarm * fbri * bri); // Warm + sl_setColor(); + } + sl_prepPower(svalue, sizeof(svalue)); + mqtt_publish_topic_P(5, "COLOR", svalue); + } else { + sl_prepPower(svalue, sizeof(svalue)); + mqtt_publish_topic_P(5, "DIMMER", svalue); + } } } diff --git a/sonoff/xdrv_wemohue.ino b/sonoff/xdrv_wemohue.ino index 08bcba878..3d0aa30fc 100644 --- a/sonoff/xdrv_wemohue.ino +++ b/sonoff/xdrv_wemohue.ino @@ -533,6 +533,7 @@ void hue_lights(String *path) float bri = 0; float hue = 0; float sat = 0; + float ct = 0; bool resp = false; bool on = false; bool change = false; @@ -636,9 +637,21 @@ void hue_lights(String *path) response.replace("{res}", String(tmp)); change = true; } + if (hue_json.containsKey("ct")) { // Color temperature 153 (Cold) to 500 (Warm) + tmp = hue_json["ct"]; + ct = (float)tmp / 500.0f; + if (resp) { + response += ","; + } + response += FPSTR(HUE_LIGHT_RESPONSE_JSON); + response.replace("{id}", String(device)); + response.replace("{cmd}", "ct"); + response.replace("{res}", String(tmp)); + change = true; + } if (change) { if (sfl_flg) { - sl_setHSB(hue, sat, bri); + sl_setHSB(hue, sat, bri, ct); #ifdef USE_WS2812 } else if (pin[GPIO_WS2812] < 99) { @@ -655,6 +668,9 @@ void hue_lights(String *path) else { response = FPSTR(HUE_ERROR_JSON); } + +addLog(LOG_LEVEL_DEBUG_MORE, response.c_str()); + webServer->send(200, FPSTR(HDR_CTYPE_JSON), response); } else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID @@ -678,6 +694,9 @@ void hue_lights(String *path) void hue_groups(String *path) { +/* + * http://sonoff/api/username/groups?1={"name":"Woonkamer","lights":[],"type":"Room","class":"Living room"}) + */ String response = "{}"; if (path->endsWith("/0")) { @@ -721,7 +740,7 @@ void handle_hue_api(String *path) else if (path->endsWith("/")) hue_auth(path); // New HUE App setup else if (path->endsWith("/config")) hue_config(path); else if (path->indexOf("/lights") >= 0) hue_lights(path); - else if (path->indexOf("/groups") >= 0) hue_groups(path); + else if (path->indexOf("/groups") >= 0) hue_groups(path); else if (path->endsWith("/schedules")) hue_todo(path); else if (path->endsWith("/sensors")) hue_todo(path); else if (path->endsWith("/scenes")) hue_todo(path); diff --git a/sonoff/xdrv_ws2812.ino b/sonoff/xdrv_ws2812.ino index 93a6e5fe4..92fe3e349 100644 --- a/sonoff/xdrv_ws2812.ino +++ b/sonoff/xdrv_ws2812.ino @@ -22,7 +22,7 @@ * WS2812 Leds using NeopixelBus library \*********************************************************************************************/ -#include +//#include // Global defined as also used by Sonoff Led #ifdef USE_WS2812_DMA #if (USE_WS2812_CTYPE == 1) @@ -145,37 +145,6 @@ void ws2812_setColor(uint16_t led, char* colstr) } } -void ws2812_replaceHSB(String *response) -{ - ws2812_setDim(sysCfg.ws_dimmer); - HsbColor hsb = HsbColor(dcolor); - response->replace("{h}", String((uint16_t)(65535.0f * hsb.H))); - response->replace("{s}", String((uint8_t)(254.0f * hsb.S))); - response->replace("{b}", String((uint8_t)(254.0f * hsb.B))); -} - -void ws2812_getHSB(float *hue, float *sat, float *bri) -{ - ws2812_setDim(sysCfg.ws_dimmer); - HsbColor hsb = HsbColor(dcolor); - *hue = hsb.H; - *sat = hsb.S; - *bri = hsb.B; -} - -void ws2812_setHSB(float hue, float sat, float bri) -{ - char rgb[7]; - - HsbColor hsb; - hsb.H = hue; - hsb.S = sat; - hsb.B = bri; - RgbColor tmp = RgbColor(hsb); - sprintf(rgb,"%02X%02X%02X", tmp.R, tmp.G, tmp.B); - ws2812_setColor(0,rgb); -} - void ws2812_getColor(uint16_t led, char* svalue, uint16_t ssvalue) { RgbColor mcolor; @@ -504,6 +473,41 @@ void ws2812_init(uint8_t powerbit) ws2812_pixels(); } +/*********************************************************************************************\ + * Hue support +\*********************************************************************************************/ + +void ws2812_replaceHSB(String *response) +{ + ws2812_setDim(sysCfg.ws_dimmer); + HsbColor hsb = HsbColor(dcolor); + response->replace("{h}", String((uint16_t)(65535.0f * hsb.H))); + response->replace("{s}", String((uint8_t)(254.0f * hsb.S))); + response->replace("{b}", String((uint8_t)(254.0f * hsb.B))); +} + +void ws2812_getHSB(float *hue, float *sat, float *bri) +{ + ws2812_setDim(sysCfg.ws_dimmer); + HsbColor hsb = HsbColor(dcolor); + *hue = hsb.H; + *sat = hsb.S; + *bri = hsb.B; +} + +void ws2812_setHSB(float hue, float sat, float bri) +{ + char rgb[7]; + + HsbColor hsb; + hsb.H = hue; + hsb.S = sat; + hsb.B = bri; + RgbColor tmp = RgbColor(hsb); + sprintf(rgb,"%02X%02X%02X", tmp.R, tmp.G, tmp.B); + ws2812_setColor(0,rgb); +} + /*********************************************************************************************\ * Commands \*********************************************************************************************/ From ebee148be89c1c16d042ec4db59d50d946a755fd Mon Sep 17 00:00:00 2001 From: arendst Date: Wed, 16 Aug 2017 17:05:36 +0200 Subject: [PATCH 5/5] Add Sonoff B1 features --- sonoff/_releasenotes.ino | 11 +- sonoff/sonoff.ino | 2 +- sonoff/sonoff_template.h | 3 +- sonoff/webserver.ino | 21 +++- sonoff/xdrv_ir_send.ino | 2 +- sonoff/xdrv_snfled.ino | 211 ++++++++++++++++++++------------------- sonoff/xdrv_wemohue.ino | 9 +- 7 files changed, 141 insertions(+), 118 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index cc9d81c0b..03a265ba0 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,9 +1,14 @@ -/* 5.5.2g +/* 5.5.2h * Fix Sonoff Pow intermittent exception 0 * Change Sonoff Pow sending Domoticz telemetry data only - * Add Sonoff B1 support + * Add Sonoff B1 RGBCW led support with command Color RRGGBBCCWW (#676) + * Add command CT 152..500 to Sonoff Led and Sonoff B1 to control Color Temperature + * Add Cold-Warm slider to web page for Sonoff Led and Sonoff B1 + * Add CT parameter to Hue * Add Ai-Thinker RGBW led (AiLight) (experimental) - * Add NeoPixelBus library to Sonoff Led for Hue support (saves 1k1 code space) + * Add NeoPixelBus library to Sonoff Led for Hue support (saves 1k code space) + * Add user configurable GPIO4 and GPIO5 to module Sonoff Bridge + * Fix Mitsubishi HVAC IR power controll (#740) * * 5.5.2 20170808 * Extent max number of WS2812 pixels from 256 to 512 (#667) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 97a0c15bc..f4be6a1d7 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x05050207 // 5.5.2g +#define VERSION 0x05050208 // 5.5.2h enum log_t {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL}; enum week_t {Last, First, Second, Third, Fourth}; diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 333ed89e9..409621322 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -486,7 +486,8 @@ const mytmplt modules[MAXMODULE] PROGMEM = { GPIO_TXD, // GPIO01 RF bridge control GPIO_USER, // GPIO02 Optional sensor GPIO_RXD, // GPIO03 RF bridge control - 0, 0, + GPIO_USER, // GPIO04 Optional sensor + GPIO_USER, // GPIO05 Optional sensor 0, 0, 0, // Flash connection 0, 0, 0, // Flash connection diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino index 249fd51e5..a8bf0bba2 100644 --- a/sonoff/webserver.ino +++ b/sonoff/webserver.ino @@ -70,6 +70,9 @@ const char HTTP_HEAD[] PROGMEM = "function lb(p){" "la('?d='+p);" "}" + "function lc(p){" + "la('?t='+p);" + "}" "" "" "" "" - "
" + "
" "

{ha} Module

{h}

"; const char HTTP_SCRIPT_CONSOL[] PROGMEM = "var sn=0;" // Scroll position @@ -415,7 +419,7 @@ void handleRoot() if (HTTP_MANAGER == _httpflag) { handleWifi0(); } else { - char stemp[10], line[100]; + char stemp[10], line[160]; String page = FPSTR(HTTP_HEAD); page.replace(F("{v}"), F("Main menu")); page.replace(F(""), F("")); @@ -423,7 +427,12 @@ void handleRoot() page += F("
"); if (Maxdevice) { if (sfl_flg) { - snprintf_P(line, sizeof(line), PSTR(""), + if ((2 == sfl_flg) || (5 == sfl_flg)) { + snprintf_P(line, sizeof(line), PSTR("
ColdWarm
"), + sl_getColorTemp()); + page += line; + } + snprintf_P(line, sizeof(line), PSTR("
DarkBright
"), sysCfg.led_dimmer[0]); page += line; } @@ -474,6 +483,10 @@ void handleAjax2() snprintf_P(svalue, sizeof(svalue), PSTR("dimmer %s"), webServer->arg("d").c_str()); do_cmnd(svalue); } + if (strlen(webServer->arg("t").c_str())) { + snprintf_P(svalue, sizeof(svalue), PSTR("ct %s"), webServer->arg("t").c_str()); + do_cmnd(svalue); + } if (strlen(webServer->arg("k").c_str())) { snprintf_P(svalue, sizeof(svalue), PSTR("rfkey%s"), webServer->arg("k").c_str()); do_cmnd(svalue); diff --git a/sonoff/xdrv_ir_send.ino b/sonoff/xdrv_ir_send.ino index a1998f605..e25b44176 100644 --- a/sonoff/xdrv_ir_send.ino +++ b/sonoff/xdrv_ir_send.ino @@ -261,7 +261,7 @@ boolean ir_hvac_mitsubishi(const char *HVAC_Mode,const char *HVAC_FanMode, boole mode = (p - HVACMODE +1) << 3; // HOT = 0x08, DRY = 0x10, COOL = 0x18, AUTO = 0x20 mitsubir->setMode(mode); - mitsubir->setPower(~HVAC_Power); + mitsubir->setPower(HVAC_Power); if (HVAC_FanMode == NULL) { p = (char*)FANSPEED; // default FAN_SPEED_AUTO diff --git a/sonoff/xdrv_snfled.ino b/sonoff/xdrv_snfled.ino index 525c245f4..dba0e6167 100644 --- a/sonoff/xdrv_snfled.ino +++ b/sonoff/xdrv_snfled.ino @@ -19,6 +19,13 @@ /*********************************************************************************************\ * Sonoff B1, AiLight, Sonoff Led and BN-SZ01 + * + * sfl_flg Module Color ColorTemp + * 1 Sonoff BN-SZ W no + * 2 Sonoff Led CW yes + * 3 not used + * 4 AiLight RGBW no + * 5 Sonoff B1 RGBCW yes \*********************************************************************************************/ uint8_t ledTable[] = { @@ -50,7 +57,7 @@ uint8_t sl_wakeupDimmer = 0; uint16_t sl_wakeupCntr = 0; /*********************************************************************************************\ - * Sonoff B1 (my9231) and AiLight (my9291) based on OpenLight https://github.com/icamgo/noduino-sdk + * Sonoff B1 and AiLight inspired by OpenLight https://github.com/icamgo/noduino-sdk \*********************************************************************************************/ extern "C" { @@ -76,83 +83,56 @@ void sl_dcki_pulse(uint8_t times) } } -void sl_my92x1_command(uint8_t chips, uint8_t command) +void sl_my92x1_write(uint8_t data) { - uint8_t command_data; + for (uint8_t i = 0; i < 4; i++) { // Send 8bit Data + digitalWrite(sl_pdcki, LOW); + digitalWrite(sl_pdi, (data & 0x80)); + digitalWrite(sl_pdcki, HIGH); + data = data << 1; + digitalWrite(sl_pdi, (data & 0x80)); + digitalWrite(sl_pdcki, LOW); + digitalWrite(sl_pdi, LOW); + data = data << 1; + } +} - os_delay_us(12); // TStop > 12us. +void sl_my92x1_init() +{ + uint8_t chips = sfl_flg -3; // 1 (AiLight) or 2 (Sonoff B1) + + sl_dcki_pulse(chips * 32); // Clear all duty register + os_delay_us(12); // TStop > 12us. // 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 + os_delay_us(12); // Delay >12us, begin send CMD data for (uint8_t n = 0; n < chips; n++) { // Send CMD data - command_data = command; - for (uint8_t i = 0; i < 4; i++) { // Send byte - digitalWrite(sl_pdcki, LOW); - digitalWrite(sl_pdi, (command_data & 0x80)); - digitalWrite(sl_pdcki, HIGH); - command_data = command_data << 1; - digitalWrite(sl_pdi, (command_data & 0x80)); - digitalWrite(sl_pdcki, LOW); - digitalWrite(sl_pdi, LOW); - command_data = command_data << 1; - } + sl_my92x1_write(0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM } - os_delay_us(12); // TStart > 12us. Delay 12 us. + os_delay_us(12); // TStart > 12us. Delay 12 us. // 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); - os_delay_us(12); // TStop > 12us. + os_delay_us(12); // TStop > 12us. } -void sl_my9231_duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t duty_w, uint8_t duty_c) +void sl_my92x1_duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t duty_w, uint8_t duty_c) { - uint8_t duty_current = 0; + uint8_t channels[2] = { 4, 6 }; - uint8_t duty[6] = { duty_w, duty_c, 0, duty_g, duty_r, duty_b }; // Definition for RGBWC channels + uint8_t didx = sfl_flg -4; // 0 or 1 - os_delay_us(12); // TStop > 12us. - for (uint8_t channel = 0; channel < 6; channel++) { // WC0GRB 6CH - duty_current = duty[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; - } + uint8_t duty[2][6] = {{ duty_r, duty_g, duty_b, duty_w, 0, 0 }, // Definition for RGBW channels + { duty_w, duty_c, 0, duty_g, duty_r, duty_b }}; // Definition for RGBWC channels + + os_delay_us(12); // TStop > 12us. + for (uint8_t channel = 0; channel < channels[didx]; channel++) { + sl_my92x1_write(duty[didx][channel]); // Send 8bit Data } - 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. -} - -void sl_my9291_duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t duty_w) -{ - uint8_t duty_current = 0; - - uint8_t duty[4] = { duty_r, duty_g, duty_b, duty_w }; // Definition for RGBW channels - - 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. + 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. } /********************************************************************************************/ @@ -186,15 +166,7 @@ void sl_init(void) digitalWrite(sl_pdi, LOW); digitalWrite(sl_pdcki, LOW); - 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(64); // 2 * 32 bits - sl_my92x1_command(2, 0x18); // ONE_SHOT_DISABLE, REACTION_FAST, BIT_WIDTH_8, FREQUENCY_DIVIDE_1, SCATTER_APDM - } + sl_my92x1_init(); } sl_power = 0; @@ -202,6 +174,46 @@ void sl_init(void) sl_wakeupActive = 0; } +void sl_setColorTemp(uint16_t ct) +{ +/* Color Temperature (https://developers.meethue.com/documentation/core-concepts) + * + * 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 (5 == sfl_flg) { + sysCfg.led_color[0] = 0; + sysCfg.led_color[1] = 0; + sysCfg.led_color[2] = 0; + sysCfg.led_color[3] = (uint8_t)icold; + sysCfg.led_color[4] = (uint8_t)iwarm; + } else { + sysCfg.led_color[0] = (uint8_t)icold; + sysCfg.led_color[1] = (uint8_t)iwarm; + } +} + +uint16_t sl_getColorTemp() +{ + uint8_t ct_idx = 0; + if (5 == sfl_flg) { + ct_idx = 3; + } + uint16_t my_ct = sysCfg.led_color[ct_idx +1]; + if (my_ct > 0) { + return ((my_ct * 136) / 100) + 154; + } else { + my_ct = sysCfg.led_color[ct_idx]; + return 499 - ((my_ct * 136) / 100); + } +} + void sl_setDim(uint8_t myDimmer) { float temp; @@ -350,11 +362,8 @@ void sl_animate() } } } - 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]); + if (sfl_flg > 3) { + sl_my92x1_duty(cur_col[0], cur_col[1], cur_col[2], cur_col[3], cur_col[4]); } } } @@ -410,35 +419,36 @@ void sl_getHSB(float *hue, float *sat, float *bri) } } -void sl_setHSB(float hue, float sat, float bri, float ct) +void sl_setHSB(float hue, float sat, float bri, uint16_t ct) { char svalue[MESSZ]; HsbColor hsb; - float my_ct; /* char log[LOGSZ]; char stemp1[10]; char stemp2[10]; char stemp3[10]; - char stemp4[10]; dtostrf(hue, 1, 3, stemp1); dtostrf(sat, 1, 3, stemp2); dtostrf(bri, 1, 3, stemp3); - dtostrf(ct, 1, 3, stemp4); - snprintf_P(log, sizeof(log), PSTR("HUE: Set Hue %s, Sat %s, Bri %s, Ct %s"), stemp1, stemp2, stemp3, stemp4); + snprintf_P(log, sizeof(log), PSTR("HUE: Set Hue %s, Sat %s, Bri %s, Ct %d"), stemp1, stemp2, stemp3, ct); addLog(LOG_LEVEL_DEBUG, log); */ if (sfl_flg > 2) { - hsb.H = hue; - hsb.S = sat; - hsb.B = bri; - RgbColor tmp = RgbColor(hsb); - sl_dcolor[0] = tmp.R; - sl_dcolor[1] = tmp.G; - sl_dcolor[2] = tmp.B; - sl_setColor(); + if ((5 == sfl_flg) && (ct > 0)) { + sl_setColorTemp(ct); + } else { + hsb.H = hue; + hsb.S = sat; + hsb.B = bri; + RgbColor tmp = RgbColor(hsb); + sl_dcolor[0] = tmp.R; + sl_dcolor[1] = tmp.G; + sl_dcolor[2] = tmp.B; + sl_setColor(); + } sl_prepPower(svalue, sizeof(svalue)); mqtt_publish_topic_P(5, "COLOR", svalue); } else { @@ -446,20 +456,7 @@ void sl_setHSB(float hue, float sat, float bri, float ct) sysCfg.led_dimmer[0] = tmp; if (2 == sfl_flg) { if (ct > 0) { - my_ct = ct - 0.306; - if (my_ct > 0.694) { // >500 (Warm) - my_ct = 0.694; - } - float fcold = 367 * (0.694 - my_ct); // 0 - 255 - float fwarm = 367 * my_ct; // 0 - 255 - float fmax = (fwarm > fcold) ? fwarm : fcold; - float fbri = 100 / (fmax / 2.55); // Scale to 255 - if (bri < 1) { - bri = bri + 0.01; // Adjust for sl_setColor - } - sl_dcolor[0] = (uint8_t)(fcold * fbri * bri); // Cold - sl_dcolor[1] = (uint8_t)(fwarm * fbri * bri); // Warm - sl_setColor(); + sl_setColorTemp(ct); } sl_prepPower(svalue, sizeof(svalue)); mqtt_publish_topic_P(5, "COLOR", svalue); @@ -493,6 +490,14 @@ boolean sl_command(char *type, uint16_t index, char *dataBufUc, uint16_t data_le snprintf_P(svalue, ssvalue, PSTR("{\"Color\":\"%s\"}"), sl_getColor(scolor)); } } + else if (!strcmp_P(type,PSTR("CT")) && ((2 == sfl_flg) || (5 == sfl_flg))) { // ColorTemp + if ((payload >= 153) && (payload <= 500)) { // https://developers.meethue.com/documentation/core-concepts + sl_setColorTemp(payload); + coldim = true; + } else { + snprintf_P(svalue, ssvalue, PSTR("{\"CT\":%d}"), sl_getColorTemp()); + } + } else if (!strcmp_P(type,PSTR("DIMMER"))) { if ((payload >= 0) && (payload <= 100)) { sysCfg.led_dimmer[0] = payload; diff --git a/sonoff/xdrv_wemohue.ino b/sonoff/xdrv_wemohue.ino index 3d0aa30fc..ac522167d 100644 --- a/sonoff/xdrv_wemohue.ino +++ b/sonoff/xdrv_wemohue.ino @@ -533,7 +533,7 @@ void hue_lights(String *path) float bri = 0; float hue = 0; float sat = 0; - float ct = 0; + uint16_t ct = 0; bool resp = false; bool on = false; bool change = false; @@ -638,15 +638,14 @@ void hue_lights(String *path) change = true; } if (hue_json.containsKey("ct")) { // Color temperature 153 (Cold) to 500 (Warm) - tmp = hue_json["ct"]; - ct = (float)tmp / 500.0f; + ct = hue_json["ct"]; if (resp) { response += ","; } response += FPSTR(HUE_LIGHT_RESPONSE_JSON); response.replace("{id}", String(device)); response.replace("{cmd}", "ct"); - response.replace("{res}", String(tmp)); + response.replace("{res}", String(ct)); change = true; } if (change) { @@ -669,7 +668,7 @@ void hue_lights(String *path) response = FPSTR(HUE_ERROR_JSON); } -addLog(LOG_LEVEL_DEBUG_MORE, response.c_str()); +//addLog(LOG_LEVEL_DEBUG_MORE, response.c_str()); webServer->send(200, FPSTR(HDR_CTYPE_JSON), response); }