ESPHome  2022.6.2
light_partition.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 
7 
8 namespace esphome {
9 namespace 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_;
31  bool reversed_;
32 };
33 
35  public:
36  explicit PartitionLightOutput(std::vector<AddressableSegment> segments) : segments_(std::move(segments)) {
37  int32_t off = 0;
38  for (auto &seg : this->segments_) {
39  seg.set_dst_offset(off);
40  off += seg.get_size();
41  }
42  }
43  int32_t size() const override {
44  auto &last_seg = this->segments_[this->segments_.size() - 1];
45  return last_seg.get_dst_offset() + last_seg.get_size();
46  }
47  void clear_effect_data() override {
48  for (auto &seg : this->segments_) {
49  seg.get_src()->clear_effect_data();
50  }
51  }
52  light::LightTraits get_traits() override { return this->segments_[0].get_src()->get_traits(); }
54  for (auto seg : this->segments_) {
55  seg.get_src()->schedule_show();
56  }
57  this->mark_shown_();
58  }
59 
60  protected:
61  light::ESPColorView get_view_internal(int32_t index) const override {
62  uint32_t lo = 0;
63  uint32_t hi = this->segments_.size() - 1;
64  while (lo < hi) {
65  uint32_t mid = (lo + hi) / 2;
66  int32_t begin = this->segments_[mid].get_dst_offset();
67  int32_t end = begin + this->segments_[mid].get_size();
68  if (index < begin) {
69  hi = mid - 1;
70  } else if (index >= end) {
71  lo = mid + 1;
72  } else {
73  lo = hi = mid;
74  }
75  }
76  auto &seg = this->segments_[lo];
77  // offset within the segment
78  int32_t seg_off = index - seg.get_dst_offset();
79  // offset within the src
80  int32_t src_off;
81  if (seg.is_reversed()) {
82  src_off = seg.get_src_offset() + seg.get_size() - seg_off - 1;
83  } else {
84  src_off = seg.get_src_offset() + seg_off;
85  }
86 
87  auto view = (*seg.get_src())[src_off];
88  view.raw_set_color_correction(&this->correction_);
89  return view;
90  }
91 
92  std::vector<AddressableSegment> segments_;
93 };
94 
95 } // namespace partition
96 } // namespace esphome
AddressableSegment(light::LightState *src, int32_t src_offset, int32_t size, bool reversed)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
void set_dst_offset(int32_t dst_offset)
STL namespace.
PartitionLightOutput(std::vector< AddressableSegment > segments)
void write_state(light::LightState *state) override
light::AddressableLight * get_src() const
light::LightTraits get_traits() override
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
void raw_set_color_correction(const ESPColorCorrection *color_correction)
light::ESPColorView get_view_internal(int32_t index) const override
Definition: a4988.cpp:4
std::vector< AddressableSegment > segments_
bool state
Definition: fan.h:34