feat: change timing

This commit is contained in:
2026-01-30 18:06:43 +01:00
parent 0ee7e02c53
commit 632297d266
2 changed files with 38 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
import { LineBreakTransformer } from "$lib/serial/line-break-transformer"; import { LineBreakTransformer } from "$lib/serial/line-break-transformer";
import { serialLog } from "$lib/serial/connection"; import { serialLog, type SerialLogEntry } from "$lib/serial/connection";
import type { Chord } from "$lib/serial/chord"; import type { Chord } from "$lib/serial/chord";
import { import {
parseChordActions, parseChordActions,
@@ -538,7 +538,6 @@ export class CharaDevice {
async updateFirmware( async updateFirmware(
file: ArrayBuffer, file: ArrayBuffer,
progress: (transferred: number, total: number) => void, progress: (transferred: number, total: number) => void,
throttle = false,
): Promise<void> { ): Promise<void> {
while (this.lock) { while (this.lock) {
await this.lock; await this.lock;
@@ -565,41 +564,49 @@ export class CharaDevice {
const writer = this.port.writable!.getWriter(); const writer = this.port.writable!.getWriter();
try { try {
await writer.write(new TextEncoder().encode(`RST OTA\r\n`)); const log: SerialLogEntry[] = [];
serialLog.update((it) => { const start = performance.now();
it.push({ writer.write(new TextEncoder().encode(`RST OTA\r\n`));
type: "input",
value: "RST OTA",
});
return it;
});
// Wait for the device to be ready // Wait for the device to be ready
const signal = await this.reader.read(); const signal = await this.reader.read();
serialLog.update((it) => { const signalTime = performance.now();
it.push({
type: "output",
value: signal.value!.trim(),
});
return it;
});
const chunkSize = 1024; const chunkSize = 128;
const chunks: Promise<void>[] = [];
for (let i = 0; i < file.byteLength; i += chunkSize) { for (let i = 0; i < file.byteLength; i += chunkSize) {
const chunk = file.slice(i, i + chunkSize); const chunk = file.slice(i, i + chunkSize);
if (throttle) { chunks.push(
await writer.ready; writer
await new Promise((resolve) => setTimeout(resolve, 1)); .write(new Uint8Array(chunk))
} .then(() => progress(i + chunk.byteLength, file.byteLength)),
await writer.write(new Uint8Array(chunk)); );
progress(i + chunk.byteLength, file.byteLength);
} }
await Promise.all(chunks);
serialLog.update((it) => { serialLog.update((it) => {
it.push({ it.push(
type: "input", {
value: `...${file.byteLength} bytes`, type: "input",
}); value: "RST OTA",
},
{
type: "system",
value: `+${(signalTime - start).toFixed(0)} ms`,
},
{
type: "output",
value: signal.value!.trim(),
},
{
type: "system",
value: `+${(performance.now() - signalTime).toFixed(0)} ms`,
},
{
type: "input",
value: `...${file.byteLength} bytes`,
},
);
return it; return it;
}); });

View File

@@ -56,13 +56,9 @@
retries = 2; retries = 2;
while (retries-- > 0 && !success) { while (retries-- > 0 && !success) {
try { try {
await port.updateFirmware( await port.updateFirmware(file, (transferred, total) => {
file, progress = transferred / total;
(transferred, total) => { });
progress = transferred / total;
},
retries === 0,
);
success = true; success = true;
} catch (e) { } catch (e) {