Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNRGB split/diff packets #402

Merged
merged 1 commit into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};