ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
light_partition.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4#include <vector>
5
8
9namespace esphome::partition {
10
12 public:
13 AddressableSegment(light::LightState *src, int32_t src_offset, int32_t size, bool reversed)
14 : src_(static_cast<light::AddressableLight *>(src->get_output())),
15 src_offset_(src_offset),
16 size_(size),
17 reversed_(reversed) {}
18
19 light::AddressableLight *get_src() const { return this->src_; }
20 int32_t get_src_offset() const { return this->src_offset_; }
21 int32_t get_size() const { return this->size_; }
22 int32_t get_dst_offset() const { return this->dst_offset_; }
23 void set_dst_offset(int32_t dst_offset) { this->dst_offset_ = dst_offset; }
24 bool is_reversed() const { return this->reversed_; }
25
26 protected:
28 int32_t src_offset_;
29 int32_t size_;
30 int32_t dst_offset_;
32};
33
35 public:
36 explicit PartitionLightOutput(std::vector<AddressableSegment> segments)
37 : segments_(std::move(segments)), internal_accessor_(this) {
38 int32_t off = 0;
39 for (auto &seg : this->segments_) {
40 seg.set_dst_offset(off);
41 off += seg.get_size();
42 }
43 this->accessor_ = &this->internal_accessor_;
44 }
45 int32_t size() const override {
46 auto &last_seg = this->segments_[this->segments_.size() - 1];
47 return last_seg.get_dst_offset() + last_seg.get_size();
48 }
49 void clear_effect_data() override {
50 for (auto &seg : this->segments_) {
51 seg.get_src()->clear_effect_data();
52 }
53 }
54 light::LightTraits get_traits() override { return this->segments_[0].get_src()->get_traits(); }
56 for (auto seg : this->segments_) {
57 seg.get_src()->schedule_show();
58 }
59 this->mark_shown_();
60 }
61
62 protected:
64 public:
66
67 Color get(int32_t index) const override {
68 light::ESPColorView color_view = this->parent_->get_color_view_(index);
69 return this->parent_->get_color_correction_(color_view)->color_uncorrect(color_view.get_raw());
70 }
71 uint8_t get_channel(int32_t index, uint8_t channel) const override {
72 light::ESPColorView color_view = this->parent_->get_color_view_(index);
73 return this->parent_->get_color_correction_(color_view)
74 ->color_uncorrect_channel(channel, color_view.get_channel_raw(channel));
75 }
76 Color get_raw(int32_t index) const override { return this->parent_->get_color_view_(index).get_raw(); }
77 uint8_t get_channel_raw(int32_t index, uint8_t channel) const override {
78 return this->parent_->get_color_view_(index).get_channel_raw(channel);
79 }
80 uint8_t get_effect_data(int32_t index) const override {
81 return this->parent_->get_color_view_(index).get_effect_data();
82 }
83
84 void set(int32_t index, Color color) override {
85 light::ESPColorView color_view = this->parent_->get_color_view_(index);
86 color_view.set_raw(this->parent_->get_color_correction_(color_view)->color_correct(color));
87 }
88 void set_channel(int32_t index, uint8_t channel, uint8_t value) override {
89 light::ESPColorView color_view = this->parent_->get_color_view_(index);
90 color_view.set_channel_raw(
91 channel, this->parent_->get_color_correction_(color_view)->color_correct_channel(channel, value));
92 }
93 void set_raw(int32_t index, Color raw_color) override { this->parent_->get_color_view_(index).set_raw(raw_color); }
94 void set_channel_raw(int32_t index, uint8_t channel, uint8_t raw_value) override {
95 this->parent_->get_color_view_(index).set_channel_raw(channel, raw_value);
96 }
97 void set_effect_data(int32_t index, uint8_t value) override {
99 }
100
101#ifdef USE_LIGHT_POWER_ESTIMATION
102 const light::ESPColorCorrection *get_color_correction_internal(int32_t index) const override { // NOLINT
103 return this->parent_->get_color_correction_(this->parent_->get_color_view_(index));
104 }
105#endif
106
107 protected:
109 };
110
111 light::ESPColorView get_color_view_(int32_t index) const;
112
113 // Gets the color correction associated with the specified color view.
114 // The correction will only be valid until the next call to this method.
116#ifdef USE_LIGHT_POWER_ESTIMATION
117 // A partition has no power limit of its own so it adopts the power brightness of the underlying segment.
118 // PartitionColorAccessor calls this method to access one LED, applies color correction if needed, then
119 // discards the view. This method won't be called again until the next access thus the power brightness
120 // that is set here is guaranteed to be valid for the entire lifetime of the view that is returned.
121 const light::ESPColorCorrection *color_view_correction = color_view.get_color_correction_internal(); // NOLINT
122 const_cast<light::ESPColorCorrection &>(this->correction_)
123 .set_power_brightness(color_view_correction ? color_view_correction->get_power_brightness() : 255);
124#endif
125 return &this->correction_;
126 }
127
128 std::vector<AddressableSegment> segments_;
130};
131
132} // namespace esphome::partition
Abstract interface for accessing color and effect data for an addressable light by index.
uint8_t color_correct_channel(uint8_t channel, uint8_t value) const ESPHOME_ALWAYS_INLINE
Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE
uint8_t color_uncorrect_channel(uint8_t channel, uint8_t raw_value) const
Color color_uncorrect(Color raw_color) const
Gets or sets the color or effect data at a specific index within an color accessor.
void set_effect_data(uint8_t value) override
const ESPColorCorrection * get_color_correction_internal() const
void set_channel_raw(uint8_t channel, uint8_t raw_value)
uint8_t get_channel_raw(uint8_t channel) const
void set_raw(Color raw_color)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
AddressableSegment(light::LightState *src, int32_t src_offset, int32_t size, bool reversed)
void set_dst_offset(int32_t dst_offset)
light::AddressableLight * get_src() const
void set_effect_data(int32_t index, uint8_t value) override
uint8_t get_channel(int32_t index, uint8_t channel) const override
const light::ESPColorCorrection * get_color_correction_internal(int32_t index) const override
void set_raw(int32_t index, Color raw_color) override
uint8_t get_channel_raw(int32_t index, uint8_t channel) const override
void set_channel(int32_t index, uint8_t channel, uint8_t value) override
void set_channel_raw(int32_t index, uint8_t channel, uint8_t raw_value) override
const light::ESPColorCorrection * get_color_correction_(const light::ESPColorView &color_view) const
void write_state(light::LightState *state) override
light::LightTraits get_traits() override
light::ESPColorView get_color_view_(int32_t index) const
PartitionLightOutput(std::vector< AddressableSegment > segments)
std::vector< AddressableSegment > segments_
bool state
Definition fan.h:2
uint32_t value
Definition hal.cpp:41
uint16_t size
Definition helpers.cpp:34
const void * src
Definition hal.h:68
STL namespace.