1 /**
2  * @file lv_task.c
3  * An 'lv_task'  is a void (*fp) (void* param) type function which will be called periodically.
4  * A priority (5 levels + disable) can be assigned to lv_tasks.
5  */
6 
7 #ifndef LV_TASK_H
8 #define LV_TASK_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /*********************
15  *      INCLUDES
16  *********************/
17 #ifdef LV_CONF_INCLUDE_SIMPLE
18 #include "lv_conf.h"
19 #else
20 #include "../../lv_conf.h"
21 #endif
22 
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include "lv_mem.h"
26 #include "lv_ll.h"
27 
28 /*********************
29  *      DEFINES
30  *********************/
31 #ifndef LV_ATTRIBUTE_TASK_HANDLER
32 #define LV_ATTRIBUTE_TASK_HANDLER
33 #endif
34 /**********************
35  *      TYPEDEFS
36  **********************/
37 
38 struct _lv_task_t;
39 
40 /**
41  * Tasks execute this type type of functions.
42  */
43 typedef void (*lv_task_cb_t)(struct _lv_task_t *);
44 
45 /**
46  * Possible priorities for lv_tasks
47  */
48 enum {
49     LV_TASK_PRIO_OFF = 0,
50     LV_TASK_PRIO_LOWEST,
51     LV_TASK_PRIO_LOW,
52     LV_TASK_PRIO_MID,
53     LV_TASK_PRIO_HIGH,
54     LV_TASK_PRIO_HIGHEST,
55     _LV_TASK_PRIO_NUM,
56 };
57 typedef uint8_t lv_task_prio_t;
58 
59 /**
60  * Descriptor of a lv_task
61  */
62 typedef struct _lv_task_t
63 {
64     uint32_t period; /**< How often the task should run */
65     uint32_t last_run; /**< Last time the task ran */
66     lv_task_cb_t task_cb; /**< Task function */
67 
68     void * user_data; /**< Custom user data */
69 
70     uint8_t prio : 3; /**< Task priority */
71     uint8_t once : 1; /**< 1: one shot task */
72 } lv_task_t;
73 
74 /**********************
75  * GLOBAL PROTOTYPES
76  **********************/
77 
78 /**
79  * Init the lv_task module
80  */
81 void lv_task_core_init(void);
82 
83 //! @cond Doxygen_Suppress
84 
85 /**
86  * Call it  periodically to handle lv_tasks.
87  */
88 LV_ATTRIBUTE_TASK_HANDLER void lv_task_handler(void);
89 
90 //! @endcond
91 
92 /**
93  * Create an "empty" task. It needs to initialzed with at least
94  * `lv_task_set_cb` and `lv_task_set_period`
95  * @return pointer to the craeted task
96  */
97 lv_task_t * lv_task_create_basic(void);
98 
99 /**
100  * Create a new lv_task
101  * @param task_xcb a callback which is the task itself. It will be called periodically.
102  *                 (the 'x' in the argument name indicates that its not a fully generic function because it not follows
103  *                  the `func_name(object, callback, ...)` convention)
104  * @param period call period in ms unit
105  * @param prio priority of the task (LV_TASK_PRIO_OFF means the task is stopped)
106  * @param user_data custom parameter
107  * @return pointer to the new task
108  */
109 lv_task_t * lv_task_create(lv_task_cb_t task_xcb, uint32_t period, lv_task_prio_t prio, void * user_data);
110 
111 /**
112  * Delete a lv_task
113  * @param task pointer to task_cb created by task
114  */
115 void lv_task_del(lv_task_t * task);
116 
117 /**
118  * Set the callback the task (the function to call periodically)
119  * @param task pointer to a task
120  * @param task_cb the function to call periodically
121  */
122 void lv_task_set_cb(lv_task_t * task, lv_task_cb_t task_cb);
123 
124 /**
125  * Set new priority for a lv_task
126  * @param task pointer to a lv_task
127  * @param prio the new priority
128  */
129 void lv_task_set_prio(lv_task_t * task, lv_task_prio_t prio);
130 
131 /**
132  * Set new period for a lv_task
133  * @param task pointer to a lv_task
134  * @param period the new period
135  */
136 void lv_task_set_period(lv_task_t * task, uint32_t period);
137 
138 /**
139  * Make a lv_task ready. It will not wait its period.
140  * @param task pointer to a lv_task.
141  */
142 void lv_task_ready(lv_task_t * task);
143 
144 /**
145  * Delete the lv_task after one call
146  * @param task pointer to a lv_task.
147  */
148 void lv_task_once(lv_task_t * task);
149 
150 /**
151  * Reset a lv_task.
152  * It will be called the previously set period milliseconds later.
153  * @param task pointer to a lv_task.
154  */
155 void lv_task_reset(lv_task_t * task);
156 
157 /**
158  * Enable or disable the whole  lv_task handling
159  * @param en: true: lv_task handling is running, false: lv_task handling is suspended
160  */
161 void lv_task_enable(bool en);
162 
163 /**
164  * Get idle percentage
165  * @return the lv_task idle in percentage
166  */
167 uint8_t lv_task_get_idle(void);
168 
169 /**********************
170  *      MACROS
171  **********************/
172 
173 #ifdef __cplusplus
174 } /* extern "C" */
175 #endif
176 
177 #endif
178