Skip to content

Commit

Permalink
lib: fix buzzer melody task destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Mar 11, 2024
1 parent 8c3fe35 commit b39a2f2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 16 deletions.
16 changes: 2 additions & 14 deletions sdk/lib/lilka/src/lilka/buzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,12 @@ void Buzzer::melodyTask(void* arg) {
xSemaphoreGive(buzzer->buzzerMutex);
vTaskDelay(duration / portTICK_PERIOD_MS);
}
// Start another lambda task to delete the melody task & release mutex
xSemaphoreTake(buzzer->buzzerMutex, portMAX_DELAY);
noTone(LILKA_BUZZER);
if (xTaskCreate(buzzer->melodyTaskCleanup, "melodyTaskCleanup", 2048, buzzer, 1, NULL) != pdPASS) {
serial_err("Failed to create melody task cleanup task (not enough memory?)");
// Failsafe: release the mutex & delete task.
// This is NOT thread-safe, but it's better than a deadlock.
xSemaphoreGive(buzzer->buzzerMutex);
vTaskDelete(NULL);
noTone(LILKA_BUZZER);
}
}

void Buzzer::melodyTaskCleanup(void* arg) {
Buzzer* buzzer = static_cast<Buzzer*>(arg);
vTaskDelete(buzzer->melodyTaskHandle);
// Release the mutex & delete task.
buzzer->melodyTaskHandle = NULL;
xSemaphoreGive(buzzer->buzzerMutex);
vTaskDelete(NULL);
}

void Buzzer::stop() {
Expand Down
2 changes: 0 additions & 2 deletions sdk/lib/lilka/src/lilka/buzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class Buzzer {
private:
// cppcheck-suppress unusedPrivateFunction
void _stop();
// cppcheck-suppress unusedPrivateFunction
static void melodyTaskCleanup(void* arg);

SemaphoreHandle_t buzzerMutex;
TaskHandle_t melodyTaskHandle;
Expand Down

0 comments on commit b39a2f2

Please sign in to comment.