From 85aad4dbe5c5a20ca3335c7a4431a832bdf56846 Mon Sep 17 00:00:00 2001 From: Andrew Dunai Date: Tue, 30 Apr 2024 01:28:13 +0300 Subject: [PATCH] keira: fix nesapp termination (requires changes to nofrendo upstream) --- firmware/keira/src/apps/nes/osd.cpp | 40 ++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/firmware/keira/src/apps/nes/osd.cpp b/firmware/keira/src/apps/nes/osd.cpp index c930639c..46c398a8 100644 --- a/firmware/keira/src/apps/nes/osd.cpp +++ b/firmware/keira/src/apps/nes/osd.cpp @@ -4,7 +4,9 @@ #include #include +#include "utils/acquire.h" #include "driver.h" + #define OSD_OK 0 #define OSD_INIT_FAILED -1 @@ -23,6 +25,9 @@ extern "C" { // No need to add `extern "C"` to functions below, because it's already declared in `osd.h` +static SemaphoreHandle_t xSoundMutex = NULL; +static TaskHandle_t audioTaskHandle = NULL; + int osd_init_sound(); void* mem_alloc(int size, bool prefer_fast_memory) { @@ -85,14 +90,13 @@ int logprint(const char* string) { } int osd_init() { + xSoundMutex = xSemaphoreCreateMutex(); + xSemaphoreGive(xSoundMutex); nofrendo_log_chain_logfunc(logprint); osd_init_sound(); return 0; } -void osd_shutdown() { -} - char configfilename[] = "na"; int osd_main(int argc, char* argv[]) { config.filename = configfilename; @@ -137,7 +141,7 @@ void osd_getmouse(int* x, int* y, int* button) { #define HW_AUDIO_SAMPLERATE 22050 #define HW_AUDIO_BPS 16 static void (*audio_callback)(void* buffer, int length) = NULL; -int16_t* audio_frame; +int16_t* audio_frame = NULL; QueueHandle_t queue; int osd_init_sound() { #if LILKA_VERSION == 1 @@ -182,6 +186,12 @@ int osd_init_sound() { } void osd_stopsound() { + Acquire lock(xSoundMutex); + if (i2s_driver_uninstall(esp_i2s::I2S_NUM_0) != ESP_OK) { + lilka::serial_err("Failed to uninstall I2S driver\n"); + } + free(audio_frame); + audio_frame = NULL; audio_callback = 0; } @@ -219,17 +229,24 @@ void osd_setsound(void (*playfunc)(void* buffer, int length)) { [](void* arg) { const TickType_t xFrequency = pdMS_TO_TICKS(1000 / NES_REFRESH_RATE); TickType_t xLastWakeTime = xTaskGetTickCount(); - while (1) { + while (true) { // Call do_audio_frame 60 times per second. - do_audio_frame(); + { + Acquire lock(xSoundMutex); + if (!audio_frame) { + break; + } + do_audio_frame(); + } vTaskDelayUntil(&xLastWakeTime, xFrequency); } + vTaskDelete(NULL); }, "nes_audio", 8192, NULL, 1, - NULL, + &audioTaskHandle, 1 ); #endif @@ -245,3 +262,12 @@ void osd_getvideoinfo(vidinfo_t* info) { info->default_height = NES_SCREEN_HEIGHT; info->driver = &Driver::driver; } + +void osd_shutdown() { + osd_stopsound(); + + vTaskDelete(audioTaskHandle); + xTimerDelete(timer, 0); + + vSemaphoreDelete(xSoundMutex); +}