mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-02-14 13:12:41 +00:00
feat: change timing
This commit is contained in:
@@ -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;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user