1 /*
2  * Copyright (c) 2024 Fabian Blatz <fabianblatz@gmail.com>
3  * Copyright (c) 2025 Abderrahmane JARMOUNI
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_MODULES_LVGL_ZEPHYR_H_
9 #define ZEPHYR_MODULES_LVGL_ZEPHYR_H_
10 
11 #include <zephyr/kernel.h>
12 
13 #if DT_ZEPHYR_DISPLAYS_COUNT == 0
14 #error Could not find "zephyr,display" chosen property, or "zephyr,displays" compatible node in DT
15 #endif /* DT_ZEPHYR_DISPLAYS_COUNT == 0 */
16 
17 #define LV_DISPLAY_IDX_MACRO(i, _) _##i
18 
19 #define LV_DISPLAYS_IDX_LIST LISTIFY(DT_ZEPHYR_DISPLAYS_COUNT, LV_DISPLAY_IDX_MACRO, (,))
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /**
26  * @brief Initialize the LittlevGL library
27  *
28  * This function initializes the LittlevGL library and setups the display and input devices.
29  * If `LV_Z_AUTO_INIT` is disabled it must be called before any other LittlevGL function.
30  *
31  * @return 0 on success, negative errno code on failure
32  */
33 int lvgl_init(void);
34 
35 #if defined(CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE) || defined(__DOXYGEN__)
36 /**
37  * @brief Gets the instance of LVGL's workqueue
38  *
39  * This function returns the workqueue instance used
40  * by LVGL core, allowing user to submit their own
41  * work items to it.
42  *
43  * @return pointer to LVGL's workqueue instance
44  */
45 struct k_work_q *lvgl_get_workqueue(void);
46 
47 #endif /* CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE */
48 
49 
50 #if (defined(CONFIG_LV_Z_LVGL_MUTEX) && !defined(CONFIG_LV_Z_USE_OSAL)) || defined(__DOXYGEN__)
51 /**
52  * @brief Lock internal mutex before accessing LVGL API.
53  *
54  * @details LVGL API is not thread-safe. Therefore, before accessing any
55  * API function, you need to lock the internal mutex, to prevent the
56  * LVGL thread from pre-empting the API call.
57  */
58 void lvgl_lock(void);
59 
60 /**
61  * @brief Try to lock internal mutex before accessing LVGL API.
62  *
63  * @details This function behaves like lvgl_lock(), provided that
64  * the internal mutex is unlocked. Otherwise, it returns false without
65  * waiting.
66  */
67 bool lvgl_trylock(void);
68 
69 /**
70  * @brief Unlock internal mutex after accessing LVGL API.
71  */
72 void lvgl_unlock(void);
73 
74 #elif defined(CONFIG_LV_Z_LVGL_MUTEX) && defined(CONFIG_LV_Z_USE_OSAL)
75 
76 #include <osal/lv_os.h>
77 
lvgl_lock(void)78 static inline void lvgl_lock(void)
79 {
80 	lv_lock();
81 }
82 
lvgl_trylock(void)83 static inline bool lvgl_trylock(void)
84 {
85 	return lv_lock_isr() == LV_RESULT_OK;
86 }
87 
lvgl_unlock(void)88 static inline void lvgl_unlock(void)
89 {
90 	lv_unlock();
91 }
92 
93 #else /* CONFIG_LV_Z_LVGL_MUTEX */
94 
95 #define lvgl_lock(...) (void)0
96 #define lvgl_trylock(...) true
97 #define lvgl_unlock(...) (void)0
98 
99 #endif /* CONFIG_LV_Z_LVGL_MUTEX */
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif /* ZEPHYR_MODULES_LVGL_ZEPHYR_H_ */
106