Skip to content

Commit

Permalink
Merge pull request #402 from zomfg/feature/dnrgb-packet-split
Browse files Browse the repository at this point in the history
DNRGB split/diff packets
  • Loading branch information
psieg authored Oct 31, 2020
2 parents 8fad33e + 125c044 commit 49e1d96
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
1 change: 1 addition & 0 deletions Software/src/AbstractLedDeviceUdp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public slots:
bool writeBuffer(const QByteArray& buff);

int m_timeout;
constexpr static const char InfiniteTimeout = 255;

private:
QUdpSocket* m_Socket;
Expand Down
74 changes: 44 additions & 30 deletions Software/src/LedDeviceDnrgb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -44,49 +44,63 @@ int LedDeviceDnrgb::maxLedsCount()
void LedDeviceDnrgb::setColors(const QList<QRgb> & colors)
{
bool ok = true;

m_colorsSaved = colors;
bool sentPackets = false;

resizeColorsBuffer(colors.count());

applyColorModifications(colors, m_colorsBuffer);
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);
}

Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion Software/src/LedDeviceDnrgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ public slots:
void reinitBufferHeader();

private:
const int LEDS_PER_PACKET = 489;
constexpr static const int LedsPerPacket = 489;
};

0 comments on commit 49e1d96

Please sign in to comment.