Add mutex to global race data.

This commit is contained in:
bissen21 committed 2026-11-06 15:22:02 -06:00
1 parent f12c812d73
commit 3ec87ada9c
3 files changed
+66 -33

No files matched your search

+23 -3
View File
@@ -5,6 +5,7 @@
#include "F1_api.h"
#include "FreeRTOS.h"
#include "task.h"
#include <semphr.h>
#include "config.h"
@@ -21,7 +22,9 @@ static constexpr UWORD COUNTER_HEIGHT = 24;
static constexpr UWORD COUNTER_Y = 20;
static UBYTE counterBuffer[(COUNTER_WIDTH / 8) * COUNTER_HEIGHT];
static int counter = 0;
race_info next_race;
race_info_t race_data;
SemaphoreHandle_t dataMutex;
void wifi_connect() {
WiFi.setHostname(DEVICE_HOSTNAME);
@@ -78,6 +81,7 @@ bool readLocalTime(struct tm* result) {
}
void display_loop(void *pvParameters) {
static race_info_t local_data;
while (true) {
static unsigned long lastRefresh = 0;
static char counterStr[32];
@@ -91,12 +95,22 @@ void display_loop(void *pvParameters) {
counter++;
if (next_race.session_key == 0) {
if (xSemaphoreTake(dataMutex, MUTEX_TIMEOUT_TICKS)) {
local_data = race_data;
xSemaphoreGive(dataMutex);
//Display will continue to use old data
} else {
Serial.print("");
Serial.println("Failed to acquire mutex in display thread!");
}
if (race_data.session_key == 0) {
snprintf(counterStr, sizeof(counterStr), "Counter: %d", counter);
Serial.print("");
Serial.println(counterStr);
} else {
snprintf(counterStr, sizeof(counterStr), "%s", next_race.circuit_short_name);
snprintf(counterStr, sizeof(counterStr), "%s", race_data.circuit_short_name);
}
Paint_Clear(WHITE);
@@ -116,6 +130,12 @@ void display_loop(void *pvParameters) {
void setup() {
Serial.begin(115200);
dataMutex = xSemaphoreCreateMutex();
if (dataMutex == NULL) {
Serial.println("Failed to create mutex!");
while(1);
}
if (DEV_Module_Init() != 0) {
Serial.println("Failed to initialize display module.");
return;