ESPHome  2024.10.2
addressable_light.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/color.h"
6 #include "esp_color_correction.h"
7 #include "esp_color_view.h"
8 #include "esp_range_view.h"
9 #include "light_output.h"
10 #include "light_state.h"
11 #include "transformers.h"
12 
13 #ifdef USE_POWER_SUPPLY
15 #endif
16 
17 namespace esphome {
18 namespace light {
19 
20 using ESPColor ESPDEPRECATED("esphome::light::ESPColor is deprecated, use esphome::Color instead.", "v1.21") = Color;
21 
24 
29 };
30 
31 class AddressableLight : public LightOutput, public Component {
32  public:
33  virtual int32_t size() const = 0;
34  ESPColorView operator[](int32_t index) const { return this->get_view_internal(interpret_index(index, this->size())); }
35  ESPColorView get(int32_t index) { return this->get_view_internal(interpret_index(index, this->size())); }
36  virtual void clear_effect_data() = 0;
37  ESPRangeView range(int32_t from, int32_t to) {
38  from = interpret_index(from, this->size());
39  to = interpret_index(to, this->size());
40  return ESPRangeView(this, from, to);
41  }
42  ESPRangeView all() { return ESPRangeView(this, 0, this->size()); }
43  ESPRangeIterator begin() { return this->all().begin(); }
44  ESPRangeIterator end() { return this->all().end(); }
45  void shift_left(int32_t amnt) {
46  if (amnt < 0) {
47  this->shift_right(-amnt);
48  return;
49  }
50  if (amnt > this->size())
51  amnt = this->size();
52  this->range(0, -amnt) = this->range(amnt, this->size());
53  }
54  void shift_right(int32_t amnt) {
55  if (amnt < 0) {
56  this->shift_left(-amnt);
57  return;
58  }
59  if (amnt > this->size())
60  amnt = this->size();
61  this->range(amnt, this->size()) = this->range(0, -amnt);
62  }
63  // Indicates whether an effect that directly updates the output buffer is active to prevent overwriting
64  bool is_effect_active() const { return this->effect_active_; }
65  void set_effect_active(bool effect_active) { this->effect_active_ = effect_active; }
66  std::unique_ptr<LightTransformer> create_default_transition() override;
67  void set_correction(float red, float green, float blue, float white = 1.0f) {
68  this->correction_.set_max_brightness(
69  Color(to_uint8_scale(red), to_uint8_scale(green), to_uint8_scale(blue), to_uint8_scale(white)));
70  }
71  void setup_state(LightState *state) override {
72  this->correction_.calculate_gamma_table(state->get_gamma_correct());
73  this->state_parent_ = state;
74  }
75  void update_state(LightState *state) override;
76  void schedule_show() { this->state_parent_->next_write_ = true; }
77 
78 #ifdef USE_POWER_SUPPLY
79  void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
80 #endif
81 
82  void call_setup() override;
83 
84  protected:
86 
87  void mark_shown_() {
88 #ifdef USE_POWER_SUPPLY
89  for (const auto &c : *this) {
90  if (c.get_red_raw() > 0 || c.get_green_raw() > 0 || c.get_blue_raw() > 0 || c.get_white_raw() > 0) {
91  this->power_.request();
92  return;
93  }
94  }
95  this->power_.unrequest();
96 #endif
97  }
98  virtual ESPColorView get_view_internal(int32_t index) const = 0;
99 
100  bool effect_active_{false};
101  ESPColorCorrection correction_{};
102 #ifdef USE_POWER_SUPPLY
104 #endif
105  LightState *state_parent_{nullptr};
106 };
107 
109  public:
111 
112  void start() override;
114 
115  protected:
117  Color target_color_{};
118  float last_transition_progress_{0.0f};
119  float accumulated_alpha_{0.0f};
120 };
121 
122 } // namespace light
123 } // namespace esphome
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
Interface to write LightStates to hardware.
Definition: light_output.h:12
float get_gamma_correct() const
Definition: light_state.h:114
void set_effect_active(bool effect_active)
void apply(Climate *climate)
Apply these settings to the climate device.
power_supply::PowerSupplyRequester power_
void set_power_supply(power_supply::PowerSupply *power_supply)
mopeka_std_values val[4]
int32_t HOT interpret_index(int32_t index, int32_t size)
virtual void call_setup()
Definition: component.cpp:78
LightState(LightOutput *output)
Definition: light_state.cpp:11
AddressableLightTransformer(AddressableLight &light)
This class represents the color state for a light object.
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
ESPColorView operator[](int32_t index) const
Color color_from_light_color_values(LightColorValues val)
Convert the color information from a LightColorValues object to a Color object (does not apply bright...
void set_correction(float red, float green, float blue, float white=1.0f)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
A half-open range of LEDs, inclusive of the begin index and exclusive of the end index, using zero-based numbering.
void setup_state(LightState *state) override
Use a custom state class for addressable lights, to allow type system to discriminate between address...
ESPRangeView range(int32_t from, int32_t to)
bool state
Definition: fan.h:34