switching to faster comms
parent
84d24d93a2
commit
bcb240773d
102
script.js
102
script.js
|
|
@ -235,7 +235,9 @@ window.onload = () => {
|
||||||
|
|
||||||
const header = [0xAA, 0x55];
|
const header = [0xAA, 0x55];
|
||||||
const length = payload.length;
|
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();
|
const writer = port.writable.getWriter();
|
||||||
await writer.write(new Uint8Array(message));
|
await writer.write(new Uint8Array(message));
|
||||||
|
|
@ -384,66 +386,70 @@ window.onload = () => {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendAnimationToESP32(port, commandCode, filename, currentAnimation, chunkSize = 256) {
|
async function sendAnimationToESP32(port, commandCode, filename, currentAnimation, chunkSize = 256) {
|
||||||
const { header, frames, keyframes } = currentAnimation;
|
const { header, frames, keyframes } = currentAnimation;
|
||||||
const payloadBuffer = buildAnimationPayload({ header, frames, keyframes });
|
const payloadBuffer = buildAnimationPayload({ header, frames, keyframes });
|
||||||
const totalSize = payloadBuffer.byteLength;
|
const totalSize = payloadBuffer.byteLength;
|
||||||
const payloadArray = new Uint8Array(payloadBuffer);
|
const payloadArray = new Uint8Array(payloadBuffer);
|
||||||
|
|
||||||
for (let offset = 0; offset < totalSize; offset += chunkSize) {
|
for (let offset = 0; offset < totalSize; offset += chunkSize) {
|
||||||
const end = Math.min(offset + chunkSize, totalSize);
|
const end = Math.min(offset + chunkSize, totalSize);
|
||||||
const chunkData = payloadArray.slice(offset, end);
|
const chunkData = payloadArray.slice(offset, end);
|
||||||
|
|
||||||
const HEADER1 = 0xAA;
|
const HEADER1 = 0xAA;
|
||||||
const HEADER2 = 0x55;
|
const HEADER2 = 0x55;
|
||||||
const offsetHigh = (offset >> 8) & 0xFF;
|
const offsetHigh = (offset >> 8) & 0xFF;
|
||||||
const offsetLow = offset & 0xFF;
|
const offsetLow = offset & 0xFF;
|
||||||
const totalHigh = (totalSize >> 8) & 0xFF;
|
const totalHigh = (totalSize >> 8) & 0xFF;
|
||||||
const totalLow = totalSize & 0xFF;
|
const totalLow = totalSize & 0xFF;
|
||||||
|
|
||||||
const length = 4 + chunkData.length; // offset(2) + total(2) + chunk
|
const length = 4 + chunkData.length; // offset(2) + total(2) + chunk
|
||||||
const lengthHigh = (length >> 8) & 0xFF;
|
const lengthHigh = (length >> 8) & 0xFF;
|
||||||
const lengthLow = length & 0xFF;
|
const lengthLow = length & 0xFF;
|
||||||
|
|
||||||
const packet = new Uint8Array(5 + length + 1);
|
const packet = new Uint8Array(5 + length + 1);
|
||||||
packet[0] = HEADER1;
|
packet[0] = HEADER1;
|
||||||
packet[1] = HEADER2;
|
packet[1] = HEADER2;
|
||||||
packet[2] = commandCode;
|
packet[2] = commandCode;
|
||||||
packet[3] = lengthHigh;
|
packet[3] = lengthHigh;
|
||||||
packet[4] = lengthLow;
|
packet[4] = lengthLow;
|
||||||
packet[5] = offsetHigh;
|
packet[5] = offsetHigh;
|
||||||
packet[6] = offsetLow;
|
packet[6] = offsetLow;
|
||||||
packet[7] = totalHigh;
|
packet[7] = totalHigh;
|
||||||
packet[8] = totalLow;
|
packet[8] = totalLow;
|
||||||
packet.set(chunkData, 9);
|
packet.set(chunkData, 9);
|
||||||
|
|
||||||
let checksum = commandCode ^ lengthHigh ^ lengthLow ^ offsetHigh ^ offsetLow ^ totalHigh ^ totalLow;
|
let checksum = commandCode ^ lengthHigh ^ lengthLow ^ offsetHigh ^ offsetLow ^ totalHigh ^ totalLow;
|
||||||
for (let b of chunkData) checksum ^= b;
|
for (let b of chunkData) checksum ^= b;
|
||||||
packet[5 + length] = checksum;
|
packet[5 + length] = checksum;
|
||||||
|
|
||||||
const writer = port.writable.getWriter();
|
const writer = port.writable.getWriter();
|
||||||
await writer.write(packet);
|
await writer.write(packet);
|
||||||
writer.releaseLock();
|
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) {
|
function waitForOkResponse(timeoutMs = 1000) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
const index = okResponseQueue.indexOf(resolve);
|
const index = okResponseQueue.indexOf(resolve);
|
||||||
if (index !== -1) okResponseQueue.splice(index, 1);
|
if (index !== -1) okResponseQueue.splice(index, 1);
|
||||||
resolve(false);
|
resolve(false);
|
||||||
}, timeoutMs);
|
}, timeoutMs);
|
||||||
|
|
||||||
okResponseQueue.push(() => {
|
okResponseQueue.push(() => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue