[English]

Operating system and interrupts(操作系统和中断)

显示原文

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 Handler.


默认情况下,LVGL 非线程安全

但是,在以下情况下调用 LVGL 相关函数是有效的:

Tasks and threads(任务和线程)

显示原文

If you need to use real tasks or threads, you need a mutex which should be invoked before the call of lv_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:


如果你需要使用真正的任务或线程,你需要一个互斥锁,它应该在调用 lv_timer_handler() 之前被调用并在它之后释放。 此外,您必须在每个 LVGL (lv_...) 相关函数调用和代码周围的其他任务和线程中使用相同的互斥锁。 这样你就可以在真正的多任务环境中使用 LVGL。只需使用互斥锁来避免并发调用 LVGL 函数。

下面是一些伪代码来说明这个概念:

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);
    }
}

Interrupts(中断)

显示原文

Try to avoid calling LVGL functions from interrupt handlers (except lv_tick_inc() and lv_display_flush_ready()). But if you need to do this you have to disable the interrupt which uses LVGL functions while lv_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 lv_timer_handler()).


尽量避免从中断处理程序调用 LVGL 函数(除了 lv_tick_inc()lv_display_flush_ready())。但是如果你需要这样做,你必须在 lv_timer_handler() 运行时禁用使用 LVGL 函数的中断。

这是一种更好的方法,只需在中断,并在LVGL计时器中定期检查它(由 lv_timer_handler() 运行)。