Skip to content

Commit

Permalink
added a Play button!
Browse files Browse the repository at this point in the history
  • Loading branch information
k12ish committed May 28, 2024
1 parent 511ced4 commit b6b8cf8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
24 changes: 12 additions & 12 deletions frontend/src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import Joi from 'joi';
export type arduinoModes = 'Regular' | 'Augmented';
// check that packet is of length 4, containing 10 bit ints
const PACKET_VALIDATOR = Joi.array().length(4).items(
Joi.number().integer().min(0).max(2 ** 10 - 1)
Joi.number().integer().min(0)
);

// Taken from https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
const timeout = (prom: Promise<IteratorResult<unknown, any>>, time: number, exception: any): Promise<any> => {
let timer;
return Promise.race([
prom,
new Promise((_r, rej) => timer = setTimeout(rej, time, exception))
]).finally(() => clearTimeout(timer));
let timer;
return Promise.race([
prom,
new Promise((_r, rej) => timer = setTimeout(rej, time, exception))
]).finally(() => clearTimeout(timer));
}

export type setModeProgress = 'timeout' | 'invalid-response' | 'ioerror';
Expand All @@ -40,11 +40,12 @@ export class ArduinoInterface {
let nextMessage = this.readDecoded.next();
let message;

for (let i = 0; i < 100; i++) {
for (let i = 0; i < 10; i++) {
await this.write(mode);
try {
await this.write(mode);
message = await timeout(nextMessage, 1000, timeoutError);
} catch (e) {
console.log(e)
if (e == timeoutError) {
callback('timeout')
} else {
Expand All @@ -55,7 +56,7 @@ export class ArduinoInterface {
let result = PACKET_VALIDATOR.validate(message.value);
if (result.error) {
callback('invalid-response');
console.log("error:", result)
console.log("invalid:", message.value)
nextMessage = this.readDecoded.next()
} else {
break;
Expand All @@ -71,10 +72,9 @@ export class ArduinoInterface {
}
}

private async write(val: any) {
let buffer = encode(val, { sortKeys: true });
private async write(val: string) {
console.log("Writing", JSON.stringify(val))
console.log(buffer)
let buffer = new TextEncoder().encode(val);
let writer = this.writeStream.getWriter();
return writer.ready.then(
() => writer.write(buffer)
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/routes/(app)/measure/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { Button, Dropdown, DropdownItem, Spinner } from 'flowbite-svelte';
import { ChevronDownOutline } from 'flowbite-svelte-icons';
import { ChevronDownOutline, PlayOutline , StopSolid } from 'flowbite-svelte-icons';
import Navbar from 'components/Navbar.svelte';
import PopupModal from 'components/PopupModal.svelte';
Expand Down Expand Up @@ -82,6 +82,10 @@
<DropdownItem on:click={() => dropdownClick('Regular')}>Regular</DropdownItem>
<DropdownItem on:click={() => dropdownClick('Augmented')}>Augmented</DropdownItem>
</Dropdown>
<div class="px-2"/>
<Button color="light" on:click={() => ard.run()}>
<PlayOutline/>
</Button>
</Navbar>

<div class="chart-container mx-auto rounded border p-8">
Expand Down

0 comments on commit b6b8cf8

Please sign in to comment.