mirror of
https://github.com/esphome/esphome.git
synced 2025-10-17 01:03:46 +01:00
Color mode implementation (#2012)
This commit is contained in:
@@ -18,10 +18,7 @@ class HBridgeLightOutput : public PollingComponent, public light::LightOutput {
|
||||
|
||||
light::LightTraits get_traits() override {
|
||||
auto traits = light::LightTraits();
|
||||
traits.set_supports_brightness(true); // Dimming
|
||||
traits.set_supports_rgb(false);
|
||||
traits.set_supports_rgb_white_value(true); // hbridge color
|
||||
traits.set_supports_color_temperature(false);
|
||||
traits.set_supported_color_modes({light::ColorMode::COLD_WARM_WHITE});
|
||||
return traits;
|
||||
}
|
||||
|
||||
@@ -31,11 +28,11 @@ class HBridgeLightOutput : public PollingComponent, public light::LightOutput {
|
||||
// This method runs around 60 times per second
|
||||
// We cannot do the PWM ourselves so we are reliant on the hardware PWM
|
||||
if (!this->forward_direction_) { // First LED Direction
|
||||
this->pinb_pin_->set_level(this->duty_off_);
|
||||
this->pina_pin_->set_level(this->pina_duty_);
|
||||
this->pinb_pin_->set_level(0);
|
||||
this->forward_direction_ = true;
|
||||
} else { // Second LED Direction
|
||||
this->pina_pin_->set_level(this->duty_off_);
|
||||
this->pina_pin_->set_level(0);
|
||||
this->pinb_pin_->set_level(this->pinb_duty_);
|
||||
this->forward_direction_ = false;
|
||||
}
|
||||
@@ -44,23 +41,7 @@ class HBridgeLightOutput : public PollingComponent, public light::LightOutput {
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
void write_state(light::LightState *state) override {
|
||||
float bright;
|
||||
state->current_values_as_brightness(&bright);
|
||||
|
||||
state->set_gamma_correct(0);
|
||||
float red, green, blue, white;
|
||||
state->current_values_as_rgbw(&red, &green, &blue, &white);
|
||||
|
||||
if ((white / bright) > 0.55) {
|
||||
this->pina_duty_ = (bright * (1 - (white / bright)));
|
||||
this->pinb_duty_ = bright;
|
||||
} else if (white < 0.45) {
|
||||
this->pina_duty_ = bright;
|
||||
this->pinb_duty_ = white;
|
||||
} else {
|
||||
this->pina_duty_ = bright;
|
||||
this->pinb_duty_ = bright;
|
||||
}
|
||||
state->current_values_as_cwww(&this->pina_duty_, &this->pinb_duty_, false);
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -68,7 +49,6 @@ class HBridgeLightOutput : public PollingComponent, public light::LightOutput {
|
||||
output::FloatOutput *pinb_pin_;
|
||||
float pina_duty_ = 0;
|
||||
float pinb_duty_ = 0;
|
||||
float duty_off_ = 0;
|
||||
bool forward_direction_ = false;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user