ESPHome 2025.5.0
Loading...
Searching...
No Matches
transformers.h
Go to the documentation of this file.
1#pragma once
2
6#include "light_state.h"
7#include "light_transformer.h"
8
9namespace esphome {
10namespace light {
11
13 public:
14 void start() override {
15 // When turning light on from off state, use target state and only increase brightness from zero.
16 if (!this->start_values_.is_on() && this->target_values_.is_on()) {
18 this->start_values_.set_brightness(0.0f);
19 }
20
21 // When turning light off from on state, use source state and only decrease brightness to zero. Use a second
22 // variable for transition end state, as overwriting target_values breaks LightState logic.
23 if (this->start_values_.is_on() && !this->target_values_.is_on()) {
25 this->end_values_.set_brightness(0.0f);
26 } else {
28 }
29
30 // When changing color mode, go through off state, as color modes are orthogonal and there can't be two active.
31 if (this->start_values_.get_color_mode() != this->target_values_.get_color_mode()) {
32 this->changing_color_mode_ = true;
35 }
36 }
37
39 float p = this->get_progress_();
40
41 // Halfway through, when intermediate state (off) is reached, flip it to the target, but remain off.
42 if (this->changing_color_mode_ && p > 0.5f &&
43 this->intermediate_values_.get_color_mode() != this->target_values_.get_color_mode()) {
46 }
47
50 if (this->changing_color_mode_)
51 p = p < 0.5f ? p * 2 : (p - 0.5) * 2;
52
55 }
56
57 protected:
58 // This looks crazy, but it reduces to 6x^5 - 15x^4 + 10x^3 which is just a smooth sigmoid-like
59 // transition from 0 to 1 on x = [0, 1]
60 static float smoothed_progress(float x) { return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); }
61
65};
66
68 public:
70
71 void start() override {
73 if (this->transition_length_ * 2 > this->length_)
74 this->transition_length_ = this->length_ / 2;
75
76 this->begun_lightstate_restore_ = false;
77
78 // first transition to original target
80 this->transformer_->setup(this->state_.current_values, this->target_values_, this->transition_length_);
81 }
82
85
86 if (this->transformer_ == nullptr && millis() > this->start_time_ + this->length_ - this->transition_length_) {
87 // second transition back to start value
89 this->transformer_->setup(this->state_.current_values, this->get_start_values(), this->transition_length_);
90 this->begun_lightstate_restore_ = true;
91 }
92
93 if (this->transformer_ != nullptr) {
94 result = this->transformer_->apply();
95
96 if (this->transformer_->is_finished()) {
97 this->transformer_->stop();
98 this->transformer_ = nullptr;
99 }
100 }
101
102 return result;
103 }
104
105 // Restore the original values after the flash.
106 void stop() override {
107 if (this->transformer_ != nullptr) {
108 this->transformer_->stop();
109 this->transformer_ = nullptr;
110 }
111 this->state_.current_values = this->get_start_values();
112 this->state_.remote_values = this->get_start_values();
113 this->state_.publish_state();
114 }
115
117
118 protected:
121 std::unique_ptr<LightTransformer> transformer_{nullptr};
123};
124
125} // namespace light
126} // namespace esphome
This class represents the color state for a light object.
static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion)
Linearly interpolate between the values in start to the values in end.
void set_brightness(float brightness)
Set the brightness property of these light color values. In range 0.0 to 1.0.
bool is_on() const
Get the binary true/false state of these light color values.
void set_state(float state)
Set the state of these light color values. In range from 0.0 (off) to 1.0 (on)
ColorMode get_color_mode() const
Get the color mode of these light color values.
optional< LightColorValues > apply() override
std::unique_ptr< LightTransformer > transformer_
virtual std::unique_ptr< LightTransformer > create_default_transition()
Return the default transformer used for transitions.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:63
LightColorValues remote_values
The remote color values reported to the frontend.
LightOutput * get_output() const
Get the light output associated with this object.
uint32_t get_flash_transition_length() const
void publish_state()
Publish the currently active state to the frontend.
LightColorValues current_values
The current values of the light as outputted to the light.
Definition light_state.h:94
Base class for all light color transformers, such as transitions or flashes.
float get_progress_()
The progress of this transition, on a scale of 0 to 1.
virtual bool is_finished()
Indicates whether this transformation is finished.
const LightColorValues & get_start_values() const
optional< LightColorValues > apply() override
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5