From 48fe73611d18d2faa7d6a6b5745a5be2dd8b7604 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sat, 20 Jan 2024 11:16:01 +0100 Subject: [PATCH] Fix: properly wait for audio --- firmware/src/main.cpp | 12 ++++++++++-- src/routes/+page.svelte | 13 ++++++++++--- src/routes/Timer.svelte | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 43888b3..14e3592 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -10,7 +10,9 @@ #include "freertos/queue.h" #include "esp_log.h" #include "esp_timer.h" +#include "esp_sleep.h" #include "driver/gpio.h" +#include "driver/rtc_io.h" #define TAG "main" #define TRIGGER_PIN GPIO_NUM_12 @@ -82,6 +84,9 @@ void handleTrigger(void *) void app_main() { + rtc_gpio_pullup_en(TRIGGER_PIN); + esp_sleep_enable_ext0_wakeup(TRIGGER_PIN, 0); + ESP_LOGI("main", "Starting BLE work!"); NimBLEDevice::init("Speed buzzer"); @@ -124,11 +129,14 @@ void app_main() for (;;) { uint64_t currentTime; - if (xQueueReceive(triggerQueue, ¤tTime, portMAX_DELAY) == pdTRUE) + if (xQueueReceive(triggerQueue, ¤tTime, 5 * 60 * 1000 / portTICK_PERIOD_MS) == pdTRUE) { bleLastTriggerTimeCharacteristic->setValue(currentTime); bleLastTriggerTimeCharacteristic->notify(); - // ESP_LOGI(TAG, "Characteristic written!"); + } + else if (bleServer->getConnectedCount() == 0) + { + esp_deep_sleep_start(); } } } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index dd69102..a828ec9 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -18,9 +18,16 @@ state = 'wait'; await delay(3000); state = 'ready'; - await new Audio('/sound/ok-ready-go.mp3').play(); - await delay(2600); - timer.start(); + const audio = new Audio('/sound/ok-ready-go.mp3'); + const audioDone = new Promise((resolve) => { + audio.onended = () => { + resolve(); + }; + }); + + await audio.play(); + await audioDone; + timer.start(200n); state = 'running'; }; diff --git a/src/routes/Timer.svelte b/src/routes/Timer.svelte index 7cec7ac..8368fa5 100644 --- a/src/routes/Timer.svelte +++ b/src/routes/Timer.svelte @@ -3,8 +3,8 @@ import { buzzerState } from '../stores'; import { Button } from 'flowbite-svelte'; - export const start = () => { - timerStartedAt = BigInt(Math.floor(performance.now())); + export const start = (offset: bigint = 0n) => { + timerStartedAt = BigInt(Math.floor(performance.now())) - offset; timerStoppedAt = undefined; };