Skip to content

Commit 9d1abd3

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

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ 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

37-
3838
if (code == LV_EVENT_CLICKED || (code == LV_EVENT_KEY && Braccio.getKey() == BUTTON_ENTER)) {
3939
uint32_t id = lv_btnmatrix_get_selected_btn(obj);
4040
const char * txt = lv_btnmatrix_get_btn_text(obj, id);
@@ -93,9 +93,12 @@ static void eventHandlerMenu(lv_event_t * e) {
9393
break;
9494
}
9595
}
96+
Braccio.lvgl_unlock();
9697
}
9798

98-
void mainMenu() {
99+
void mainMenu()
100+
{
101+
Braccio.lvgl_lock();
99102
static lv_style_t style_focus;
100103
lv_style_init(&style_focus);
101104
lv_style_set_outline_color(&style_focus, lv_color_hex(COLOR_ORANGE));
@@ -127,6 +130,7 @@ void mainMenu() {
127130
lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80);
128131

129132
lv_obj_add_event_cb(btnm, eventHandlerMenu, LV_EVENT_ALL, NULL);
133+
Braccio.lvgl_unlock();
130134

131135
Braccio.connectJoystickTo(btnm);
132136
}
@@ -149,8 +153,10 @@ void loop() {
149153
if (sample_cnt >= MAX_SAMPLES) {
150154
state = ZERO_POSITION;
151155
Serial.println("ZERO_POSITION");
156+
Braccio.lvgl_lock();
152157
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
153158
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
159+
Braccio.lvgl_unlock();
154160
}
155161
/* Capture those samples. */
156162
Braccio.positions(idx);
@@ -164,12 +170,16 @@ void loop() {
164170
if (idx >= final_idx) {
165171
Serial.println("REPLAY done");
166172
state = ZERO_POSITION;
173+
Braccio.lvgl_lock();
167174
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
168175
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
176+
Braccio.lvgl_unlock();
169177
}
170178
}
171179
delay(100);
172180
if (state != ZERO_POSITION) {
181+
Braccio.lvgl_lock();
173182
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
183+
Braccio.lvgl_unlock();
174184
}
175185
}

0 commit comments

Comments
 (0)