Skip to content

Commit 1510899

Browse files
authored
Merge pull request #1 from lougovsk/refactor-leddevice-udpe131
Refactor LedDeviceUdpE131 for performance moved buffer creation in LedDeviceUdpE131 from write to init function.
2 parents e46db70 + a535db3 commit 1510899

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

libsrc/leddevice/dev_net/LedDeviceUdpE131.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const int DMX_MAX = 512; // 512 usable slots
4040
LedDeviceUdpE131::LedDeviceUdpE131(const QJsonObject &deviceConfig)
4141
: ProviderUdp(deviceConfig)
4242
, _whiteAlgorithm(RGBW::WhiteAlgorithm::INVALID)
43+
, _dmxChannelCount(0)
4344
{
4445
}
4546

@@ -74,6 +75,9 @@ bool LedDeviceUdpE131::init(const QJsonObject &deviceConfig)
7475
}
7576
Debug(_log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithmStr));
7677

78+
_dmxChannelCount = (_whiteAlgorithm == RGBW::WhiteAlgorithm::WHITE_OFF) ? _ledRGBCount : _ledRGBWCount;
79+
_ledBuffer.resize(_dmxChannelCount);
80+
7781
if (_json_cid.isEmpty())
7882
{
7983
_e131_cid = QUuid::createUuid();
@@ -153,11 +157,8 @@ int LedDeviceUdpE131::write(const std::vector<ColorRgb> &ledValues)
153157
{
154158
int retVal = 0;
155159
int thisChannelCount = 0;
156-
int dmxChannelCount = (_whiteAlgorithm == RGBW::WhiteAlgorithm::WHITE_OFF) ? _ledRGBCount : _ledRGBWCount; // if white_off we expect 3 channels per LED otherwise 4
157160

158-
// Create a temporary buffer for RGB or RGBW data
159-
std::vector<uint8_t> tempBuffer(dmxChannelCount);
160-
uint8_t* rawDataPtr = tempBuffer.data();
161+
uint8_t* rawDataPtr = _ledBuffer.data();
161162

162163
int currentChannel = 0;
163164
for (const ColorRgb& color : ledValues)
@@ -180,11 +181,11 @@ int LedDeviceUdpE131::write(const std::vector<ColorRgb> &ledValues)
180181

181182
_e131_seq++;
182183

183-
for (int rawIdx = 0; rawIdx < dmxChannelCount; rawIdx++)
184+
for (int rawIdx = 0; rawIdx < _dmxChannelCount; rawIdx++)
184185
{
185186
if (rawIdx % _e131_dmx_max == 0) // start of new packet
186187
{
187-
thisChannelCount = (dmxChannelCount - rawIdx < _e131_dmx_max) ? dmxChannelCount % _e131_dmx_max : _e131_dmx_max;
188+
thisChannelCount = (_dmxChannelCount - rawIdx < _e131_dmx_max) ? _dmxChannelCount % _e131_dmx_max : _e131_dmx_max;
188189
// is this the last packet? ? ^^ last packet : ^^ earlier packets
189190

190191
prepare(_e131_universe + rawIdx / _e131_dmx_max, thisChannelCount);
@@ -194,13 +195,13 @@ int LedDeviceUdpE131::write(const std::vector<ColorRgb> &ledValues)
194195
e131_packet.property_values[1 + rawIdx % _e131_dmx_max] = rawDataPtr[rawIdx];
195196

196197
// is this the last byte of last packet || last byte of other packets
197-
if ((rawIdx == dmxChannelCount - 1) || (rawIdx % _e131_dmx_max == _e131_dmx_max - 1))
198+
if ((rawIdx == _dmxChannelCount - 1) || (rawIdx % _e131_dmx_max == _e131_dmx_max - 1))
198199
{
199200
#undef e131debug
200201
#if e131debug
201202
Debug (_log, "send packet: rawidx %d dmxchannelcount %d universe: %d, packetsz %d"
202203
, rawIdx
203-
, dmxChannelCount
204+
, _dmxChannelCount
204205
, _e131_universe + rawIdx / _e131_dmx_max
205206
, E131_DMP_DATA + 1 + thisChannelCount
206207
);

libsrc/leddevice/dev_net/LedDeviceUdpE131.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "utils/RgbToRgbw.h"
88

99
#include <QUuid>
10+
#include <vector>
1011

1112
/**
1213
*
@@ -149,6 +150,8 @@ class LedDeviceUdpE131 : public ProviderUdp
149150
// RGBW specific members
150151
RGBW::WhiteAlgorithm _whiteAlgorithm;
151152
ColorRgbw _temp_rgbw;
153+
int _dmxChannelCount;
154+
std::vector<uint8_t> _ledBuffer;
152155
};
153156

154157
#endif // LEDEVICEUDPE131_H

0 commit comments

Comments
 (0)