Skip to content

Commit

Permalink
keira: fix nesapp termination (requires changes to nofrendo upstream)
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Apr 29, 2024
1 parent 65c3e1e commit 85aad4d
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions firmware/keira/src/apps/nes/osd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>

#include "utils/acquire.h"
#include "driver.h"

#define OSD_OK 0
#define OSD_INIT_FAILED -1

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
}

0 comments on commit 85aad4d

Please sign in to comment.