ESPHome 2025.5.0
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 {
5namespace light {
6
7int32_t HOT interpret_index(int32_t index, int32_t size) {
8 if (index < 0)
9 return size + index;
10 return index;
11}
12
14 index = interpret_index(index, this->size()) + this->begin_;
15 return (*this->parent_)[index];
16}
17ESPRangeIterator ESPRangeView::begin() { return {*this, this->begin_}; }
18ESPRangeIterator ESPRangeView::end() { return {*this, this->end_}; }
19
20void ESPRangeView::set(const Color &color) {
21 for (int32_t i = this->begin_; i < this->end_; i++) {
22 (*this->parent_)[i] = color;
23 }
24}
25
26void ESPRangeView::set_red(uint8_t red) {
27 for (auto c : *this)
28 c.set_red(red);
29}
30void ESPRangeView::set_green(uint8_t green) {
31 for (auto c : *this)
32 c.set_green(green);
33}
34void ESPRangeView::set_blue(uint8_t blue) {
35 for (auto c : *this)
36 c.set_blue(blue);
37}
38void ESPRangeView::set_white(uint8_t white) {
39 for (auto c : *this)
40 c.set_white(white);
41}
42void ESPRangeView::set_effect_data(uint8_t effect_data) {
43 for (auto c : *this)
44 c.set_effect_data(effect_data);
45}
46
47void ESPRangeView::fade_to_white(uint8_t amnt) {
48 for (auto c : *this)
49 c.fade_to_white(amnt);
50}
51void ESPRangeView::fade_to_black(uint8_t amnt) {
52 for (auto c : *this)
53 c.fade_to_black(amnt);
54}
55void ESPRangeView::lighten(uint8_t delta) {
56 for (auto c : *this)
57 c.lighten(delta);
58}
59void ESPRangeView::darken(uint8_t delta) {
60 for (auto c : *this)
61 c.darken(delta);
62}
64 // If size doesn't match, error (todo warning)
65 if (rhs.size() != this->size())
66 return *this;
67
68 if (this->parent_ != rhs.parent_) {
69 for (int32_t i = 0; i < this->size(); i++)
70 (*this)[i].set(rhs[i].get());
71 return *this;
72 }
73
74 // If both equal, already done
75 if (rhs.begin_ == this->begin_)
76 return *this;
77
78 if (rhs.begin_ > this->begin_) {
79 // Copy from left
80 for (int32_t i = 0; i < this->size(); i++) {
81 (*this)[i].set(rhs[i].get());
82 }
83 } else {
84 // Copy from right
85 for (int32_t i = this->size() - 1; i >= 0; i--) {
86 (*this)[i].set(rhs[i].get());
87 }
88 }
89
90 return *this;
91}
92
94
95} // namespace light
96} // namespace esphome
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7