1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-10-18     Meco Man     the first version
9  * 2022-05-10     Meco Man     improve rt-thread initialization process
10  */
11 
12 #ifdef __RTTHREAD__
13 
14 #include <lvgl.h>
15 #include <rtthread.h>
16 
17 #define DBG_TAG    "LVGL"
18 #define DBG_LVL    DBG_INFO
19 #include <rtdbg.h>
20 
21 #ifndef PKG_LVGL_THREAD_STACK_SIZE
22 #define PKG_LVGL_THREAD_STACK_SIZE 4096
23 #endif /* PKG_LVGL_THREAD_STACK_SIZE */
24 
25 #ifndef PKG_LVGL_THREAD_PRIO
26 #define PKG_LVGL_THREAD_PRIO (RT_THREAD_PRIORITY_MAX*2/3)
27 #endif /* PKG_LVGL_THREAD_PRIO */
28 
29 extern void lv_port_disp_init(void);
30 extern void lv_port_indev_init(void);
31 extern void lv_user_gui_init(void);
32 
33 static struct rt_thread lvgl_thread;
34 
35 #ifdef rt_align
rt_align(RT_ALIGN_SIZE)36 rt_align(RT_ALIGN_SIZE)
37 #else
38 ALIGN(RT_ALIGN_SIZE)
39 #endif
40 static rt_uint8_t lvgl_thread_stack[PKG_LVGL_THREAD_STACK_SIZE];
41 
42 #if LV_USE_LOG
43 static void lv_rt_log(const char *buf)
44 {
45     LOG_I(buf);
46 }
47 #endif /* LV_USE_LOG */
48 
lvgl_thread_entry(void * parameter)49 static void lvgl_thread_entry(void *parameter)
50 {
51 #if LV_USE_LOG
52     lv_log_register_print_cb(lv_rt_log);
53 #endif /* LV_USE_LOG */
54     lv_init();
55     lv_port_disp_init();
56     lv_port_indev_init();
57     lv_user_gui_init();
58 
59     /* handle the tasks of LVGL */
60     while(1)
61     {
62         lv_task_handler();
63         rt_thread_mdelay(LV_DISP_DEF_REFR_PERIOD);
64     }
65 }
66 
lvgl_thread_init(void)67 static int lvgl_thread_init(void)
68 {
69     rt_err_t err;
70 
71     err = rt_thread_init(&lvgl_thread, "LVGL", lvgl_thread_entry, RT_NULL,
72            &lvgl_thread_stack[0], sizeof(lvgl_thread_stack), PKG_LVGL_THREAD_PRIO, 10);
73     if(err != RT_EOK)
74     {
75         LOG_E("Failed to create LVGL thread");
76         return -1;
77     }
78     rt_thread_startup(&lvgl_thread);
79 
80     return 0;
81 }
82 INIT_ENV_EXPORT(lvgl_thread_init);
83 
84 #endif /*__RTTHREAD__*/
85