acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

os.md (1878B)

      1 ```eval_rst
      2 .. include:: /header.rst
      3 :github_url: |github_link_base|/porting/os.md
      4 ```
      5 # Operating system and interrupts
      6 
      7 LVGL is **not thread-safe** by default.
      8 
      9 However, in the following conditions it's valid to call LVGL related functions:
     10 - In *events*. Learn more in [Events](/overview/event).
     11 - In *lv_timer*. Learn more in [Timers](/overview/timer).
     12 
     13 
     14 ## Tasks and threads
     15 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.
     16 Also, you have to use the same mutex in other tasks and threads around every LVGL (`lv_...`) related function call and code.
     17 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.
     18 
     19 Here is some pseudocode to illustrate the concept:
     20 
     21 ```c
     22 static mutex_t lvgl_mutex;
     23 
     24 void lvgl_thread(void)
     25 {
     26     while(1) {
     27         mutex_lock(&lvgl_mutex);
     28         lv_task_handler();
     29         mutex_unlock(&lvgl_mutex);
     30         thread_sleep(10); /* sleep for 10 ms */
     31     }
     32 }
     33 
     34 void other_thread(void)
     35 {
     36     /* You must always hold the mutex while using LVGL APIs */
     37     mutex_lock(&lvgl_mutex);
     38     lv_obj_t *img = lv_img_create(lv_scr_act());
     39     mutex_unlock(&lvgl_mutex);
     40 
     41     while(1) {
     42         mutex_lock(&lvgl_mutex);
     43         /* change to the next image */
     44         lv_img_set_src(img, next_image);
     45         mutex_unlock(&lvgl_mutex);
     46         thread_sleep(2000);
     47     }
     48 }
     49 ```
     50 
     51 ## Interrupts
     52 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.
     53 
     54 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`).