ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
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
6namespace esphome {
7namespace e131 {
8
9static const char *const TAG = "e131_addressable_light_effect";
10static const int MAX_DATA_SIZE = (sizeof(E131Packet::values) - 1);
11
12E131AddressableLightEffect::E131AddressableLightEffect(const char *name) : AddressableLightEffect(name) {}
13
15
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
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 auto effect_name = get_name();
62 ESP_LOGV(TAG, "Applying data for '%.*s' on %d universe, for %" PRId32 "-%d.", (int) effect_name.size(),
63 effect_name.c_str(), universe, output_offset, output_end);
64
65 switch (channels_) {
66 case E131_MONO:
67 for (; output_offset < output_end; output_offset++, input_data++) {
68 auto output = (*it)[output_offset];
69 output.set(Color(input_data[0], input_data[0], input_data[0], input_data[0]));
70 }
71 break;
72
73 case E131_RGB:
74 for (; output_offset < output_end; output_offset++, input_data += 3) {
75 auto output = (*it)[output_offset];
76 output.set(
77 Color(input_data[0], input_data[1], input_data[2], (input_data[0] + input_data[1] + input_data[2]) / 3));
78 }
79 break;
80
81 case E131_RGBW:
82 for (; output_offset < output_end; output_offset++, input_data += 4) {
83 auto output = (*it)[output_offset];
84 output.set(Color(input_data[0], input_data[1], input_data[2], input_data[3]));
85 }
86 break;
87 }
88
89 it->schedule_show();
90 return true;
91}
92
93} // namespace e131
94} // namespace esphome
95#endif
bool process_(int universe, const E131Packet &packet)
void add_effect(E131AddressableLightEffect *light_effect)
Definition e131.cpp:90
void remove_effect(E131AddressableLightEffect *light_effect)
Definition e131.cpp:106
StringRef get_name() const
Returns the name of this effect.
uint16_t universe
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
size_t size
Definition helpers.h:854
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition e131.h:26