ESPHome  2024.9.0
e131_addressable_light_effect.cpp
Go to the documentation of this file.
2 #include "e131.h"
3 #ifdef USE_NETWORK
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace e131 {
8 
9 static const char *const TAG = "e131_addressable_light_effect";
10 static const int MAX_DATA_SIZE = (sizeof(E131Packet::values) - 1);
11 
12 E131AddressableLightEffect::E131AddressableLightEffect(const std::string &name) : AddressableLightEffect(name) {}
13 
15 
16 int E131AddressableLightEffect::get_lights_per_universe() const { return MAX_DATA_SIZE / channels_; }
17 
19 
21 
23  // Round up to lights_per_universe
24  auto lights = get_lights_per_universe();
25  return (get_addressable_()->size() + lights - 1) / lights;
26 }
27 
29  AddressableLightEffect::start();
30 
31  if (this->e131_) {
32  this->e131_->add_effect(this);
33  }
34 }
35 
37  if (this->e131_) {
38  this->e131_->remove_effect(this);
39  }
40 
41  AddressableLightEffect::stop();
42 }
43 
45  // ignore, it is run by `E131Component::update()`
46 }
47 
49  auto *it = get_addressable_();
50 
51  // check if this is our universe and data are valid
52  if (universe < first_universe_ || universe > get_last_universe())
53  return false;
54 
55  int32_t output_offset = (universe - first_universe_) * get_lights_per_universe();
56  // limit amount of lights per universe and received
57  int output_end =
58  std::min(it->size(), std::min(output_offset + get_lights_per_universe(), output_offset + packet.count - 1));
59  auto *input_data = packet.values + 1;
60 
61  ESP_LOGV(TAG, "Applying data for '%s' on %d universe, for %" PRId32 "-%d.", get_name().c_str(), universe,
62  output_offset, output_end);
63 
64  switch (channels_) {
65  case E131_MONO:
66  for (; output_offset < output_end; output_offset++, input_data++) {
67  auto output = (*it)[output_offset];
68  output.set(Color(input_data[0], input_data[0], input_data[0], input_data[0]));
69  }
70  break;
71 
72  case E131_RGB:
73  for (; output_offset < output_end; output_offset++, input_data += 3) {
74  auto output = (*it)[output_offset];
75  output.set(
76  Color(input_data[0], input_data[1], input_data[2], (input_data[0] + input_data[1] + input_data[2]) / 3));
77  }
78  break;
79 
80  case E131_RGBW:
81  for (; output_offset < output_end; output_offset++, input_data += 4) {
82  auto output = (*it)[output_offset];
83  output.set(Color(input_data[0], input_data[1], input_data[2], input_data[3]));
84  }
85  break;
86  }
87 
88  it->schedule_show();
89  return true;
90 }
91 
92 } // namespace e131
93 } // namespace esphome
94 #endif
const char * name
Definition: stm32flash.h:78
bool process_(int universe, const E131Packet &packet)
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition: e131.h:24
uint16_t universe
const std::string & get_name()
Definition: light_effect.h:27
void add_effect(E131AddressableLightEffect *light_effect)
Definition: e131.cpp:78
void remove_effect(E131AddressableLightEffect *light_effect)
Definition: e131.cpp:93
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7