ESPHome 2026.6.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::e131 {
7
8static const char *const TAG = "e131_addressable_light_effect";
9static const int MAX_DATA_SIZE = (sizeof(E131Packet::values) - 1);
10
11E131AddressableLightEffect::E131AddressableLightEffect(const char *name) : AddressableLightEffect(name) {}
12
14
16
18
20
22 // Round up to lights_per_universe
23 auto lights = get_lights_per_universe();
24 return (get_addressable_()->size() + lights - 1) / lights;
25}
26
28 AddressableLightEffect::start();
29
30 if (this->e131_) {
31 this->e131_->add_effect(this);
32 }
33}
34
36 if (this->e131_) {
37 this->e131_->remove_effect(this);
38 }
39
40 AddressableLightEffect::stop();
41}
42
44 // ignore, it is run by `E131Component::update()`
45}
46
48 auto *it = get_addressable_();
49
50 // check if this is our universe and data are valid
52 return false;
53
54 int32_t output_offset = (universe - first_universe_) * get_lights_per_universe();
55 // limit amount of lights per universe and received
56 // packet.count is the number of DMX bytes including start code; divide by channels to get the number of lights
57 int lights_in_packet = (packet.count > 0) ? (packet.count - 1) / channels_ : 0;
58 int output_end = std::min({it->size(), output_offset + get_lights_per_universe(), output_offset + lights_in_packet});
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 esphome::e131
94
95#endif
bool process_(int universe, const E131Packet &packet)
void add_effect(E131AddressableLightEffect *light_effect)
Definition e131.cpp:89
void remove_effect(E131AddressableLightEffect *light_effect)
Definition e131.cpp:105
StringRef get_name() const
Returns the name of this effect.
uint16_t universe
uint16_t size
Definition helpers.cpp:25
uint8_t values[E131_MAX_PROPERTY_VALUES_COUNT]
Definition e131.h:25