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 "../hal/lv_hal_tick.h"
17 
18 #include <stdint.h>
19 #include <stdbool.h>
20 
21 /*********************
22  *      DEFINES
23  *********************/
24 #ifndef LV_ATTRIBUTE_TIMER_HANDLER
25 #define LV_ATTRIBUTE_TIMER_HANDLER
26 #endif
27 
28 #define LV_NO_TIMER_READY 0xFFFFFFFF
29 
30 /**********************
31  *      TYPEDEFS
32  **********************/
33 
34 struct _lv_timer_t;
35 
36 /**
37  * Timers execute this type of functions.
38  */
39 typedef void (*lv_timer_cb_t)(struct _lv_timer_t *);
40 
41 /**
42  * Descriptor of a lv_timer
43  */
44 typedef struct _lv_timer_t {
45     uint32_t period; /**< How often the timer should run*/
46     uint32_t last_run; /**< Last time the timer ran*/
47     lv_timer_cb_t timer_cb; /**< Timer function*/
48     void * user_data; /**< Custom user data*/
49     int32_t repeat_count; /**< 1: One time;  -1 : infinity;  n>0: residual times*/
50     uint32_t paused : 1;
51 } lv_timer_t;
52 
53 /**********************
54  * GLOBAL PROTOTYPES
55  **********************/
56 
57 /**
58  * Init the lv_timer module
59  */
60 void _lv_timer_core_init(void);
61 
62 //! @cond Doxygen_Suppress
63 
64 /**
65  * Call it periodically to handle lv_timers.
66  * @return time till it needs to be run next (in ms)
67  */
68 LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler(void);
69 
70 //! @endcond
71 
72 /**
73  * Call it in the super-loop of main() or threads. It will run lv_timer_handler()
74  * with a given period in ms. You can use it with sleep or delay in OS environment.
75  * This function is used to simplify the porting.
76  * @param __ms the period for running lv_timer_handler()
77  */
lv_timer_handler_run_in_period(uint32_t ms)78 static inline LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler_run_in_period(uint32_t ms)
79 {
80     static uint32_t last_tick = 0;
81     uint32_t curr_tick = lv_tick_get();
82 
83     if((curr_tick - last_tick) >= (ms)) {
84         last_tick = curr_tick;
85         return lv_timer_handler();
86     }
87     return 1;
88 }
89 
90 /**
91  * Create an "empty" timer. It needs to initialized with at least
92  * `lv_timer_set_cb` and `lv_timer_set_period`
93  * @return pointer to the created timer
94  */
95 lv_timer_t * lv_timer_create_basic(void);
96 
97 /**
98  * Create a new lv_timer
99  * @param timer_xcb a callback to call periodically.
100  *                 (the 'x' in the argument name indicates that it's not a fully generic function because it not follows
101  *                  the `func_name(object, callback, ...)` convention)
102  * @param period call period in ms unit
103  * @param user_data custom parameter
104  * @return pointer to the new timer
105  */
106 lv_timer_t * lv_timer_create(lv_timer_cb_t timer_xcb, uint32_t period, void * user_data);
107 
108 /**
109  * Delete a lv_timer
110  * @param timer pointer to an lv_timer
111  */
112 void lv_timer_del(lv_timer_t * timer);
113 
114 /**
115  * Pause/resume a timer.
116  * @param timer pointer to an lv_timer
117  */
118 void lv_timer_pause(lv_timer_t * timer);
119 
120 void lv_timer_resume(lv_timer_t * timer);
121 
122 /**
123  * Set the callback the timer (the function to call periodically)
124  * @param timer pointer to a timer
125  * @param timer_cb the function to call periodically
126  */
127 void lv_timer_set_cb(lv_timer_t * timer, lv_timer_cb_t timer_cb);
128 
129 /**
130  * Set new period for a lv_timer
131  * @param timer pointer to a lv_timer
132  * @param period the new period
133  */
134 void lv_timer_set_period(lv_timer_t * timer, uint32_t period);
135 
136 /**
137  * Make a lv_timer ready. It will not wait its period.
138  * @param timer pointer to a lv_timer.
139  */
140 void lv_timer_ready(lv_timer_t * timer);
141 
142 /**
143  * Set the number of times a timer will repeat.
144  * @param timer pointer to a lv_timer.
145  * @param repeat_count -1 : infinity;  0 : stop ;  n>0: residual times
146  */
147 void lv_timer_set_repeat_count(lv_timer_t * timer, int32_t repeat_count);
148 
149 /**
150  * Reset a lv_timer.
151  * It will be called the previously set period milliseconds later.
152  * @param timer pointer to a lv_timer.
153  */
154 void lv_timer_reset(lv_timer_t * timer);
155 
156 /**
157  * Enable or disable the whole lv_timer handling
158  * @param en true: lv_timer handling is running, false: lv_timer handling is suspended
159  */
160 void lv_timer_enable(bool en);
161 
162 /**
163  * Get idle percentage
164  * @return the lv_timer idle in percentage
165  */
166 uint8_t lv_timer_get_idle(void);
167 
168 /**
169  * Iterate through the timers
170  * @param timer NULL to start iteration or the previous return value to get the next timer
171  * @return the next timer or NULL if there is no more timer
172  */
173 lv_timer_t * lv_timer_get_next(lv_timer_t * timer);
174 
175 /**********************
176  *      MACROS
177  **********************/
178 
179 #ifdef __cplusplus
180 } /*extern "C"*/
181 #endif
182 
183 #endif
184