feat: editing

This commit is contained in:
2023-11-02 00:16:18 +01:00
parent fade2f978e
commit ef309d603e
22 changed files with 409 additions and 517 deletions

View File

@@ -5,6 +5,7 @@ import type {Writable} from "svelte/store"
import type {CharaLayout} from "$lib/serialization/layout"
import {persistentWritable} from "$lib/storage"
import {userPreferences} from "$lib/preferences"
import settingInfo from "$lib/assets/settings.yml"
export const serialPort = writable<CharaDevice | undefined>()
@@ -15,27 +16,32 @@ export interface SerialLogEntry {
export const serialLog = writable<SerialLogEntry[]>([])
export const chords = persistentWritable<Chord[]>("chord-library", [], () => get(userPreferences).backup)
/**
* Chords as read from the device
*/
export const deviceChords = persistentWritable<Chord[]>(
"chord-library",
[],
() => get(userPreferences).backup,
)
export const layout = persistentWritable<CharaLayout>(
/**
* Layout as read from the device
*/
export const deviceLayout = persistentWritable<CharaLayout>(
"layout",
[[], [], []],
() => get(userPreferences).backup,
)
export interface Change {
layout?: Record<number, Record<number, number>>
chords?: Array<Record<"delete" | "edit" | "add", Chord>>
settings?: Record<number, number>
}
export const changes = persistentWritable<Change[]>("changes", [])
export const settings = writable({})
export const unsavedChanges = writable(new Map<number, number>())
export const highlightActions: Writable<number[]> = writable([])
/**
* Settings as read from the device
*/
export const deviceSettings = persistentWritable<number[]>(
"device-settings",
[],
() => get(userPreferences).backup,
)
export const syncStatus: Writable<"done" | "error" | "downloading" | "uploading"> = writable("done")
@@ -45,19 +51,28 @@ export async function initSerial(manual = false) {
serialPort.set(device)
syncStatus.set("downloading")
const parsedSettings: number[] = []
for (const key in settingInfo.settings) {
try {
parsedSettings[Number.parseInt(key)] = await device.getSetting(Number.parseInt(key))
} catch {}
}
deviceSettings.set(parsedSettings)
const parsedLayout: CharaLayout = [[], [], []]
for (let layer = 1; layer <= 3; layer++) {
// TODO: this will fail for LITE!
for (let i = 0; i < 90; i++) {
parsedLayout[layer - 1][i] = await device.getLayoutKey(layer, i)
}
}
layout.set(parsedLayout)
deviceLayout.set(parsedLayout)
const chordCount = await device.getChordCount()
const chordInfo = []
for (let i = 0; i < chordCount; i++) {
chordInfo.push(await device.getChord(i))
}
chords.set(chordInfo)
deviceChords.set(chordInfo)
syncStatus.set("done")
}

View File

@@ -193,7 +193,7 @@ export class CharaDevice {
if (status !== "0") throw new Error(`Failed with status ${status}`)
}
async deleteChord(chord: Chord) {
async deleteChord(chord: Pick<Chord, "actions">) {
const status = await this.send(`CML C4 ${stringifyChordActions(chord.actions)}`)
if (status.at(-1) !== "0") throw new Error(`Failed with status ${status}`)
}
@@ -205,7 +205,8 @@ export class CharaDevice {
* @param action the assigned action id
*/
async setLayoutKey(layer: number, id: number, action: number) {
const [status] = await this.send(`VAR B3 A${layer} ${id} ${action}`)
const [status] = await this.send(`VAR B4 A${layer} ${id} ${action}`)
console.log(status)
if (status !== "0") throw new Error(`Failed with status ${status}`)
}