ESPHome 2025.12.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 {
18namespace light {
19
21Color color_from_light_color_values(LightColorValues val);
22
28
29class AddressableLight : public LightOutput, public Component {
30 public:
31 virtual int32_t size() const = 0;
32 ESPColorView operator[](int32_t index) const { return this->get_view_internal(interpret_index(index, this->size())); }
33 ESPColorView get(int32_t index) { return this->get_view_internal(interpret_index(index, this->size())); }
34 virtual void clear_effect_data() = 0;
35 ESPRangeView range(int32_t from, int32_t to) {
36 from = interpret_index(from, this->size());
37 to = interpret_index(to, this->size());
38 return ESPRangeView(this, from, to);
39 }
40 ESPRangeView all() { return ESPRangeView(this, 0, this->size()); }
41 ESPRangeIterator begin() { return this->all().begin(); }
42 ESPRangeIterator end() { return this->all().end(); }
43 void shift_left(int32_t amnt) {
44 if (amnt < 0) {
45 this->shift_right(-amnt);
46 return;
47 }
48 if (amnt > this->size())
49 amnt = this->size();
50 this->range(0, -amnt) = this->range(amnt, this->size());
51 }
52 void shift_right(int32_t amnt) {
53 if (amnt < 0) {
54 this->shift_left(-amnt);
55 return;
56 }
57 if (amnt > this->size())
58 amnt = this->size();
59 this->range(amnt, this->size()) = this->range(0, -amnt);
60 }
61 // Indicates whether an effect that directly updates the output buffer is active to prevent overwriting
62 bool is_effect_active() const { return this->effect_active_; }
63 void set_effect_active(bool effect_active) { this->effect_active_ = effect_active; }
64 std::unique_ptr<LightTransformer> create_default_transition() override;
65 void set_correction(float red, float green, float blue, float white = 1.0f) {
67 Color(to_uint8_scale(red), to_uint8_scale(green), to_uint8_scale(blue), to_uint8_scale(white)));
68 }
69 void setup_state(LightState *state) override {
71 this->state_parent_ = state;
72 }
73 void update_state(LightState *state) override;
74 void schedule_show() { this->state_parent_->next_write_ = true; }
75
76#ifdef USE_POWER_SUPPLY
77 void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
78#endif
79
80 void call_setup() override;
81
82 protected:
84
85 void mark_shown_() {
86#ifdef USE_POWER_SUPPLY
87 for (const auto &c : *this) {
88 if (c.get_red_raw() > 0 || c.get_green_raw() > 0 || c.get_blue_raw() > 0 || c.get_white_raw() > 0) {
89 this->power_.request();
90 return;
91 }
92 }
93 this->power_.unrequest();
94#endif
95 }
96 virtual ESPColorView get_view_internal(int32_t index) const = 0;
97
100#ifdef USE_POWER_SUPPLY
102#endif
103 bool effect_active_{false};
104};
105
107 public:
109
110 void start() override;
112
113 protected:
117};
118
119} // namespace light
120} // namespace esphome
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:69
LightState(LightOutput *output)
float get_gamma_correct() const
bool next_write_
Whether the light value should be written in the next cycle.
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7