From 125c04492ab2840f6c5f1067d82d8d3af32e3272 Mon Sep 17 00:00:00 2001 From: zomfg Date: Sat, 24 Oct 2020 23:53:51 +0200 Subject: [PATCH] device dnrgb: split packets --- Software/src/AbstractLedDeviceUdp.hpp | 1 + Software/src/LedDeviceDnrgb.cpp | 74 ++++++++++++++++----------- Software/src/LedDeviceDnrgb.hpp | 2 +- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/Software/src/AbstractLedDeviceUdp.hpp b/Software/src/AbstractLedDeviceUdp.hpp index 592d9658d..c6a547dd6 100644 --- a/Software/src/AbstractLedDeviceUdp.hpp +++ b/Software/src/AbstractLedDeviceUdp.hpp @@ -57,6 +57,7 @@ public slots: bool writeBuffer(const QByteArray& buff); int m_timeout; + constexpr static const char InfiniteTimeout = 255; private: QUdpSocket* m_Socket; diff --git a/Software/src/LedDeviceDnrgb.cpp b/Software/src/LedDeviceDnrgb.cpp index 5132ce3a6..be05bb4ee 100644 --- a/Software/src/LedDeviceDnrgb.cpp +++ b/Software/src/LedDeviceDnrgb.cpp @@ -32,8 +32,8 @@ LedDeviceDnrgb::LedDeviceDnrgb(const QString& address, const QString& port, cons } const QString LedDeviceDnrgb::name() const -{ - return "dnrgb"; +{ + return "dnrgb"; } int LedDeviceDnrgb::maxLedsCount() @@ -44,8 +44,7 @@ int LedDeviceDnrgb::maxLedsCount() void LedDeviceDnrgb::setColors(const QList & colors) { bool ok = true; - - m_colorsSaved = colors; + bool sentPackets = false; resizeColorsBuffer(colors.count()); @@ -53,40 +52,55 @@ void LedDeviceDnrgb::setColors(const QList & colors) applyDithering(m_colorsBuffer, 8); // Send multiple buffers - uint16_t remainingColors = colors.count(); + const int totalColors = colors.count(); uint16_t startIndex = 0; - uint16_t colorsToSend = 0; - - while (remainingColors > 0) - { - m_writeBuffer.clear(); - m_writeBuffer.append(m_writeBufferHeader); - m_writeBuffer.append((char)(startIndex >> 8)); //High byte - m_writeBuffer.append((char)startIndex); //Low byte - if (remainingColors > LEDS_PER_PACKET) - { - colorsToSend = LEDS_PER_PACKET; - } - else + while (startIndex < totalColors) + { + // skip equals + while (startIndex < totalColors && + m_colorsSaved[startIndex] == colors[startIndex]) + startIndex++; + + // get diffs + QByteArray colorPacket; + uint16_t colorPacketLen = 0; + while (colorPacketLen < LedsPerPacket && + startIndex + colorPacketLen < totalColors && + m_colorsSaved[startIndex + colorPacketLen] != colors[startIndex + colorPacketLen]) { - colorsToSend = remainingColors; - } + StructRgb color = m_colorsBuffer[startIndex + colorPacketLen]; - for (uint16_t i = startIndex; i < startIndex + colorsToSend; i++) - { - StructRgb color = m_colorsBuffer[i]; + colorPacket.append(color.r); + colorPacket.append(color.g); + colorPacket.append(color.b); - m_writeBuffer.append(color.r); - m_writeBuffer.append(color.g); - m_writeBuffer.append(color.b); + colorPacketLen++; } - startIndex += colorsToSend; - remainingColors -= colorsToSend; + if (colorPacketLen > 0) { + m_writeBuffer.clear(); + m_writeBuffer.append(m_writeBufferHeader); + m_writeBuffer.append((char)(startIndex >> 8)); //High byte + m_writeBuffer.append((char)startIndex); //Low byte + m_writeBuffer.append(colorPacket); + startIndex += colorPacketLen; + ok &= writeBuffer(m_writeBuffer); + sentPackets = true; + } + } + + // if no packets are sent, send empty packet to not timeout + if (!sentPackets && m_timeout != InfiniteTimeout) { + m_writeBuffer.clear(); + m_writeBuffer.append(m_writeBufferHeader); + m_writeBuffer.append((char)0); + m_writeBuffer.append((char)0); ok &= writeBuffer(m_writeBuffer); } + m_colorsSaved = colors; + emit commandCompleted(ok); } @@ -112,9 +126,9 @@ void LedDeviceDnrgb::switchOffLeds() m_writeBuffer.append((char)startIndex >> 8); //High byte m_writeBuffer.append((char)startIndex); //Low byte - if (remainingColors > LEDS_PER_PACKET) + if (remainingColors > LedsPerPacket) { - colorsToSend = LEDS_PER_PACKET; + colorsToSend = LedsPerPacket; } else { diff --git a/Software/src/LedDeviceDnrgb.hpp b/Software/src/LedDeviceDnrgb.hpp index 081d98022..6c3654b2b 100644 --- a/Software/src/LedDeviceDnrgb.hpp +++ b/Software/src/LedDeviceDnrgb.hpp @@ -46,5 +46,5 @@ public slots: void reinitBufferHeader(); private: - const int LEDS_PER_PACKET = 489; + constexpr static const int LedsPerPacket = 489; };