From bcb240773d377b372710fe9771291b0bc27756a8 Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 28 Sep 2025 22:05:51 +0800 Subject: [PATCH] switching to faster comms --- script.js | 102 +++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/script.js b/script.js index 99b4942..e7a4304 100644 --- a/script.js +++ b/script.js @@ -235,7 +235,9 @@ window.onload = () => { const header = [0xAA, 0x55]; const length = payload.length; - const message = [...header, commandCode, length, ...payload]; + const lengthHigh = (length >> 8) & 0xFF; + const lengthLow = length & 0xFF; + const message = [...header, commandCode, lengthHigh, lengthLow, ...payload]; const writer = port.writable.getWriter(); await writer.write(new Uint8Array(message)); @@ -384,66 +386,70 @@ window.onload = () => { return buffer; } - async function sendAnimationToESP32(port, commandCode, filename, currentAnimation, chunkSize = 256) { - const { header, frames, keyframes } = currentAnimation; - const payloadBuffer = buildAnimationPayload({ header, frames, keyframes }); - const totalSize = payloadBuffer.byteLength; - const payloadArray = new Uint8Array(payloadBuffer); + async function sendAnimationToESP32(port, commandCode, filename, currentAnimation, chunkSize = 256) { + const { header, frames, keyframes } = currentAnimation; + const payloadBuffer = buildAnimationPayload({ header, frames, keyframes }); + const totalSize = payloadBuffer.byteLength; + const payloadArray = new Uint8Array(payloadBuffer); - for (let offset = 0; offset < totalSize; offset += chunkSize) { - const end = Math.min(offset + chunkSize, totalSize); - const chunkData = payloadArray.slice(offset, end); + for (let offset = 0; offset < totalSize; offset += chunkSize) { + const end = Math.min(offset + chunkSize, totalSize); + const chunkData = payloadArray.slice(offset, end); - const HEADER1 = 0xAA; - const HEADER2 = 0x55; - const offsetHigh = (offset >> 8) & 0xFF; - const offsetLow = offset & 0xFF; - const totalHigh = (totalSize >> 8) & 0xFF; - const totalLow = totalSize & 0xFF; + const HEADER1 = 0xAA; + const HEADER2 = 0x55; + const offsetHigh = (offset >> 8) & 0xFF; + const offsetLow = offset & 0xFF; + const totalHigh = (totalSize >> 8) & 0xFF; + const totalLow = totalSize & 0xFF; - const length = 4 + chunkData.length; // offset(2) + total(2) + chunk - const lengthHigh = (length >> 8) & 0xFF; - const lengthLow = length & 0xFF; + const length = 4 + chunkData.length; // offset(2) + total(2) + chunk + const lengthHigh = (length >> 8) & 0xFF; + const lengthLow = length & 0xFF; - const packet = new Uint8Array(5 + length + 1); - packet[0] = HEADER1; - packet[1] = HEADER2; - packet[2] = commandCode; - packet[3] = lengthHigh; - packet[4] = lengthLow; - packet[5] = offsetHigh; - packet[6] = offsetLow; - packet[7] = totalHigh; - packet[8] = totalLow; - packet.set(chunkData, 9); + const packet = new Uint8Array(5 + length + 1); + packet[0] = HEADER1; + packet[1] = HEADER2; + packet[2] = commandCode; + packet[3] = lengthHigh; + packet[4] = lengthLow; + packet[5] = offsetHigh; + packet[6] = offsetLow; + packet[7] = totalHigh; + packet[8] = totalLow; + packet.set(chunkData, 9); - let checksum = commandCode ^ lengthHigh ^ lengthLow ^ offsetHigh ^ offsetLow ^ totalHigh ^ totalLow; - for (let b of chunkData) checksum ^= b; - packet[5 + length] = checksum; + let checksum = commandCode ^ lengthHigh ^ lengthLow ^ offsetHigh ^ offsetLow ^ totalHigh ^ totalLow; + for (let b of chunkData) checksum ^= b; + packet[5 + length] = checksum; - const writer = port.writable.getWriter(); - await writer.write(packet); - writer.releaseLock(); + const writer = port.writable.getWriter(); + await writer.write(packet); + writer.releaseLock(); - console.log(`Sent chunk: ${filename} offset=${offset} size=${chunkData.length}`); + console.log(`Sent chunk: ${filename} offset=${offset} size=${chunkData.length}`); + console.log(packet); + + } + + console.log("Total animation size:", totalSize); } -} function waitForOkResponse(timeoutMs = 1000) { - return new Promise((resolve) => { - const timeout = setTimeout(() => { - const index = okResponseQueue.indexOf(resolve); - if (index !== -1) okResponseQueue.splice(index, 1); - resolve(false); - }, timeoutMs); + return new Promise((resolve) => { + const timeout = setTimeout(() => { + const index = okResponseQueue.indexOf(resolve); + if (index !== -1) okResponseQueue.splice(index, 1); + resolve(false); + }, timeoutMs); - okResponseQueue.push(() => { - clearTimeout(timeout); - resolve(true); + okResponseQueue.push(() => { + clearTimeout(timeout); + resolve(true); + }); }); - }); -} + }