1 /**
2  * @file lv_timer.h
3  */
4 
5 #ifndef LV_TIMER_H
6 #define LV_TIMER_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /*********************
13  *      INCLUDES
14  *********************/
15 #include "../lv_conf_internal.h"
16 #include "../tick/lv_tick.h"
17 #include "lv_types.h"
18 #include "lv_ll.h"
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 #ifndef LV_ATTRIBUTE_TIMER_HANDLER
24 #define LV_ATTRIBUTE_TIMER_HANDLER
25 #endif
26 
27 #define LV_NO_TIMER_READY 0xFFFFFFFF
28 
29 /**********************
30  *      TYPEDEFS
31  **********************/
32 
33 /**
34  * Timers execute this type of functions.
35  */
36 typedef void (*lv_timer_cb_t)(lv_timer_t *);
37 
38 /**
39  * Timer handler resume this type of function.
40  */
41 typedef void (*lv_timer_handler_resume_cb_t)(void * data);
42 
43 /**********************
44  * GLOBAL PROTOTYPES
45  **********************/
46 
47 //! @cond Doxygen_Suppress
48 
49 /**
50  * Call it periodically to handle lv_timers.
51  * @return time till it needs to be run next (in ms)
52  */
53 LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler(void);
54 
55 //! @endcond
56 
57 /**
58  * Call it in the super-loop of main() or threads. It will run lv_timer_handler()
59  * with a given period in ms. You can use it with sleep or delay in OS environment.
60  * This function is used to simplify the porting.
61  * @param period the period for running lv_timer_handler()
62  * @return the time after which it must be called again
63  */
64 LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler_run_in_period(uint32_t period);
65 
66 /**
67  * Call it in the super-loop of main() or threads. It will automatically call lv_timer_handler() at the right time.
68  * This function is used to simplify the porting.
69  */
70 LV_ATTRIBUTE_TIMER_HANDLER void lv_timer_periodic_handler(void);
71 
72 /**
73  * Set the resume callback to the timer handler
74  * @param cb the function to call when timer handler is resumed
75  * @param data pointer to a resume data
76  */
77 void lv_timer_handler_set_resume_cb(lv_timer_handler_resume_cb_t cb, void * data);
78 
79 /**
80  * Create an "empty" timer. It needs to be initialized with at least
81  * `lv_timer_set_cb` and `lv_timer_set_period`
82  * @return pointer to the created timer
83  */
84 lv_timer_t * lv_timer_create_basic(void);
85 
86 /**
87  * Create a new lv_timer
88  * @param timer_xcb a callback to call periodically.
89  *                 (the 'x' in the argument name indicates that it's not a fully generic function because it not follows
90  *                  the `func_name(object, callback, ...)` convention)
91  * @param period call period in ms unit
92  * @param user_data custom parameter
93  * @return pointer to the new timer
94  */
95 lv_timer_t * lv_timer_create(lv_timer_cb_t timer_xcb, uint32_t period, void * user_data);
96 
97 /**
98  * Delete a lv_timer
99  * @param timer pointer to an lv_timer
100  */
101 void lv_timer_delete(lv_timer_t * timer);
102 
103 /**
104  * Pause a timer.
105  * @param timer pointer to an lv_timer
106  */
107 void lv_timer_pause(lv_timer_t * timer);
108 
109 /**
110  * Resume a timer.
111  * @param timer pointer to an lv_timer
112  */
113 void lv_timer_resume(lv_timer_t * timer);
114 
115 /**
116  * Set the callback to the timer (the function to call periodically)
117  * @param timer pointer to a timer
118  * @param timer_cb the function to call periodically
119  */
120 void lv_timer_set_cb(lv_timer_t * timer, lv_timer_cb_t timer_cb);
121 
122 /**
123  * Set new period for a lv_timer
124  * @param timer pointer to a lv_timer
125  * @param period the new period
126  */
127 void lv_timer_set_period(lv_timer_t * timer, uint32_t period);
128 
129 /**
130  * Make a lv_timer ready. It will not wait its period.
131  * @param timer pointer to a lv_timer.
132  */
133 void lv_timer_ready(lv_timer_t * timer);
134 
135 /**
136  * Set the number of times a timer will repeat.
137  * @param timer pointer to a lv_timer.
138  * @param repeat_count -1 : infinity;  0 : stop ;  n>0: residual times
139  */
140 void lv_timer_set_repeat_count(lv_timer_t * timer, int32_t repeat_count);
141 
142 /**
143  * Set whether a lv_timer will be deleted automatically when it is called `repeat_count` times.
144  * @param timer pointer to a lv_timer.
145  * @param auto_delete true: auto delete; false: timer will be paused when it is called `repeat_count` times.
146  */
147 void lv_timer_set_auto_delete(lv_timer_t * timer, bool auto_delete);
148 
149 /**
150  * Set custom parameter to the lv_timer.
151  * @param timer pointer to a lv_timer.
152  * @param user_data custom parameter
153  */
154 void lv_timer_set_user_data(lv_timer_t * timer, void * user_data);
155 
156 /**
157  * Reset a lv_timer.
158  * It will be called the previously set period milliseconds later.
159  * @param timer pointer to a lv_timer.
160  */
161 void lv_timer_reset(lv_timer_t * timer);
162 
163 /**
164  * Enable or disable the whole lv_timer handling
165  * @param en true: lv_timer handling is running, false: lv_timer handling is suspended
166  */
167 void lv_timer_enable(bool en);
168 
169 /**
170  * Get idle percentage
171  * @return the lv_timer idle in percentage
172  */
173 uint32_t lv_timer_get_idle(void);
174 
175 /**
176  * Get the time remaining until the next timer will run
177  * @return the time remaining in ms
178  */
179 uint32_t lv_timer_get_time_until_next(void);
180 
181 /**
182  * Iterate through the timers
183  * @param timer NULL to start iteration or the previous return value to get the next timer
184  * @return the next timer or NULL if there is no more timer
185  */
186 lv_timer_t * lv_timer_get_next(lv_timer_t * timer);
187 
188 /**
189  * Get the user_data passed when the timer was created
190  * @param timer pointer to the lv_timer
191  * @return pointer to the user_data
192  */
193 void * lv_timer_get_user_data(lv_timer_t * timer);
194 
195 /**
196  * Get the pause state of a timer
197  * @param timer pointer to a lv_timer
198  * @return true: timer is paused; false: timer is running
199  */
200 bool lv_timer_get_paused(lv_timer_t * timer);
201 
202 /**********************
203  *      MACROS
204  **********************/
205 
206 #ifdef __cplusplus
207 } /*extern "C"*/
208 #endif
209 
210 #endif
211