mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-04-23 14:49:00 +00:00
feat: use progress api
This commit is contained in:
@@ -8,6 +8,7 @@ import { userPreferences } from "$lib/preferences";
|
|||||||
import { getMeta } from "$lib/meta/meta-storage";
|
import { getMeta } from "$lib/meta/meta-storage";
|
||||||
import type { VersionMeta } from "$lib/meta/types/meta";
|
import type { VersionMeta } from "$lib/meta/types/meta";
|
||||||
import { serial as serialPolyfill } from "web-serial-polyfill";
|
import { serial as serialPolyfill } from "web-serial-polyfill";
|
||||||
|
import semverGte from "semver/functions/gte";
|
||||||
|
|
||||||
export const serialPort = writable<CharaDevice | undefined>();
|
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() {
|
export async function sync() {
|
||||||
const device = get(serialPort);
|
const device = get(serialPort);
|
||||||
if (!device) return;
|
if (!device) return;
|
||||||
@@ -100,6 +115,9 @@ export async function sync() {
|
|||||||
device.version.toString(),
|
device.version.toString(),
|
||||||
);
|
);
|
||||||
deviceMeta.set(meta);
|
deviceMeta.set(meta);
|
||||||
|
|
||||||
|
await waitForDevice(device);
|
||||||
|
|
||||||
const chordCount = await device.getChordCount();
|
const chordCount = await device.getChordCount();
|
||||||
|
|
||||||
const maxSettings = meta.settings
|
const maxSettings = meta.settings
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ export class CharaDevice {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
readonly port: SerialPortLike,
|
readonly port: SerialPortLike,
|
||||||
public baudRate = 115200,
|
public baudRate = 921600,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
@@ -390,6 +390,11 @@ export class CharaDevice {
|
|||||||
if (status !== "0") throw new Error(`Failed with status ${status}`);
|
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">) {
|
async deleteChord(chord: Pick<Chord, "actions">) {
|
||||||
const status = await this.send(1, [
|
const status = await this.send(1, [
|
||||||
"CML",
|
"CML",
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
sync,
|
sync,
|
||||||
syncProgress,
|
syncProgress,
|
||||||
syncStatus,
|
syncStatus,
|
||||||
|
waitForDevice,
|
||||||
} from "$lib/serial/connection";
|
} from "$lib/serial/connection";
|
||||||
import ProgressButton from "$lib/ProgressButton.svelte";
|
import ProgressButton from "$lib/ProgressButton.svelte";
|
||||||
import { tick } from "svelte";
|
import { tick } from "svelte";
|
||||||
@@ -136,6 +137,7 @@
|
|||||||
empty.add(id);
|
empty.add(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
changes.update((changes) => {
|
changes.update((changes) => {
|
||||||
changes.push([
|
changes.push([
|
||||||
...empty.keys().map(
|
...empty.keys().map(
|
||||||
@@ -242,6 +244,8 @@
|
|||||||
|
|
||||||
let progressCurrent = 0;
|
let progressCurrent = 0;
|
||||||
|
|
||||||
|
await waitForDevice(port);
|
||||||
|
|
||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
syncProgress.set({
|
syncProgress.set({
|
||||||
max: progressMax,
|
max: progressMax,
|
||||||
|
|||||||
Reference in New Issue
Block a user