diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 7efd6ee..43888b3 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -13,7 +13,7 @@ #include "driver/gpio.h" #define TAG "main" -#define TRIGGER_PIN GPIO_NUM_23 +#define TRIGGER_PIN GPIO_NUM_12 #define BLE_SERVICE_UUID "deea3136-d728-4f23-823a-104290000000" #define BLE_CURRENTTIME_CHARACTERISTIC_UUID "deea3136-d728-4f23-823a-104290000001" #define BLE_LASTTRIGGERTIME_CHARACTERISTIC_UUID "deea3136-d728-4f23-823a-104290000002" @@ -55,11 +55,10 @@ class LocalServerCallbacks : public NimBLEServerCallbacks class LocalCharacteristicCallbacks : public NimBLECharacteristicCallbacks { - void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override + void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override { bleCurrentTimeCharacteristic->setValue(esp_timer_get_time() / 1000); - bleCurrentTimeCharacteristic->notify(); - // ESP_LOGI(TAG, "Characteristic written!"); + // ESP_LOGI(TAG, "Characteristic read!"); } }; @@ -94,9 +93,7 @@ void app_main() bleCurrentTimeCharacteristic = bleService->createCharacteristic( BLE_CURRENTTIME_CHARACTERISTIC_UUID, - NIMBLE_PROPERTY::READ | - NIMBLE_PROPERTY::WRITE | - NIMBLE_PROPERTY::NOTIFY); + NIMBLE_PROPERTY::READ); bleCurrentTimeCharacteristic->addDescriptor(new NimBLE2904()); bleCurrentTimeCharacteristic->setCallbacks(new LocalCharacteristicCallbacks()); diff --git a/src/lib/bluetooth.ts b/src/lib/bluetooth.ts index f076ef5..c01a6a9 100644 --- a/src/lib/bluetooth.ts +++ b/src/lib/bluetooth.ts @@ -65,8 +65,6 @@ async function startBluetooth() { bluetoothService = await server.getPrimaryService(BLUETOOTH_BUZZER_SERVICE_UUID); bluetoothCharacteristics.currentTime = await bluetoothService.getCharacteristic(BLUETOOTH_BUZZER_SERVICE_BASE_UUID + '000' + (1)); - await bluetoothCharacteristics.currentTime.startNotifications(); - bluetoothCharacteristics.currentTime.addEventListener('characteristicvaluechanged', handleNotifications); bluetoothCharacteristics.lastTriggerTime = await bluetoothService.getCharacteristic(BLUETOOTH_BUZZER_SERVICE_BASE_UUID + '000' + (2)); await bluetoothCharacteristics.lastTriggerTime.startNotifications(); @@ -95,9 +93,7 @@ function handleNotifications(this: BluetoothRemoteGATTCharacteristic, event: Eve const characteristic = event.target as BluetoothRemoteGATTCharacteristic; - if (characteristic.uuid == bluetoothCharacteristics.currentTime?.uuid) - handleNewCurrentTime(new DataView(characteristic.value!.buffer, 0).getBigUint64(0, true)); - else if (characteristic.uuid == bluetoothCharacteristics.lastTriggerTime?.uuid) + if (characteristic.uuid == bluetoothCharacteristics.lastTriggerTime?.uuid) handleNewLastTriggerTime(new DataView(characteristic.value!.buffer, 0).getBigUint64(0, true)); } @@ -109,13 +105,9 @@ async function doOneTimeSync() { let sentAt = performance.now(); - const remoteTime = new Promise((resolve) => { - timeSyncResponse = resolve; - }); + const remoteTime = await bluetoothCharacteristics.currentTime.readValue(); - await bluetoothCharacteristics.currentTime.writeValueWithResponse(new ArrayBuffer(0)); - - let time = await remoteTime; + let time = remoteTime.getBigUint64(0, true); let receivedAt = performance.now(); ntp.handleTimeSync(BigInt(Math.floor(sentAt)), BigInt(Math.floor(receivedAt)), time); @@ -126,14 +118,6 @@ async function doOneTimeSync() { }); } -function handleNewCurrentTime(time: bigint) { - if (timeSyncResponse === undefined) - return; - - timeSyncResponse(time); - timeSyncResponse = undefined; -} - function handleNewLastTriggerTime(time: bigint) { if (ntp === undefined) return; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 503cefe..dd69102 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -6,6 +6,30 @@ import Timer from './Timer.svelte'; const uap = new UAParser(); + let timer: Timer; + + let state: 'idle' | 'wait' | 'ready' | 'running' = 'idle'; + + const delay = (time: number) => { + return new Promise((resolve) => setTimeout(resolve, time)); + }; + + const start = async () => { + state = 'wait'; + await delay(3000); + state = 'ready'; + await new Audio('/sound/ok-ready-go.mp3').play(); + await delay(2600); + timer.start(); + state = 'running'; + }; + + const PLACEHOLDERS = { + idle: 'Press start!', + wait: 'At your marks!', + ready: 'Ready!', + running: '' + }; $: { checkAvailability(); @@ -39,7 +63,27 @@ {/if}. {:else if $bluetoothState == 'CONNECTED'} - + + + {:else} Connecting... {/if} diff --git a/src/routes/Timer.svelte b/src/routes/Timer.svelte index 8a72d37..7cec7ac 100644 --- a/src/routes/Timer.svelte +++ b/src/routes/Timer.svelte @@ -3,6 +3,19 @@ import { buzzerState } from '../stores'; import { Button } from 'flowbite-svelte'; + export const start = () => { + timerStartedAt = BigInt(Math.floor(performance.now())); + timerStoppedAt = undefined; + }; + + export const reset = () => { + timerStartedAt = undefined; + timerStoppedAt = undefined; + currentTimerValue = undefined; + }; + + export let placeholder: string = ''; + let timerStartedAt: bigint | undefined; let timerStoppedAt: bigint | undefined; @@ -48,22 +61,19 @@ } -
+
- - {(Number(currentTimerValue ?? 0) / 1000).toFixed(3)} - + {#if placeholder.length > 0} + {placeholder} + {:else} + + {(Number(currentTimerValue ?? 0) / 1000).toFixed(3)} + + {/if}
- - diff --git a/static/sound/ok-ready-go.mp3 b/static/sound/ok-ready-go.mp3 new file mode 100644 index 0000000..5e08287 Binary files /dev/null and b/static/sound/ok-ready-go.mp3 differ