Skip to content

Commit 1612ac5

Browse files
committed
use proper locking on bsp level
1 parent 192e2b3 commit 1612ac5

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

apps/tic_tac_toe/main/tic_tac_toe.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
static lv_obj_t *btn_grid[3][3];
1313
static char board[3][3];
1414
static bool player_turn = true; // true for 'X', false for 'O'
15-
static bool event_in_progress = false; // Flag for event handling
16-
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; // Mutex for critical sections
1715

1816
static void reset_game() {
17+
bsp_display_lock(0);
1918
memset(board, 0, sizeof(board));
2019
for (int row = 0; row < 3; ++row) {
2120
for (int col = 0; col < 3; ++col) {
@@ -25,21 +24,26 @@ static void reset_game() {
2524
}
2625
}
2726
player_turn = true;
27+
bsp_display_unlock();
2828
}
2929

3030
static void close_message_box(lv_timer_t *timer) {
3131
lv_obj_t *msg_box = (lv_obj_t *)lv_timer_get_user_data(timer);
32+
bsp_display_lock(0);
3233
lv_msgbox_close(msg_box);
3334
lv_timer_delete(timer);
35+
bsp_display_unlock();
3436
reset_game();
3537
}
3638

3739
static void show_message(const char *text) {
3840
ESP_LOGI(TAG, "%s", text);
41+
bsp_display_lock(0);
3942
lv_obj_t *msg_box = lv_msgbox_create(NULL);
4043
lv_msgbox_add_text(msg_box, text);
4144
lv_obj_center(msg_box);
4245
lv_timer_create(close_message_box, 2000, msg_box);
46+
bsp_display_unlock();
4347
}
4448

4549
static bool check_winner(char player) {
@@ -60,18 +64,11 @@ static bool check_winner(char player) {
6064
}
6165

6266
static void event_handler(lv_event_t *e) {
63-
taskENTER_CRITICAL(&mux); // Enter critical section
64-
if (event_in_progress) {
65-
taskEXIT_CRITICAL(&mux); // Exit critical section if an event is already in progress
66-
return;
67-
}
68-
event_in_progress = true;
69-
taskEXIT_CRITICAL(&mux); // Exit critical section
70-
7167
lv_event_code_t code = lv_event_get_code(e);
7268
lv_obj_t *btn = lv_event_get_target(e);
7369

7470
if (code == LV_EVENT_CLICKED) {
71+
bsp_display_lock(0);
7572
uint32_t id = (uint32_t)lv_event_get_user_data(e);
7673
uint8_t row = id / 3;
7774
uint8_t col = id % 3;
@@ -88,11 +85,8 @@ static void event_handler(lv_event_t *e) {
8885
player_turn = !player_turn;
8986
}
9087
}
88+
bsp_display_unlock();
9189
}
92-
93-
taskENTER_CRITICAL(&mux); // Enter critical section
94-
event_in_progress = false; // Mark the event as processed
95-
taskEXIT_CRITICAL(&mux); // Exit critical section
9690
}
9791

9892
void lvgl_task(void *pvParameter) {

0 commit comments

Comments
 (0)