Skip to content

Commit 26e6597

Browse files
committed
Prevent race conditions due to multi-threaded LVGL access.
1 parent 1e9abeb commit 26e6597

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ static const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITIO
3131

3232

3333
static void eventHandlerMenu(lv_event_t * e) {
34+
Braccio.lvgl_lock();
3435
lv_event_code_t code = lv_event_get_code(e);
3536
lv_obj_t * obj = lv_event_get_target(e);
3637

38+
if (code == LV_EVENT_KEY && lv_indev_get_key(lv_indev_get_act()) == LV_KEY_HOME) {
39+
Braccio.lvgl_unlock();
40+
state = ZERO_POSITION;
41+
return;
42+
}
3743

38-
if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey() == BUTTON_ENTER)) {
44+
if (code == LV_EVENT_CLICKED) {
3945
uint32_t id = lv_btnmatrix_get_selected_btn(obj);
4046
const char * txt = lv_btnmatrix_get_btn_text(obj, id);
4147

@@ -93,9 +99,12 @@ static void eventHandlerMenu(lv_event_t * e) {
9399
break;
94100
}
95101
}
102+
Braccio.lvgl_unlock();
96103
}
97104

98-
void mainMenu() {
105+
void mainMenu()
106+
{
107+
Braccio.lvgl_lock();
99108
static lv_style_t style_focus;
100109
lv_style_init(&style_focus);
101110
lv_style_set_outline_color(&style_focus, lv_color_hex(COLOR_ORANGE));
@@ -127,6 +136,7 @@ void mainMenu() {
127136
lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80);
128137

129138
lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL);
139+
Braccio.lvgl_unlock();
130140

131141
Braccio.connectJoystickTo(btnm);
132142
}
@@ -149,8 +159,10 @@ void loop() {
149159
if (sample_cnt >= MAX_SAMPLES) {
150160
state = ZERO_POSITION;
151161
Serial.println("ZERO_POSITION");
162+
Braccio.lvgl_lock();
152163
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
153164
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
165+
Braccio.lvgl_unlock();
154166
}
155167
/* Capture those samples. */
156168
Braccio.positions(idx);
@@ -164,12 +176,16 @@ void loop() {
164176
if (idx >= final_idx) {
165177
Serial.println("REPLAY done");
166178
state = ZERO_POSITION;
179+
Braccio.lvgl_lock();
167180
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
168181
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
182+
Braccio.lvgl_unlock();
169183
}
170184
}
171185
delay(100);
172186
if (state != ZERO_POSITION) {
187+
Braccio.lvgl_lock();
173188
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
189+
Braccio.lvgl_unlock();
174190
}
175191
}

0 commit comments

Comments
 (0)