Browse Source

Transitioning to off works too now. Transitioning to on needs some more work (it is now instant, but that won't be hard to implement correctly).

pull/3/head
Maurice Makaay 3 years ago
parent
commit
abe8137b42
3 changed files with 18 additions and 65 deletions
  1. +3
    -3
      color_off.h
  2. +1
    -1
      color_translator.h
  3. +14
    -61
      light.h

+ 3
- 3
color_off.h View File

@ -13,9 +13,9 @@ class ColorOff : public GPIOOutputs {
public: public:
bool set_light_color_values(light::LightColorValues v) { bool set_light_color_values(light::LightColorValues v) {
values = v; values = v;
red = 0.0f;
green = 0.0f;
blue = 0.0f;
red = 1.0f;
green = 1.0f;
blue = 1.0f;
white = 0.0f; white = 0.0f;
return true; return true;


+ 1
- 1
color_translator.h View File

@ -27,7 +27,7 @@ public:
bool set_light_color_values(light::LightColorValues v) { bool set_light_color_values(light::LightColorValues v) {
values = v; values = v;
GPIOOutputs *delegate = nullptr;
GPIOOutputs *delegate;
// Well, not much light here! Use the off "color". // Well, not much light here! Use the off "color".
if (v.get_state() == 0.0f || v.get_brightness() == 0.0f) { if (v.get_state() == 0.0f || v.get_brightness() == 0.0f) {


+ 14
- 61
light.h View File

@ -1,21 +1,5 @@
#pragma once #pragma once
// What seems to be a bug in ESPHome transitioning: when turning on
// the device, the brightness is scaled along with the state (which
// runs from 0 to 1), but when turning off the device, the brightness
// is kept the same while the state goes down from 1 to 0. As a result
// when turning off the lamp with a transition time of 1s, the light
// stays on for 1s and then turn itself off abruptly.
//
// Reported the issue + fix at:
// https://github.com/esphome/esphome/pull/1643
//
// A work-around for this issue can be enabled using the following
// define. Note that the code provides a forward-compatible fix, so
// having this define active with a fixed ESPHome version should
// not be a problem.
#define TRANSITION_TO_OFF_BUGFIX
namespace esphome { namespace esphome {
namespace yeelight { namespace yeelight {
namespace bs2 { namespace bs2 {
@ -31,7 +15,6 @@ namespace bs2 {
public: public:
virtual bool has_active_transformer() = 0; virtual bool has_active_transformer() = 0;
virtual bool transformer_is_transition() = 0; virtual bool transformer_is_transition() = 0;
virtual light::LightColorValues get_transformer_values() = 0;
virtual light::LightColorValues get_transformer_end_values() = 0; virtual light::LightColorValues get_transformer_end_values() = 0;
virtual float get_transformer_progress() = 0; virtual float get_transformer_progress() = 0;
}; };
@ -182,48 +165,26 @@ namespace bs2 {
white_->set_level(0.0f); white_->set_level(0.0f);
master2_->turn_off(); master2_->turn_off();
master1_->turn_off(); master1_->turn_off();
#ifdef TRANSITION_TO_OFF_BUGFIX
previous_state_ = -1;
previous_brightness_ = 0;
#endif
return; return;
} }
GPIOOutputs *delegate;
if (transition_handler_->set_light_color_values(values)) { if (transition_handler_->set_light_color_values(values)) {
master2_->turn_on();
master1_->turn_on();
red_->set_level(transition_handler_->red);
green_->set_level(transition_handler_->green);
blue_->set_level(transition_handler_->blue);
white_->set_level(transition_handler_->white);
return;
transition_handler_->log("TRANSITION");
delegate = transition_handler_;
} else {
instant_handler_->set_light_color_values(values);
instant_handler_->log("INSTANT");
delegate = instant_handler_;
} }
#ifdef TRANSITION_TO_OFF_BUGFIX
// Remember the brightness that is used when the light is fully ON.
auto brightness = values.get_brightness();
if (values.get_state() == 1) {
previous_brightness_ = brightness;
}
// When transitioning towards zero brightness ...
else if (values.get_state() < previous_state_) {
// ... check if the prevous brightness is the same as the current
// brightness. If yes, then the brightness isn't being scaled ...
if (previous_brightness_ == brightness) {
// ... and we need to do that ourselves.
brightness = values.get_state() * brightness;
}
}
previous_state_ = values.get_state();
#endif
instant_handler_->set_light_color_values(values);
delegate->set_light_color_values(values);
master2_->turn_on(); master2_->turn_on();
master1_->turn_on(); master1_->turn_on();
red_->set_level(instant_handler_->red);
green_->set_level(instant_handler_->green);
blue_->set_level(instant_handler_->blue);
white_->set_level(instant_handler_->white);
red_->set_level(delegate->red);
green_->set_level(delegate->green);
blue_->set_level(delegate->blue);
white_->set_level(delegate->white);
} }
protected: protected:
@ -233,12 +194,8 @@ namespace bs2 {
ledc::LEDCOutput *white_; ledc::LEDCOutput *white_;
esphome::gpio::GPIOBinaryOutput *master1_; esphome::gpio::GPIOBinaryOutput *master1_;
esphome::gpio::GPIOBinaryOutput *master2_; esphome::gpio::GPIOBinaryOutput *master2_;
TransitionHandler *transition_handler_;
ColorTranslator *instant_handler_ = new ColorTranslator();
#ifdef TRANSITION_TO_OFF_BUGFIX
float previous_state_ = 1;
float previous_brightness_ = -1;
#endif
GPIOOutputs *transition_handler_;
GPIOOutputs *instant_handler_ = new ColorTranslator();
friend class YeelightBS2LightState; friend class YeelightBS2LightState;
@ -264,10 +221,6 @@ namespace bs2 {
return this->transformer_->is_transition(); return this->transformer_->is_transition();
} }
light::LightColorValues get_transformer_values() {
return this->transformer_->get_values();
}
light::LightColorValues get_transformer_end_values() { light::LightColorValues get_transformer_end_values() {
return this->transformer_->get_end_values(); return this->transformer_->get_end_values();
} }


Loading…
Cancel
Save