inc/ecu/timer.h Source File

ECU: inc/ecu/timer.h Source File
ECU
timer.h
Go to the documentation of this file.
1 
18 #ifndef ECU_TIMER_H_
19 #define ECU_TIMER_H_
20 
21 /*------------------------------------------------------------*/
22 /*------------------------- INCLUDES -------------------------*/
23 /*------------------------------------------------------------*/
24 
25 /* STDLib. */
26 #include <limits.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 
30 /* Linked list of timers. */
31 #include "ecu/dlist.h"
32 
33 /*------------------------------------------------------------*/
34 /*---------------------- DEFINES AND MACROS ------------------*/
35 /*------------------------------------------------------------*/
36 
41 #define ECU_TIMER_OBJ_UNUSED \
42  ((void *)0)
43 
47 #define ECU_TICK_MAX \
48  (UINT_MAX)
49 
50 /*------------------------------------------------------------*/
51 /*------------------------- TIMER TYPES ----------------------*/
52 /*------------------------------------------------------------*/
53 
62 typedef unsigned int ecu_tick_t;
63 
68 {
71  /********************/
73 };
74 
75 /*------------------------------------------------------------*/
76 /*--------------------------- TIMER --------------------------*/
77 /*------------------------------------------------------------*/
78 
85 struct ecu_timer
86 {
88  struct ecu_dnode dnode;
89 
92 
95 
98 
105  bool (*callback)(struct ecu_timer *me, void *obj);
106 
108  void *obj;
109 };
110 
119 struct ecu_tlist
120 {
124 
128 
131  struct ecu_dlist timers;
132 
137  struct ecu_dlist wraparounds;
138 };
139 
140 /*------------------------------------------------------------*/
141 /*------------------- TIMER MEMBER FUNCTIONS -----------------*/
142 /*------------------------------------------------------------*/
143 
144 #ifdef __cplusplus
145 extern "C" {
146 #endif
147 
171 extern void ecu_timer_ctor(struct ecu_timer *me,
172  bool (*callback)(struct ecu_timer *me, void *obj),
173  void *obj);
186 extern bool ecu_timer_active(const struct ecu_timer *me);
187 
195 extern void ecu_timer_disarm(struct ecu_timer *me);
196 
203 extern ecu_tick_t ecu_timer_period(const struct ecu_timer *me);
204 
218 extern void ecu_timer_set(struct ecu_timer *me,
219  ecu_tick_t period,
220  enum ecu_timer_type_e type);
221 
228 extern enum ecu_timer_type_e ecu_timer_type(const struct ecu_timer *me);
231 /*------------------------------------------------------------*/
232 /*-------------------- TLIST MEMBER FUNCTIONS ----------------*/
233 /*------------------------------------------------------------*/
234 
248 extern void ecu_tlist_ctor(struct ecu_tlist *me);
268 extern void ecu_tlist_service(struct ecu_tlist *me, ecu_tick_t elapsed);
269 
285 extern void ecu_tlist_timer_arm(struct ecu_tlist *me,
286  struct ecu_timer *timer,
287  ecu_tick_t period,
288  enum ecu_timer_type_e type);
289 
299 extern void ecu_tlist_timer_rearm(struct ecu_tlist *me, struct ecu_timer *timer);
302 #ifdef __cplusplus
303 }
304 #endif
305 
306 #endif /* ECU_TIMER_H_ */
Intrusive, doubly-linked list.
Definition: dlist.h:190
Single node within list. Intrusive, so user-defined types contain this node as a member.
Definition: dlist.h:166
Timer object represented as node in linked list.
Definition: timer.h:86
ecu_tick_t period
Timer expires once elapsed ticks exceeds this value.
Definition: timer.h:94
bool(* callback)(struct ecu_timer *me, void *obj)
Mandatory callback that executes when timer expires. First parameter passed is this timer object....
Definition: timer.h:105
void * obj
Optional object to pass to ecu_timer::callback.
Definition: timer.h:108
ecu_tick_t expiration
Absolute time, in ticks, when this timer expires.
Definition: timer.h:91
struct ecu_dnode dnode
Linked list node in ecu_tlist.
Definition: timer.h:88
enum ecu_timer_type_e type
Single shot, periodic, etc.
Definition: timer.h:97
Timer linked list that runs all software timers (ecu_timer) added to it. Each list usually holds mult...
Definition: timer.h:120
bool overflowed
True if ecu_tlist::current overflowed. Flag set back to false once overflow handled....
Definition: timer.h:127
struct ecu_dlist timers
Ordered linked list of timers that are running. Ordered by ecu_timer::expiration.
Definition: timer.h:131
ecu_tick_t current
Absolute timestamp, in ticks. Incremented with each call to ecu_tlist_service(). Overflow is automati...
Definition: timer.h:123
struct ecu_dlist wraparounds
Ordered linked list of timers that are running, but expire after an ecu_tlist::current overflow....
Definition: timer.h:137
void ecu_timer_set(struct ecu_timer *me, ecu_tick_t period, enum ecu_timer_type_e type)
Stops the timer if it was running and reconfigures it with the newly supplied settings....
unsigned int ecu_tick_t
To remain portable, units of time are measured arbitrarily in ticks. It is the application's responsi...
Definition: timer.h:62
void ecu_timer_ctor(struct ecu_timer *me, bool(*callback)(struct ecu_timer *me, void *obj), void *obj)
Timer constructor.
bool ecu_timer_active(const struct ecu_timer *me)
Returns true if the timer is currently running. False otherwise.
void ecu_tlist_ctor(struct ecu_tlist *me)
Timer list constructor.
ecu_tick_t ecu_timer_period(const struct ecu_timer *me)
Returns the timer's period that was set when it was started.
void ecu_tlist_service(struct ecu_tlist *me, ecu_tick_t elapsed)
Services all software timers (ecu_timer) currently in the list. Servicing involves expiring appropria...
ecu_timer_type_e
Type of ecu_timer used.
Definition: timer.h:68
@ ECU_TIMER_TYPE_ONE_SHOT
Definition: timer.h:69
@ ECU_TIMER_TYPE_PERIODIC
Definition: timer.h:70
@ ECU_TIMER_TYPES_COUNT
Definition: timer.h:72
void ecu_tlist_timer_arm(struct ecu_tlist *me, struct ecu_timer *timer, ecu_tick_t period, enum ecu_timer_type_e type)
Starts a timer with the specified settings. If the timer is already running it is restarted and recon...
enum ecu_timer_type_e ecu_timer_type(const struct ecu_timer *me)
Returns the timer's type that was specified when it was started.
void ecu_tlist_timer_rearm(struct ecu_tlist *me, struct ecu_timer *timer)
Restarts the timer with its same settings.
void ecu_timer_disarm(struct ecu_timer *me)
Stops the timer. Timer can be rearmed without reconstruction. This function can be used on a timer th...