ESPHome 2026.1.0-dev
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#ifdef USE_POWER_SUPPLY
15#endif
16
17namespace esphome::light {
18
20Color color_from_light_color_values(LightColorValues val);
21
27
28class AddressableLight : public LightOutput, public Component {
29 public:
30 virtual int32_t size() const = 0;
31 ESPColorView operator[](int32_t index) const { return this->get_view_internal(interpret_index(index, this->size())); }
32 ESPColorView get(int32_t index) { return this->get_view_internal(interpret_index(index, this->size())); }
33 virtual void clear_effect_data() = 0;
34 ESPRangeView range(int32_t from, int32_t to) {
35 from = interpret_index(from, this->size());
36 to = interpret_index(to, this->size());
37 return ESPRangeView(this, from, to);
38 }
39 ESPRangeView all() { return ESPRangeView(this, 0, this->size()); }
40 ESPRangeIterator begin() { return this->all().begin(); }
41 ESPRangeIterator end() { return this->all().end(); }
42 void shift_left(int32_t amnt) {
43 if (amnt < 0) {
44 this->shift_right(-amnt);
45 return;
46 }
47 if (amnt > this->size())
48 amnt = this->size();
49 this->range(0, -amnt) = this->range(amnt, this->size());
50 }
51 void shift_right(int32_t amnt) {
52 if (amnt < 0) {
53 this->shift_left(-amnt);
54 return;
55 }
56 if (amnt > this->size())
57 amnt = this->size();
58 this->range(amnt, this->size()) = this->range(0, -amnt);
59 }
60 // Indicates whether an effect that directly updates the output buffer is active to prevent overwriting
61 bool is_effect_active() const { return this->effect_active_; }
62 void set_effect_active(bool effect_active) { this->effect_active_ = effect_active; }
63 std::unique_ptr<LightTransformer> create_default_transition() override;
64 void set_correction(float red, float green, float blue, float white = 1.0f) {
66 Color(to_uint8_scale(red), to_uint8_scale(green), to_uint8_scale(blue), to_uint8_scale(white)));
67 }
68 void setup_state(LightState *state) override {
70 this->state_parent_ = state;
71 }
72 void update_state(LightState *state) override;
74
75#ifdef USE_POWER_SUPPLY
76 void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
77#endif
78
79 void call_setup() override;
80
81 protected:
83
84 void mark_shown_() {
85#ifdef USE_POWER_SUPPLY
86 for (const auto &c : *this) {
87 if (c.get_red_raw() > 0 || c.get_green_raw() > 0 || c.get_blue_raw() > 0 || c.get_white_raw() > 0) {
88 this->power_.request();
89 return;
90 }
91 }
92 this->power_.unrequest();
93#endif
94 }
95 virtual ESPColorView get_view_internal(int32_t index) const = 0;
96
99#ifdef USE_POWER_SUPPLY
101#endif
102 bool effect_active_{false};
103};
104
106 public:
108
109 void start() override;
111
112 protected:
116};
117
118} // namespace esphome::light
virtual void clear_effect_data()=0
virtual ESPColorView get_view_internal(int32_t index) const =0
void set_power_supply(power_supply::PowerSupply *power_supply)
void setup_state(LightState *state) override
ESPColorView get(int32_t index)
power_supply::PowerSupplyRequester power_
ESPColorView operator[](int32_t index) const
void set_effect_active(bool effect_active)
virtual int32_t size() const =0
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
void set_max_brightness(const Color &max_brightness)
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:91
LightState(LightOutput *output)
float get_gamma_correct() const
void schedule_write_()
Schedule a write to the light output and enable the loop to process it.
Base class for all light color transformers, such as transitions or flashes.
bool state
Definition fan.h:0
mopeka_std_values val[4]
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...
int32_t HOT interpret_index(int32_t index, int32_t size)