From d7c642b9b95c77c99b6d17ed8bcd5e72c97b683c Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Tue, 23 Mar 2021 15:25:19 +0100 Subject: [PATCH] Two fixes for light transitioning issues * When turning off the light at a low brightness setting, the LEDs were seen flashing brightly for a short time. * Transitioning did not work when turning of the light. The light would be kept at normal brightness during the transition and then it turned of at once. This looks like an issue in the ESPHome transitioning code, because that is the code that is responsible for the transitioning. When turning on the light, it works correctly. I see both the state and the brightness go up. But when turning off the light, I see that the state decreases gradually to zero, but the brightness is kept as-is. I wrote a fix for this behavior in my code and will investigate if the ESPHome code can be fixed for this. --- yeelight_bs2.h | 64 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/yeelight_bs2.h b/yeelight_bs2.h index 72b2b31..4661b44 100644 --- a/yeelight_bs2.h +++ b/yeelight_bs2.h @@ -80,40 +80,44 @@ class YeelightBedsideLampV2LightOutput : public Component, public LightOutput void turn_off_() { - master2_->turn_off(); - master1_->turn_off(); - red_->turn_off(); - green_->turn_off(); - blue_->turn_off(); - white_->turn_off(); + master2_->turn_off(); + master1_->turn_off(); + red_->turn_off(); + green_->turn_off(); + blue_->turn_off(); + white_->turn_off(); } void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness) { - ESP_LOGD("custom", "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness); - - // Compensate for brightness. - red = red * brightness; - green = green * brightness; - blue = blue * brightness; - - // Inverse the signal. The LEDs in the lamp's circuit are brighter - // when the voltages on the GPIO pins are lower. - red = 1.0f - red; - green = 1.0f - green; - blue = 1.0f - blue; - - float white = 0.0; - - ESP_LOGD("rgb_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white); - - // Drive the LEDs. - red_->set_level(red); - green_->set_level(green); - blue_->set_level(blue); - white_->set_level(white); - master1_->turn_on(); - master2_->turn_on(); + ESP_LOGD("custom", "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness); + + // The brightness must be at least 3/100 to light up the LEDs. + if (brightness < 0.03) + brightness = 0.03; + + // Compensate for brightness. + red = red * brightness; + green = green * brightness; + blue = blue * brightness; + + // Inverse the signal. The LEDs in the lamp's circuit are brighter + // when the voltages on the GPIO pins are lower. + red = 1.0f - red; + green = 1.0f - green; + blue = 1.0f - blue; + + float white = 0.0; + + ESP_LOGD("rgb_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white); + + // Drive the LEDs. + red_->set_level(red); + green_->set_level(green); + blue_->set_level(blue); + white_->set_level(white); + master1_->turn_on(); + master2_->turn_on(); } void turn_on_in_color_temperature_mode_(float temperature, float brightness)