feat: use progress api

This commit is contained in:
2026-04-22 17:21:58 +02:00
parent 40f8e3a430
commit c7f46e8711
3 changed files with 28 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import { userPreferences } from "$lib/preferences";
import { getMeta } from "$lib/meta/meta-storage";
import type { VersionMeta } from "$lib/meta/types/meta";
import { serial as serialPolyfill } from "web-serial-polyfill";
import semverGte from "semver/functions/gte";
export const serialPort = writable<CharaDevice | undefined>();
@@ -91,6 +92,20 @@ export async function initSerial(port: SerialPortLike, withSync: boolean) {
}
}
export async function waitForDevice(device: CharaDevice) {
if (semverGte(device.version, "3.1.0")) {
const startProgress = await device.getProgress();
let total = startProgress;
while (total < 1.0) {
total = await device.getProgress();
syncProgress.set({
max: 1.0 - startProgress,
current: total - startProgress,
});
}
}
}
export async function sync() {
const device = get(serialPort);
if (!device) return;
@@ -100,6 +115,9 @@ export async function sync() {
device.version.toString(),
);
deviceMeta.set(meta);
await waitForDevice(device);
const chordCount = await device.getChordCount();
const maxSettings = meta.settings

View File

@@ -143,7 +143,7 @@ export class CharaDevice {
constructor(
readonly port: SerialPortLike,
public baudRate = 115200,
public baudRate = 921600,
) {}
async init() {
@@ -390,6 +390,11 @@ export class CharaDevice {
if (status !== "0") throw new Error(`Failed with status ${status}`);
}
async getProgress() {
const [status] = await this.send(1, ["CML", "C5"]);
return Number.parseFloat(status);
}
async deleteChord(chord: Pick<Chord, "actions">) {
const status = await this.send(1, [
"CML",

View File

@@ -20,6 +20,7 @@
sync,
syncProgress,
syncStatus,
waitForDevice,
} from "$lib/serial/connection";
import ProgressButton from "$lib/ProgressButton.svelte";
import { tick } from "svelte";
@@ -136,6 +137,7 @@
empty.add(id);
}
}
changes.update((changes) => {
changes.push([
...empty.keys().map(
@@ -242,6 +244,8 @@
let progressCurrent = 0;
await waitForDevice(port);
function updateProgress() {
syncProgress.set({
max: progressMax,