LVGL is not thread-safe by default.
However, in the following conditions it's valid to call LVGL related functions: - In events. Learn more in events
. -In lv_timer. Learn more in timer
.
If you need to use real tasks or threads, you need a mutex which should be invoked before the call of :cpplv_timer_handler
and released after it. Also, you have to use the same mutex in other tasks and threads around every LVGL (lv_...
) related function call and code. This way you can use LVGL in a real multitasking environment. Just make use of a mutex to avoid the concurrent calling of LVGL functions.
Here is some pseudocode to illustrate the concept:
static mutex_t lvgl_mutex;
void lvgl_thread(void)
{
while(1) {
uint32_t time_till_next;
mutex_lock(&lvgl_mutex);
time_till_next = lv_timer_handler();
mutex_unlock(&lvgl_mutex);
thread_sleep(time_till_next); /* sleep for a while */
}
}
void other_thread(void)
{
/* You must always hold the mutex while using LVGL APIs */
mutex_lock(&lvgl_mutex);
lv_obj_t *img = lv_image_create(lv_screen_active());
mutex_unlock(&lvgl_mutex);
while(1) {
mutex_lock(&lvgl_mutex);
/* change to the next image */
lv_image_set_src(img, next_image);
mutex_unlock(&lvgl_mutex);
thread_sleep(2000);
}
}
Try to avoid calling LVGL functions from interrupt handlers (except :cpplv_tick_inc
and :cpplv_display_flush_ready
). But if you need to do this you have to disable the interrupt which uses LVGL functions while :cpplv_timer_handler
is running.
It's a better approach to simply set a flag or some value in the interrupt, and periodically check it in an LVGL timer (which is run by :cpplv_timer_handler
).