From 25856c323a834cead4d412402ed77563adfc236f Mon Sep 17 00:00:00 2001 From: Colin Kuebler Date: Fri, 1 Feb 2019 00:59:53 -0500 Subject: [PATCH 1/6] Track sm16716 select pin state so we don't waste cycles continuously enabling/disabling color Added some comments; disabled sm16716 debug messages by default Removed delay after setting select pin; didn't seem to help anyway --- sonoff/xdrv_04_light.ino | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sonoff/xdrv_04_light.ino b/sonoff/xdrv_04_light.ino index 9cb22d698..4a6c21389 100644 --- a/sonoff/xdrv_04_light.ino +++ b/sonoff/xdrv_04_light.ino @@ -367,11 +367,12 @@ void LightMy92x1Duty(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b, uint8_t dut \*********************************************************************************************/ // Enable this for debug logging -#define D_LOG_SM16716 "SM16716: " +//#define D_LOG_SM16716 "SM16716: " uint8_t sm16716_pin_clk = 100; uint8_t sm16716_pin_dat = 100; uint8_t sm16716_pin_sel = 100; +uint8_t sm16716_enabled = 0; void SM16716_SendBit(uint8_t v) { @@ -399,19 +400,23 @@ void SM16716_SendByte(uint8_t v) void SM16716_Update(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b) { if (sm16716_pin_sel < 99) { - if (duty_r | duty_g | duty_b) { + uint8_t sm16716_should_enable = (duty_r | duty_g | duty_b); + if (!sm16716_enabled && sm16716_should_enable) { #ifdef D_LOG_SM16716 snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "turning color on")); AddLog(LOG_LEVEL_DEBUG); #endif // D_LOG_SM16716 + sm16716_enabled = 1; digitalWrite(sm16716_pin_sel, HIGH); - delayMicroseconds(20); + //delayMicroseconds(20); SM16716_Init(); - } else { + } + else if (sm16716_enabled && !sm16716_should_enable) { #ifdef D_LOG_SM16716 snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "turning color off")); AddLog(LOG_LEVEL_DEBUG); #endif // D_LOG_SM16716 + sm16716_enabled = 0; digitalWrite(sm16716_pin_sel, LOW); } } @@ -977,6 +982,7 @@ void LightAnimate(void) #endif // USE_ES2812 ************************************************************************ #ifdef USE_SM16716 else if (16 & light_type) { + // handle any PWM pins, skipping the first 3 values for sm16716 for (uint8_t i = 3; i < light_subtype; i++) { if (pin[GPIO_PWM1 +i-3] < 99) { if (cur_col[i] > 0xFC) { @@ -988,6 +994,7 @@ void LightAnimate(void) analogWrite(pin[GPIO_PWM1 +i-3], bitRead(pwm_inverted, i-3) ? Settings.pwm_range - curcol : curcol); } } + // handle sm16716 update SM16716_Update(cur_col[0], cur_col[1], cur_col[2]); } #endif // ifdef USE_SM16716 From 99b027852b145e27b93a7f7fdd3f933d95d01c22 Mon Sep 17 00:00:00 2001 From: Colin Kuebler Date: Fri, 1 Feb 2019 17:33:21 -0500 Subject: [PATCH 2/6] Fixed first color command failing; need to wait for chip to power up --- sonoff/xdrv_04_light.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sonoff/xdrv_04_light.ino b/sonoff/xdrv_04_light.ino index 4a6c21389..ba12ac3db 100644 --- a/sonoff/xdrv_04_light.ino +++ b/sonoff/xdrv_04_light.ino @@ -408,7 +408,9 @@ void SM16716_Update(uint8_t duty_r, uint8_t duty_g, uint8_t duty_b) #endif // D_LOG_SM16716 sm16716_enabled = 1; digitalWrite(sm16716_pin_sel, HIGH); - //delayMicroseconds(20); + // in testing I found it takes a minimum of ~380us to wake up the chip + // tested on a Merkury RGBW with an SM726EB + delayMicroseconds(400); SM16716_Init(); } else if (sm16716_enabled && !sm16716_should_enable) { From 817d0af598d887c7105e7d023f840d4f23762d87 Mon Sep 17 00:00:00 2001 From: Colin Kuebler Date: Fri, 1 Feb 2019 17:37:37 -0500 Subject: [PATCH 3/6] No need (and pointless) to call SM16716_Init until color updates, iff using sel pin --- sonoff/xdrv_04_light.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sonoff/xdrv_04_light.ino b/sonoff/xdrv_04_light.ino index ba12ac3db..f65dd43da 100644 --- a/sonoff/xdrv_04_light.ino +++ b/sonoff/xdrv_04_light.ino @@ -535,9 +535,11 @@ void LightInit(void) if (sm16716_pin_sel < 99) { pinMode(sm16716_pin_sel, OUTPUT); digitalWrite(sm16716_pin_sel, LOW); + // no need to call SM16716_Init here, it will be called after sel goes HIGH + } else { + // no sel pin means you have an 'always on' chip, so init right away + SM16716_Init(); } - - SM16716_Init(); } #endif // ifdef USE_SM16716 else { From 6d19a9c998eb6a4da0692836d420628b96d6ccfc Mon Sep 17 00:00:00 2001 From: Colin Kuebler Date: Fri, 1 Feb 2019 19:28:16 -0500 Subject: [PATCH 4/6] Generalize the SYF05 a tiny bit so it can be easily adapted to similar bulbs --- sonoff/sonoff_template.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 80155a7c1..e57cd54cf 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -1827,6 +1827,7 @@ const mytmplt kModules[MAXMODULE] PROGMEM = { 0, 0, 0 }, { "SYF05", // Sunyesmart SYF05 (a.k.a. Fcmila) = TYWE3S + SM16726 + // Also works with Merkury 904 RGBW Bulbs with 13 set to GPIO_SM16716_SEL // https://www.flipkart.com/fc-mila-bxav-xs-ad-smart-bulb/p/itmf85zgs45fzr7n // https://docs.tuya.com/en/hardware/WiFi-module/wifi-e3s-module.html // http://www.datasheet-pdf.com/PDF/SM16716-Datasheet-Sunmoon-932771 @@ -1834,17 +1835,17 @@ const mytmplt kModules[MAXMODULE] PROGMEM = { 0, GPIO_USER, // GPIO02 N.C. 0, - GPIO_SM16716_CLK, // GPIO04 - GPIO_PWM1, // GPIO05 Cold White + GPIO_SM16716_CLK, // GPIO04 SM16716 Clock + GPIO_PWM1, // GPIO05 White // GPIO06 // GPIO07 // GPIO08 0, // GPIO09 0, // GPIO10 // GPIO11 - GPIO_PWM2, // GPIO12 Warm White - GPIO_SM16716_SEL, // GPIO13 - GPIO_SM16716_DAT, // GPIO14 + GPIO_USER, // GPIO12 Alt. White on some devices + GPIO_USER, // GPIO13 SM16716 Select on some devices + GPIO_SM16716_DAT, // GPIO14 SM16716 Data 0, // GPIO15 wired to GND GPIO_USER, // GPIO16 N.C. GPIO_FLAG_ADC0 // ADC0 A0 Analog input From d7b8a6453dbea52a6006d1e2c62593365dd39376 Mon Sep 17 00:00:00 2001 From: Colin Kuebler Date: Fri, 1 Feb 2019 19:41:12 -0500 Subject: [PATCH 5/6] Added preprocessor constant for LT_SM16716 for improved code readability Note that this differs from other LT_* in that it's not an enum Ensure the SM16716 codepath is only taken when the top half of light_type is LT_SM16716 --- sonoff/sonoff.h | 2 ++ sonoff/sonoff.ino | 2 +- sonoff/xdrv_04_light.ino | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index 9ba91f091..b54a32f3c 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -168,6 +168,8 @@ typedef unsigned long power_t; // Power (Relay) type #define NEO_RGBW 5 // Neopixel RGBW leds #define NEO_GRBW 6 // Neopixel GRBW leds +#define LT_SM16716 16 // Lights that use SM16716 will have this bit set in light_type + #define MQTT_PUBSUBCLIENT 1 // Mqtt PubSubClient library #define MQTT_TASMOTAMQTT 2 // Mqtt TasmotaMqtt library based on esp-mqtt-arduino - soon obsolete #define MQTT_ESPMQTTARDUINO 3 // Mqtt esp-mqtt-arduino library by Ingo Randolf - obsolete but define is present for debugging purposes diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 694f7d4c9..7be59f746 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -2425,7 +2425,7 @@ void GpioInit(void) #ifdef USE_SM16716 if (SM16716_ModuleSelected()) { light_type += 3; - light_type |= 16; + light_type |= LT_SM16716; } #endif // ifdef USE_SM16716 if (!light_type) { diff --git a/sonoff/xdrv_04_light.ino b/sonoff/xdrv_04_light.ino index f65dd43da..bc4a9b91c 100644 --- a/sonoff/xdrv_04_light.ino +++ b/sonoff/xdrv_04_light.ino @@ -517,7 +517,7 @@ void LightInit(void) } #endif // USE_WS2812 ************************************************************************ #ifdef USE_SM16716 - else if (16 & light_type) { + else if (LT_SM16716 == light_type - light_subtype) { // init PWM for (uint8_t i = 0; i < light_subtype; i++) { Settings.pwm_value[i] = 0; // Disable direct PWM control @@ -985,7 +985,7 @@ void LightAnimate(void) } #endif // USE_ES2812 ************************************************************************ #ifdef USE_SM16716 - else if (16 & light_type) { + else if (LT_SM16716 == light_type - light_subtype) { // handle any PWM pins, skipping the first 3 values for sm16716 for (uint8_t i = 3; i < light_subtype; i++) { if (pin[GPIO_PWM1 +i-3] < 99) { From 35060dfa45cb84c248c1930f522dc51f0f23b41e Mon Sep 17 00:00:00 2001 From: Colin Kuebler Date: Fri, 1 Feb 2019 20:12:13 -0500 Subject: [PATCH 6/6] Measured impact of USE_SM16716 on binary filesize --- sonoff/my_user_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index c914e5567..d06b0c854 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -417,7 +417,7 @@ // #define USE_THEO_V2 // Add support for decoding Theo V2 sensors as documented on https://sidweb.nl using 434MHz RF sensor receiver (+1k4 code) // #define USE_ALECTO_V2 // Add support for decoding Alecto V2 sensors like ACH2010, WS3000 and DKW2012 weather stations using 868MHz RF sensor receiver (+1k7 code) -#define USE_SM16716 // Add support for SM16716 RGB LED controller +#define USE_SM16716 // Add support for SM16716 RGB LED controller (+0k7 code) /*********************************************************************************************\ * Debug features are only supported in development branch