Wait for packet echoes and send only changed packets

The keyboard echoes each colour packet back; wait for that echo before
sending the next one, like OpenRGB's EVision driver, instead of firing all
packets back to back. Skip packets whose colours did not change and resend
the whole frame every 500 ms so the keyboard stays under plugin control.
Add a passive listener tool for the echoes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-10-28 23:24:57 +01:00
1 parent 822787ae0f
commit 0b2a90ba94
3 files changed
+68

No files matched your search

+26
View File
@@ -85,6 +85,7 @@ export function Render() {
}
export function Shutdown(SystemSuspending) {
lastFullRefresh = 0; // always send the whole frame
sendColors(SystemSuspending ? "#000000" : shutdownColor);
}
@@ -117,16 +118,37 @@ function sendColors(overrideColor) {
writeRGBPackage(RGBData);
}
// Last packet sent per offset, so unchanged packets can be skipped
let sentPackets = {};
let lastFullRefresh = 0;
function writeRGBPackage(RGBData) {
// Must be a multiple of 3: the keyboard shows each packet as it arrives, so an LED split
// across two packets briefly flashes a mix of its old and new colour
const bytesToSend = lowFPS ? 48 : 54;
// Resend everything twice a second so the keyboard never falls back to its own effect
const now = Date.now();
const fullRefresh = now - lastFullRefresh >= 500;
if (fullRefresh) {
lastFullRefresh = now;
sentPackets = {};
}
for (let offset = 0; offset < RGBData.length; offset += bytesToSend) {
const data = RGBData.slice(offset, offset + bytesToSend);
while (data.length < bytesToSend) { data.push(0); }
const key = `${bytesToSend}:${data.join(",")}`;
if (sentPackets[offset] === key) {
continue;
}
sentPackets[offset] = key;
const packet = [0x04, 0x00, 0x00, 0x12, bytesToSend, offset & 0xFF, (offset >> 8) & 0xFF, 0x00].concat(data);
// Checksum: 16-bit sum of bytes 3..end (same result as the EVision plugin's formula)
@@ -139,6 +161,10 @@ function writeRGBPackage(RGBData) {
device.write(packet, 64);
// The keyboard echoes each packet back; wait for it (as OpenRGB does) so the next
// packet doesn't arrive while it's still processing this one
device.read([0x04], 64, 10);
// device.pause() rounds up to the Windows timer tick (up to ~15 ms), so only pause when asked
if (lowFPS) { device.pause(3); }
}