auto-sync

This commit is contained in:
2023-07-05 18:19:53 +02:00
parent c1c0193dae
commit b7c9c8941b
5 changed files with 73 additions and 23 deletions

View File

@@ -1,7 +1,8 @@
<script> <script>
import {serialPort} from "$lib/serial/connection.js" import {serialPort, syncing} from "$lib/serial/connection.js"
import {browser} from "$app/environment" import {browser} from "$app/environment"
import {base} from "$app/paths" import {base} from "$app/paths"
import {slide} from "svelte/transition"
</script> </script>
<nav> <nav>
@@ -29,8 +30,11 @@
href="{base}/device-manager" href="{base}/device-manager"
title="Device Manager" title="Device Manager"
class="icon connect" class="icon connect"
class:error={$serialPort === undefined}>cable</a class:error={$serialPort === undefined}
class:syncing={$syncing}
> >
cable
</a>
<a href={base} title="Statistics" class="icon account">person</a> <a href={base} title="Statistics" class="icon account">person</a>
</div> </div>
</nav> </nav>
@@ -57,6 +61,8 @@
.icon { .icon {
cursor: pointer; cursor: pointer;
position: relative;
aspect-ratio: 1; aspect-ratio: 1;
padding: 4px; padding: 4px;
@@ -116,4 +122,38 @@
color: var(--md-sys-color-on-secondary-container); color: var(--md-sys-color-on-secondary-container);
background: var(--md-sys-color-secondary-container); background: var(--md-sys-color-secondary-container);
} }
@keyframes sync {
from {
rotate: 0deg;
}
to {
rotate: -360deg;
}
}
.connect::after {
pointer-events: none;
content: "sync";
position: absolute;
bottom: 0;
left: 0;
font-size: 16px;
font-weight: 600;
color: var(--md-sys-color-on-background);
opacity: 0;
background: var(--md-sys-color-background);
border-radius: 50%;
transition: opacity 250ms ease;
animation: sync 1s linear infinite;
}
.connect.syncing::after {
opacity: 1;
}
</style> </style>

View File

@@ -5,3 +5,9 @@ export const serialPort = writable()
/** @type {import('svelte/store').Writable<Array<{type: 'input' | 'output' | 'system'; value: string}>>} */ /** @type {import('svelte/store').Writable<Array<{type: 'input' | 'output' | 'system'; value: string}>>} */
export const serialLog = writable([]) export const serialLog = writable([])
/** @type {import('svelte/store').Writable<Array<{actions: number[]; phrase: string}>>} */
export const chords = writable([])
/** @type {import('svelte/store').Writable<boolean>} */
export const syncing = writable(false)

View File

@@ -61,6 +61,7 @@ export class CharaDevice {
*/ */
async #read() { async #read() {
return this.#reader.then(async it => { return this.#reader.then(async it => {
/** @type {string} */
const result = await it.read().then(({value}) => value) const result = await it.read().then(({value}) => value)
serialLog.update(it => { serialLog.update(it => {
it.push({ it.push({

View File

@@ -4,7 +4,7 @@
import {onMount} from "svelte" import {onMount} from "svelte"
import {applyTheme, argbFromHex, themeFromSourceColor} from "@material/material-color-utilities" import {applyTheme, argbFromHex, themeFromSourceColor} from "@material/material-color-utilities"
import Navigation from "$lib/components/Navigation.svelte" import Navigation from "$lib/components/Navigation.svelte"
import {serialPort} from "$lib/serial/connection.js" import {chords, serialPort, syncing} from "$lib/serial/connection.js"
import {CharaDevice} from "$lib/serial/device.js" import {CharaDevice} from "$lib/serial/device.js"
/** @type {import('$lib/serial/device.js').CharaDevice} */ /** @type {import('$lib/serial/device.js').CharaDevice} */
@@ -17,8 +17,17 @@
const dark = true // window.matchMedia("(prefers-color-scheme: dark)").matches const dark = true // window.matchMedia("(prefers-color-scheme: dark)").matches
applyTheme(theme, {target: document.body, dark}) applyTheme(theme, {target: document.body, dark})
syncing.set(true)
device ??= new CharaDevice() device ??= new CharaDevice()
serialPort.set(device) serialPort.set(device)
const chordCount = await device.getChordCount()
const chordInfo = []
for (let i = 0; i < chordCount; i++) {
chordInfo.push(await device.getChord(i))
}
chords.set(chordInfo)
syncing.set(false)
}) })
</script> </script>

View File

@@ -1,5 +1,5 @@
<script> <script>
import {serialPort} from "$lib/serial/connection.js" import {chords, serialPort} from "$lib/serial/connection.js"
import keySymbols from "$lib/assets/key-symbols.json" import keySymbols from "$lib/assets/key-symbols.json"
</script> </script>
@@ -11,12 +11,9 @@
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p> <p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
<h2>Chords</h2> <h2>Chords</h2>
{#if $serialPort} <p>You have {$chords.length} chords</p>
{#await $serialPort.getChordCount() then chordCount}
<p>You have {chordCount} chords</p>
<table> <table>
{#each Array.from({length: chordCount}) as _, i} {#each $chords as { phrase, actions }}
{#await $serialPort.getChord(i) then { phrase, actions }}
<tr> <tr>
<th>{phrase}</th> <th>{phrase}</th>
<td> <td>
@@ -25,11 +22,8 @@
{/each} {/each}
</td> </td>
</tr> </tr>
{/await}
{/each} {/each}
</table> </table>
{/await}
{/if}
<style lang="scss"> <style lang="scss">
table i { table i {