ESPHome 2025.5.0
Loading...
Searching...
No Matches
addressable_light.h
Go to the documentation of this file.
1#pragma once
2
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
17namespace esphome {
18namespace light {
19
20using ESPColor ESPDEPRECATED("esphome::light::ESPColor is deprecated, use esphome::Color instead.", "v1.21") = Color;
21
23Color color_from_light_color_values(LightColorValues val);
24
30
31class 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) {
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 {
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};
102#ifdef USE_POWER_SUPPLY
104#endif
106};
107
121
122} // namespace light
123} // 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:63
LightState(LightOutput *output)
float get_gamma_correct() const
bool next_write_
Whether the light value should be written in the next cycle.
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
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK