diff --git a/src/lib/serial/connection.ts b/src/lib/serial/connection.ts index dde8bd53..35d8e71a 100644 --- a/src/lib/serial/connection.ts +++ b/src/lib/serial/connection.ts @@ -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(); @@ -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 diff --git a/src/lib/serial/device.ts b/src/lib/serial/device.ts index 561017c8..1c2499cc 100644 --- a/src/lib/serial/device.ts +++ b/src/lib/serial/device.ts @@ -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) { const status = await this.send(1, [ "CML", diff --git a/src/routes/(app)/config/EditActions.svelte b/src/routes/(app)/config/EditActions.svelte index dc3efeba..dbae9443 100644 --- a/src/routes/(app)/config/EditActions.svelte +++ b/src/routes/(app)/config/EditActions.svelte @@ -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,