随笔 - 2649  文章 - 2452  评论 - 0  阅读 - 80424

Operating system and interrupts

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

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:

static mutex_t lvgl_mutex;

void lvgl_thread(void)
{
    while(1) {
        mutex_lock(&lvgl_mutex);
        lv_task_handler();
        mutex_unlock(&lvgl_mutex);
        thread_sleep(10); /* sleep for 10 ms */
    }
}

void other_thread(void)
{
    /* You must always hold the mutex while using LVGL APIs */
    mutex_lock(&lvgl_mutex);
    lv_obj_t *img = lv_img_create(lv_scr_act());
    mutex_unlock(&lvgl_mutex);
    
    while(1) {
        mutex_lock(&lvgl_mutex);
        /* change to the next image */
        lv_img_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_disp_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).

posted on   AtlasLapetos  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示