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 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue