ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
esp_range_view.cpp
Go to the documentation of this file.
1#include "esp_range_view.h"
2#include "addressable_light.h"
3
4namespace esphome::light {
5
6int32_t HOT interpret_index(int32_t index, int32_t size) {
7 if (index < 0)
8 return size + index;
9 return index;
10}
11
13 index = interpret_index(index, this->size()) + this->begin_;
14 return (*this->parent_)[index];
15}
16ESPRangeIterator ESPRangeView::begin() { return {*this, this->begin_}; }
17ESPRangeIterator ESPRangeView::end() { return {*this, this->end_}; }
18
19void ESPRangeView::set(const Color &color) {
20 for (int32_t i = this->begin_; i < this->end_; i++) {
21 (*this->parent_)[i] = color;
22 }
23}
24
25void ESPRangeView::set_red(uint8_t red) {
26 for (auto c : *this)
27 c.set_red(red);
28}
29void ESPRangeView::set_green(uint8_t green) {
30 for (auto c : *this)
31 c.set_green(green);
32}
33void ESPRangeView::set_blue(uint8_t blue) {
34 for (auto c : *this)
35 c.set_blue(blue);
36}
37void ESPRangeView::set_white(uint8_t white) {
38 for (auto c : *this)
39 c.set_white(white);
40}
41void ESPRangeView::set_effect_data(uint8_t effect_data) {
42 for (auto c : *this)
43 c.set_effect_data(effect_data);
44}
45
46void ESPRangeView::fade_to_white(uint8_t amnt) {
47 for (auto c : *this)
48 c.fade_to_white(amnt);
49}
50void ESPRangeView::fade_to_black(uint8_t amnt) {
51 for (auto c : *this)
52 c.fade_to_black(amnt);
53}
54void ESPRangeView::lighten(uint8_t delta) {
55 for (auto c : *this)
56 c.lighten(delta);
57}
58void ESPRangeView::darken(uint8_t delta) {
59 for (auto c : *this)
60 c.darken(delta);
61}
63 // If size doesn't match, error (todo warning)
64 if (rhs.size() != this->size())
65 return *this;
66
67 if (this->parent_ != rhs.parent_) {
68 for (int32_t i = 0; i < this->size(); i++)
69 (*this)[i].set(rhs[i].get());
70 return *this;
71 }
72
73 // If both equal, already done
74 if (rhs.begin_ == this->begin_)
75 return *this;
76
77 if (rhs.begin_ > this->begin_) {
78 // Copy from left
79 for (int32_t i = 0; i < this->size(); i++) {
80 (*this)[i].set(rhs[i].get());
81 }
82 } else {
83 // Copy from right
84 for (int32_t i = this->size() - 1; i >= 0; i--) {
85 (*this)[i].set(rhs[i].get());
86 }
87 }
88
89 return *this;
90}
91
93
94} // namespace esphome::light
ESPColorView get(int32_t index)
A half-open range of LEDs, inclusive of the begin index and exclusive of the end index,...
void set_white(uint8_t white) override
void lighten(uint8_t delta) override
void set_blue(uint8_t blue) override
void set_green(uint8_t green) override
void fade_to_white(uint8_t amnt) override
ESPRangeView & operator=(const Color &rhs)
void fade_to_black(uint8_t amnt) override
void set_effect_data(uint8_t effect_data) override
ESPColorView operator[](int32_t index) const
void set_red(uint8_t red) override
void darken(uint8_t delta) override
void set(const Color &color) override
int32_t HOT interpret_index(int32_t index, int32_t size)