ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
esp_range_view.h
Go to the documentation of this file.
1#pragma once
2
3#include "esp_color_view.h"
4#include "esp_hsv_color.h"
5
6namespace esphome::light {
7
8class ESPRangeIterator;
9
13class ESPRangeView final : public ESPColorSettable {
14 public:
15 ESPRangeView(ESPColorAccessor *accessor, int32_t begin, int32_t end)
16 : accessor_(accessor), begin_(begin), end_(end < begin ? begin : end) {}
17 ESPRangeView(const ESPRangeView &) = default;
18
19 int32_t size() const { return this->end_ - this->begin_; }
20 ESPColorView operator[](int32_t index) const {
21 return ESPColorView(this->accessor_, this->begin_ + this->interpret_index_(index));
22 }
25
26 using ESPColorSettable::set; // ESPHSVColor overload
27 void set(Color color) override;
28 void set_channel(uint8_t channel, uint8_t value) override;
29
30 void set_effect_data(uint8_t effect_data) override;
31
32 void fade_to_white(uint8_t amnt) override;
33 void fade_to_black(uint8_t amnt) override;
34 void lighten(uint8_t delta) override;
35 void darken(uint8_t delta) override;
36
38 this->set(color);
39 return *this;
40 }
42 this->set_hsv(hsv_color);
43 return *this;
44 }
45 ESPRangeView &operator=(const ESPColorView &color_view) {
46 const Color color = color_view.get();
47 this->set(color);
48 return *this;
49 }
51
52 protected:
54
55 inline int32_t interpret_index_(int32_t index) const { return index < 0 ? this->size() + index : index; }
56
58 const int32_t begin_;
59 const int32_t end_;
60};
61
63 public:
64 ESPRangeIterator(ESPColorAccessor *accessor, int32_t index) : accessor_(accessor), index_(index) {}
67 this->index_++;
68 return *this;
69 }
70 bool operator!=(const ESPRangeIterator &other) const { return this->index_ != other.index_; }
71 ESPColorView operator*() const { return ESPColorView(this->accessor_, this->index_); }
72
73 protected:
75 int32_t index_;
76};
77
78inline ESPRangeIterator ESPRangeView::begin() { return {this->accessor_, this->begin_}; }
79inline ESPRangeIterator ESPRangeView::end() { return {this->accessor_, this->end_}; }
80
81} // namespace esphome::light
Abstract interface for accessing color and effect data for an addressable light by index.
Abstract interface that supports setting colors and effect data.
void set_hsv(ESPHSVColor hsv_color)
virtual void set(Color color)=0
Gets or sets the color or effect data at a specific index within an color accessor.
bool operator!=(const ESPRangeIterator &other) const
ESPRangeIterator(ESPColorAccessor *accessor, int32_t index)
ESPColorAccessor *const accessor_
ESPRangeIterator(const ESPRangeIterator &)=default
A half-open range of LEDs, inclusive of the begin index and exclusive of the end index,...
ESPRangeView & operator=(const ESPColorView &color_view)
int32_t interpret_index_(int32_t index) const
void lighten(uint8_t delta) override
ESPRangeView & operator=(ESPHSVColor hsv_color)
void set_channel(uint8_t channel, uint8_t value) override
void fade_to_white(uint8_t amnt) override
ESPRangeView(const ESPRangeView &)=default
void fade_to_black(uint8_t amnt) override
ESPColorAccessor *const accessor_
ESPRangeView & operator=(Color color)
void set_effect_data(uint8_t effect_data) override
ESPRangeView(ESPColorAccessor *accessor, int32_t begin, int32_t end)
void set(Color color) override
ESPColorView operator[](int32_t index) const
void darken(uint8_t delta) override
uint32_t value
Definition hal.cpp:41