mirror of
https://github.com/esphome/esphome.git
synced 2025-09-16 18:22:22 +01:00
Add transitions to light flash (#2201)
This commit is contained in:
@@ -58,7 +58,45 @@ class LightFlashTransformer : public LightTransformer {
|
||||
public:
|
||||
LightFlashTransformer(LightState &state) : state_(state) {}
|
||||
|
||||
optional<LightColorValues> apply() override { return this->get_target_values(); }
|
||||
void start() override {
|
||||
this->transition_length_ = this->state_.get_flash_transition_length();
|
||||
if (this->transition_length_ * 2 > this->length_)
|
||||
this->transition_length_ = this->length_ / 2;
|
||||
|
||||
// do not create transition if length is 0
|
||||
if (this->transition_length_ == 0)
|
||||
return;
|
||||
|
||||
// first transition to original target
|
||||
this->transformer_ = this->state_.get_output()->create_default_transition();
|
||||
this->transformer_->setup(this->state_.current_values, this->target_values_, this->transition_length_);
|
||||
}
|
||||
|
||||
optional<LightColorValues> apply() override {
|
||||
// transition transformer does not handle 0 length as progress returns nan
|
||||
if (this->transition_length_ == 0)
|
||||
return this->target_values_;
|
||||
|
||||
if (this->transformer_ != nullptr) {
|
||||
if (!this->transformer_->is_finished()) {
|
||||
return this->transformer_->apply();
|
||||
} else {
|
||||
this->transformer_->stop();
|
||||
this->transformer_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (millis() > this->start_time_ + this->length_ - this->transition_length_ &&
|
||||
!this->secondary_transition_occurred_) {
|
||||
// second transition back to start value
|
||||
this->transformer_ = this->state_.get_output()->create_default_transition();
|
||||
this->transformer_->setup(this->state_.current_values, this->get_start_values(), this->transition_length_);
|
||||
this->secondary_transition_occurred_ = true;
|
||||
}
|
||||
|
||||
// once transition is complete, don't change states until next transition
|
||||
return optional<LightColorValues>();
|
||||
}
|
||||
|
||||
// Restore the original values after the flash.
|
||||
void stop() override {
|
||||
@@ -69,6 +107,9 @@ class LightFlashTransformer : public LightTransformer {
|
||||
|
||||
protected:
|
||||
LightState &state_;
|
||||
uint32_t transition_length_;
|
||||
bool secondary_transition_occurred_{false};
|
||||
std::unique_ptr<LightTransformer> transformer_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace light
|
||||
|
Reference in New Issue
Block a user