ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
addressable_light.h
Go to the documentation of this file.
1#pragma once
2
4#include "esp_color_view.h"
5#include "esp_range_view.h"
9#include "light_output.h"
10#include "light_state.h"
11#include "light_transformer.h"
12
13#include <cmath>
14
15#ifdef USE_POWER_SUPPLY
17#endif
18
19namespace esphome::light {
20
22Color color_from_light_color_values(LightColorValues val);
23
26class AddressableLightState final : public LightState {
28
29 public:
30#ifdef USE_LIGHT_POWER_ESTIMATION
31 float get_estimated_current_ma() const;
32 void loop() override;
33#endif
34};
35
36#ifdef USE_LIGHT_POWER_ESTIMATION
37// Static storage from codegen, only for a strip with power_estimation configured
39 float ma_per_level[4];
41 float max_current_ma{NAN};
42 // Set by call_setup() once size() is known; the budget is what is left for the pixels, NAN for no limit
43 float idle_ma{0.0f};
44 float budget_ma{NAN};
45 // The draw the last pass left in the buffer; a pass that sees it again has nothing new to limit
46 float last_frame_ma{NAN};
47};
48#endif
49
50class AddressableLight : public LightOutput, public Component {
51 public:
52 virtual int32_t size() const = 0;
53 ESPColorView operator[](int32_t index) const { return ESPColorView(this->accessor_, this->interpret_index_(index)); }
54 inline ESPColorView get(int32_t index) const { return (*this)[index]; }
55 virtual void clear_effect_data() = 0;
56 ESPRangeView range(int32_t from, int32_t to) {
57 return ESPRangeView(this->accessor_, this->interpret_index_(from), this->interpret_index_(to));
58 }
59 ESPRangeView all() { return ESPRangeView(this->accessor_, 0, this->size()); }
60 ESPRangeIterator begin() { return this->all().begin(); }
61 ESPRangeIterator end() { return this->all().end(); }
62 void shift_left(int32_t amnt) {
63 if (amnt < 0) {
64 this->shift_right(-amnt);
65 return;
66 }
67 if (amnt > this->size())
68 amnt = this->size();
69 this->range(0, -amnt) = this->range(amnt, this->size());
70 }
71 void shift_right(int32_t amnt) {
72 if (amnt < 0) {
73 this->shift_left(-amnt);
74 return;
75 }
76 if (amnt > this->size())
77 amnt = this->size();
78 this->range(amnt, this->size()) = this->range(0, -amnt);
79 }
80 // Indicates whether an effect that directly updates the output buffer is active to prevent overwriting
81 bool is_effect_active() const { return this->effect_active_; }
82 void set_effect_active(bool effect_active) { this->effect_active_ = effect_active; }
83 std::unique_ptr<LightTransformer> create_default_transition() override;
84 void set_correction(float red, float green, float blue, float white = 1.0f) {
86 Color(to_uint8_scale(red), to_uint8_scale(green), to_uint8_scale(blue), to_uint8_scale(white)));
87 }
88#ifdef USE_LIGHT_POWER_ESTIMATION
89 // Per-channel draw at full level and idle draw per LED
90 void set_power_estimation(PowerEstimation *storage, float ma_red, float ma_green, float ma_blue, float ma_white,
91 float idle_ma_per_led) {
92 this->power_estimation_ = storage;
93 storage->ma_per_level[0] = ma_red / 255.0f;
94 storage->ma_per_level[1] = ma_green / 255.0f;
95 storage->ma_per_level[2] = ma_blue / 255.0f;
96 storage->ma_per_level[3] = ma_white / 255.0f;
97 storage->idle_ma_per_led = idle_ma_per_led;
98 }
99 // Called after set_power_estimation()
100 void set_max_current_ma(float max_current_ma) { this->power_estimation_->max_current_ma = max_current_ma; }
101 // The draw of the frame in the buffer, NAN without power_estimation
102 float get_estimated_current_ma() const;
103#endif
104 void setup_state(LightState *state) override {
105#ifdef USE_LIGHT_GAMMA_LUT
107#endif
108 this->state_parent_ = state;
109 }
110 void update_state(LightState *state) override;
112
113#ifdef USE_POWER_SUPPLY
114 void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
115#endif
116
117 void call_setup() override;
118
119 protected:
121
122 inline int32_t interpret_index_(int32_t index) const { return index < 0 ? this->size() + index : index; }
123
124 void mark_shown_() {
125#ifdef USE_POWER_SUPPLY
126 for (const auto &view : *this) {
127 if (view.get_raw().is_on()) {
128 this->power_.request();
129 return;
130 }
131 }
132 this->power_.unrequest();
133#endif
134 }
135 // Queues the frame for the next loop and returns true until interval_us has passed since last_us
136 bool throttle_show_(uint32_t now_us, uint32_t last_us, uint32_t interval_us) {
137 if (now_us - last_us >= interval_us)
138 return false;
139 this->schedule_show();
140 return true;
141 }
142
143#ifdef USE_LIGHT_POWER_ESTIMATION
145 // Run by the light state before each frame goes out
146 void limit_power_();
147 void setup_power_budget_();
148 // slack_ma, when asked for, is one level's draw for every lit channel: the most the round-up on the way
149 // down can have added to the frame
150 float frame_ma_(float *slack_ma = nullptr) const;
151 float draw_ma_(const uint32_t sums[4]) const;
152#endif
153
157#ifdef USE_LIGHT_POWER_ESTIMATION
159#endif
160#ifdef USE_POWER_SUPPLY
162#endif
163 bool effect_active_{false};
164};
165
167 public:
169
170 void start() override;
171 optional<LightColorValues> apply() override;
172
173 protected:
180};
181
182} // namespace esphome::light
float draw_ma_(const uint32_t sums[4]) const
virtual void clear_effect_data()=0
void set_power_supply(power_supply::PowerSupply *power_supply)
void setup_state(LightState *state) override
bool throttle_show_(uint32_t now_us, uint32_t last_us, uint32_t interval_us)
float frame_ma_(float *slack_ma=nullptr) const
power_supply::PowerSupplyRequester power_
ESPColorView operator[](int32_t index) const
void set_max_current_ma(float max_current_ma)
int32_t interpret_index_(int32_t index) const
void set_effect_active(bool effect_active)
virtual int32_t size() const =0
void set_power_estimation(PowerEstimation *storage, float ma_red, float ma_green, float ma_blue, float ma_white, float idle_ma_per_led)
ESPColorView get(int32_t index) const
void update_state(LightState *state) override
void set_correction(float red, float green, float blue, float white=1.0f)
ESPRangeView range(int32_t from, int32_t to)
std::unique_ptr< LightTransformer > create_default_transition() override
Use a custom state class for addressable lights, to allow type system to discriminate between address...
optional< LightColorValues > apply() override
Abstract interface for accessing color and effect data for an addressable light by index.
void set_max_brightness(const Color &max_brightness)
void set_gamma_table(const uint16_t *table)
Gets or sets the color or effect data at a specific index within an color accessor.
A half-open range of LEDs, inclusive of the begin index and exclusive of the end index,...
Interface to write LightStates to hardware.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
LightState(LightOutput *output)
void schedule_write_()
Schedule a write to the light output and enable the loop to process it.
const uint16_t * get_gamma_table() const
Get the forward gamma lookup table, 256 PROGMEM entries.
Base class for all light color transformers, such as transitions or flashes.
static NullColorAccessor instance
Singleton instance of this accessor.
bool state
Definition fan.h:2
mopeka_std_values val[3]
Range range
Definition msa3xx.h:0
Color color_from_light_color_values(LightColorValues val)
Convert the color information from a LightColorValues object to a Color object (does not apply bright...