ESPHome 2025.5.0
Loading...
Searching...
No Matches
light_transformer.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
6
7namespace esphome {
8namespace light {
9
12 public:
13 virtual ~LightTransformer() = default;
14
15 void setup(const LightColorValues &start_values, const LightColorValues &target_values, uint32_t length) {
16 this->start_time_ = millis();
17 this->length_ = length;
18 this->start_values_ = start_values;
19 this->target_values_ = target_values;
20 this->start();
21 }
22
24 virtual bool is_finished() { return this->get_progress_() >= 1.0f; }
25
27 virtual void start() {}
28
32
34 virtual void stop() {}
35
36 const LightColorValues &get_start_values() const { return this->start_values_; }
37
38 const LightColorValues &get_target_values() const { return this->target_values_; }
39
40 protected:
42 float get_progress_() {
43 uint32_t now = esphome::millis();
44 if (now < this->start_time_)
45 return 0.0f;
46 if (now >= this->start_time_ + this->length_)
47 return 1.0f;
48
49 return clamp((now - this->start_time_) / float(this->length_), 0.0f, 1.0f);
50 }
51
52 uint32_t start_time_;
53 uint32_t length_;
56};
57
58} // namespace light
59} // namespace esphome
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)
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.
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
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101
uint16_t length
Definition tt21100.cpp:0