1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-27 05:03:48 +00:00

Modular light transformers (#2124)

This commit is contained in:
Oxan van Leeuwen
2021-08-11 06:51:35 +02:00
committed by GitHub
parent 11477dbc03
commit 46f17bea66
8 changed files with 195 additions and 150 deletions

View File

@@ -1,6 +1,7 @@
#include "light_state.h"
#include "esphome/core/log.h"
#include "light_state.h"
#include "light_output.h"
#include "transformers.h"
namespace esphome {
namespace light {
@@ -105,19 +106,19 @@ void LightState::loop() {
// Apply transformer (if any)
if (this->transformer_ != nullptr) {
auto values = this->transformer_->apply();
this->next_write_ = values.has_value(); // don't write if transformer doesn't want us to
if (values.has_value())
this->current_values = *values;
if (this->transformer_->is_finished()) {
this->remote_values = this->current_values = this->transformer_->get_end_values();
this->target_state_reached_callback_.call();
if (this->transformer_->publish_at_end())
this->publish_state();
this->transformer_->stop();
this->transformer_ = nullptr;
} else {
this->current_values = this->transformer_->get_values();
this->remote_values = this->transformer_->get_remote_values();
this->target_state_reached_callback_.call();
}
this->next_write_ = true;
}
// Write state to the light
if (this->next_write_) {
this->output_->write_state(this);
this->next_write_ = false;
@@ -217,18 +218,21 @@ void LightState::stop_effect_() {
}
void LightState::start_transition_(const LightColorValues &target, uint32_t length) {
this->transformer_ = make_unique<LightTransitionTransformer>(millis(), length, this->current_values, target);
this->remote_values = this->transformer_->get_remote_values();
this->transformer_ = this->output_->create_default_transition();
this->transformer_->setup(this->current_values, target, length);
this->remote_values = target;
}
void LightState::start_flash_(const LightColorValues &target, uint32_t length) {
LightColorValues end_colors = this->current_values;
LightColorValues end_colors = this->remote_values;
// If starting a flash if one is already happening, set end values to end values of current flash
// Hacky but works
if (this->transformer_ != nullptr)
end_colors = this->transformer_->get_end_values();
this->transformer_ = make_unique<LightFlashTransformer>(millis(), length, end_colors, target);
this->remote_values = this->transformer_->get_remote_values();
end_colors = this->transformer_->get_target_values();
this->transformer_ = make_unique<LightFlashTransformer>(*this);
this->transformer_->setup(end_colors, target, length);
this->remote_values = target;
}
void LightState::set_immediately_(const LightColorValues &target, bool set_remote_values) {
@@ -240,10 +244,6 @@ void LightState::set_immediately_(const LightColorValues &target, bool set_remot
this->next_write_ = true;
}
void LightState::set_transformer_(std::unique_ptr<LightTransformer> transformer) {
this->transformer_ = std::move(transformer);
}
void LightState::save_remote_values_() {
LightStateRTCState saved;
saved.color_mode = this->remote_values.get_color_mode();