ESPHome 2025.12.1
Loading...
Searching...
No Matches
light_transformer.h
Go to the documentation of this file.
1#pragma once
2
3#include "esphome/core/hal.h"
6
7namespace esphome::light {
8
11 public:
12 virtual ~LightTransformer() = default;
13
14 void setup(const LightColorValues &start_values, const LightColorValues &target_values, uint32_t length) {
15 this->start_time_ = millis();
16 this->length_ = length;
17 this->start_values_ = start_values;
18 this->target_values_ = target_values;
19 this->start();
20 }
21
23 virtual bool is_finished() { return this->get_progress_() >= 1.0f; }
24
26 virtual void start() {}
27
31
33 virtual void stop() {}
34
35 const LightColorValues &get_start_values() const { return this->start_values_; }
36
37 const LightColorValues &get_target_values() const { return this->target_values_; }
38
39 protected:
40 // This looks crazy, but it reduces to 6x^5 - 15x^4 + 10x^3 which is just a smooth sigmoid-like
41 // transition from 0 to 1 on x = [0, 1]
42 static float smoothed_progress(float x) { return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); }
43
45 float get_progress_() {
47 if (now < this->start_time_)
48 return 0.0f;
49 if (now >= this->start_time_ + this->length_)
50 return 1.0f;
51
52 return clamp((now - this->start_time_) / float(this->length_), 0.0f, 1.0f);
53 }
54
59};
60
61} // namespace esphome::light
This class represents the color state for a light object.
Base class for all light color transformers, such as transitions or flashes.
const LightColorValues & get_target_values() const
virtual optional< LightColorValues > apply()=0
This will be called while the transformer is active to apply the transition to the light.
virtual void start()
This will be called before the transition is started.
virtual ~LightTransformer()=default
float get_progress_()
The progress of this transition, on a scale of 0 to 1.
void setup(const LightColorValues &start_values, const LightColorValues &target_values, uint32_t length)
static float smoothed_progress(float x)
virtual bool is_finished()
Indicates whether this transformation is finished.
const LightColorValues & get_start_values() const
virtual void stop()
This will be called after transition is finished.
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5