From 48968b4bc06043d0208f51c23d811f21b288f158 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 8 Sep 2022 22:04:08 +0200 Subject: [PATCH] Fix fade pwmct #16454 --- tasmota/tasmota_xdrv_driver/xdrv_04_light.ino | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino b/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino index 94268aa99..18534aee5 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_04_light.ino @@ -1941,6 +1941,17 @@ void LightAnimate(void) memcpy(Light.fade_start_10, Light.fade_cur_10, sizeof(Light.fade_start_10)); } memcpy(Light.fade_end_10, cur_col_10, sizeof(Light.fade_start_10)); + + // check if PWM CT is enabled, we need a special handling of CT #16454 + int32_t channel_ct = ChannelCT(); + int32_t channel_white = ChannelWhite_when_PWMCT(); + if (channel_ct >= 0 && channel_white >= 0) { + if (Light.fade_start_10[channel_white] == 0) { + // if fading from black, change the start CT to the target, otherwise we will have a wrong fade + Light.fade_start_10[channel_ct] = Light.fade_end_10[channel_ct]; + } + } + Light.fade_running = true; Light.fade_duration = 0; // set the value to zero to force a recompute Light.fade_start = 0; @@ -1977,17 +1988,30 @@ bool isChannelGammaCorrected(uint32_t channel) { return true; } -// is the channel a regular PWM or ColorTemp control -bool isChannelCT(uint32_t channel) { +// Returns the channel number for PWM CT if any, or -1 if none +int32_t ChannelCT(void) { #ifdef ESP8266 if ((PHILIPS == TasmotaGlobal.module_type) || (Settings->flag4.pwm_ct_mode)) { #else if (Settings->flag4.pwm_ct_mode) { #endif // ESP8266 - if ((LST_COLDWARM == Light.subtype) && (1 == channel)) { return true; } // PMW reserved for CT - if ((LST_RGBCW == Light.subtype) && (4 == channel)) { return true; } // PMW reserved for CT + if (LST_COLDWARM == Light.subtype) { return 1; } // PMW reserved for CT + if (LST_RGBCW == Light.subtype) { return 4; } // PMW reserved for CT } - return false; + return -1; +} + +// Returns the white channel when PWM CT is enabled -- needed to check for brightness #16454 +int32_t ChannelWhite_when_PWMCT(void) { +#ifdef ESP8266 + if ((PHILIPS == TasmotaGlobal.module_type) || (Settings->flag4.pwm_ct_mode)) { +#else + if (Settings->flag4.pwm_ct_mode) { +#endif // ESP8266 + if (LST_COLDWARM == Light.subtype) { return 0; } + if (LST_RGBCW == Light.subtype) { return 3; } + } + return -1; } // Calculate the Gamma correction, if any, for fading, using the fast Gamma curve (10 bits in+out) @@ -2142,6 +2166,7 @@ void LightSetOutputs(const uint16_t *cur_col_10) { } else #endif // USE_I2C #endif // USE_PWM_DIMMER + int32_t channel_ct = ChannelCT(); // Channel for PWM CT or -1 if no CT or regular CT for (uint32_t i = 0; i < (Light.subtype - Light.pwm_offset); i++) { uint16_t cur_col = cur_col_10[i + Light.pwm_offset]; #ifdef USE_PWM_DIMMER @@ -2150,14 +2175,16 @@ void LightSetOutputs(const uint16_t *cur_col_10) { if (PinUsed(GPIO_PWM1, i)) { //AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "Cur_Col%d 10 bits %d"), i, cur_col_10[i]); uint16_t cur_col = cur_col_10[i + Light.pwm_offset]; - if (!isChannelCT(i)) { // if CT don't use pwm_min and pwm_max + if (i != channel_ct) { // if CT don't use pwm_min and pwm_max cur_col = cur_col > 0 ? changeUIntScale(cur_col, 0, Settings->pwm_range, Light.pwm_min, Light.pwm_max) : 0; // shrink to the range of pwm_min..pwm_max } if (!Settings->flag4.zerocross_dimmer) { #ifdef ESP32 TasmotaGlobal.pwm_value[i] = cur_col; // mark the new expected value + // AddLog(LOG_LEVEL_DEBUG_MORE, "analogWrite-%i 0x%03X", i, cur_col); #else // ESP32 analogWrite(Pin(GPIO_PWM1, i), bitRead(TasmotaGlobal.pwm_inverted, i) ? Settings->pwm_range - cur_col : cur_col); + // AddLog(LOG_LEVEL_DEBUG_MORE, "analogWrite-%i 0x%03X", bitRead(TasmotaGlobal.pwm_inverted, i) ? Settings->pwm_range - cur_col : cur_col); #endif // ESP32 } }