ESPHome  2022.9.1
esp_range_view.cpp
Go to the documentation of this file.
1 #include "esp_range_view.h"
2 #include "addressable_light.h"
3 
4 namespace esphome {
5 namespace light {
6 
7 int32_t HOT interpret_index(int32_t index, int32_t size) {
8  if (index < 0)
9  return size + index;
10  return index;
11 }
12 
13 ESPColorView ESPRangeView::operator[](int32_t index) const {
14  index = interpret_index(index, this->size()) + this->begin_;
15  return (*this->parent_)[index];
16 }
17 ESPRangeIterator ESPRangeView::begin() { return {*this, this->begin_}; }
18 ESPRangeIterator ESPRangeView::end() { return {*this, this->end_}; }
19 
20 void ESPRangeView::set(const Color &color) {
21  for (int32_t i = this->begin_; i < this->end_; i++) {
22  (*this->parent_)[i] = color;
23  }
24 }
25 
26 void ESPRangeView::set_red(uint8_t red) {
27  for (auto c : *this)
28  c.set_red(red);
29 }
30 void ESPRangeView::set_green(uint8_t green) {
31  for (auto c : *this)
32  c.set_green(green);
33 }
34 void ESPRangeView::set_blue(uint8_t blue) {
35  for (auto c : *this)
36  c.set_blue(blue);
37 }
38 void ESPRangeView::set_white(uint8_t white) {
39  for (auto c : *this)
40  c.set_white(white);
41 }
42 void ESPRangeView::set_effect_data(uint8_t effect_data) {
43  for (auto c : *this)
44  c.set_effect_data(effect_data);
45 }
46 
47 void ESPRangeView::fade_to_white(uint8_t amnt) {
48  for (auto c : *this)
49  c.fade_to_white(amnt);
50 }
51 void ESPRangeView::fade_to_black(uint8_t amnt) {
52  for (auto c : *this)
53  c.fade_to_black(amnt);
54 }
55 void ESPRangeView::lighten(uint8_t delta) {
56  for (auto c : *this)
57  c.lighten(delta);
58 }
59 void 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 
93 ESPColorView ESPRangeIterator::operator*() const { return this->range_.parent_->get(this->i_); }
94 
95 } // namespace light
96 } // namespace esphome
void set_red(uint8_t red) override
void fade_to_white(uint8_t amnt) override
void set_white(uint8_t white) override
void darken(uint8_t delta) override
ESPRangeView & operator=(const Color &rhs)
int32_t HOT interpret_index(int32_t index, int32_t size)
AddressableLight * parent_
ESPColorView operator[](int32_t index) const
void fade_to_black(uint8_t amnt) override
void set(const Color &color) override
void lighten(uint8_t delta) override
Definition: a4988.cpp:4
A half-open range of LEDs, inclusive of the begin index and exclusive of the end index, using zero-based numbering.
void set_effect_data(uint8_t effect_data) override
void set_blue(uint8_t blue) override
void set_green(uint8_t green) override