ESPHome  2024.12.2
light_transformer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/hal.h"
5 #include "light_color_values.h"
6 
7 namespace esphome {
8 namespace 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 
31  virtual optional<LightColorValues> apply() = 0;
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
float get_progress_()
The progress of this transition, on a scale of 0 to 1.
virtual ~LightTransformer()=default
Base class for all light color transformers, such as transitions or flashes.
virtual void stop()
This will be called after transition is finished.
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:93
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
This class represents the color state for a light object.
virtual optional< LightColorValues > apply()=0
This will be called while the transformer is active to apply the transition to the light...
const LightColorValues & get_start_values() const
void setup(const LightColorValues &start_values, const LightColorValues &target_values, uint32_t length)
const LightColorValues & get_target_values() const
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual void start()
This will be called before the transition is started.
virtual bool is_finished()
Indicates whether this transformation is finished.