If timers are updated the elapsed time between their due time and the time they were actually updated is not considered.
More...
|
#define | TIMER_ID_INVALID UINT32_MAX |
| Timer id, that is always considered invalid by the timer functions.
|
|
|
typedef int | timer_cb (uint64_t elapsed, void *data) |
| Format of a timer callback. More...
|
|
|
static bool | timer_isvalid (struct timer_base *base, uint32_t id) |
|
int | timer_base_init (struct timer_base *base, uint32_t num_timers) |
| Initialize a timer base and allocate resources. More...
|
|
void | timer_base_clear (struct timer_base *base) |
| Clear a timer base and free all its associated resources. More...
|
|
int | timer_add (struct timer_base *base, uint32_t *id, uint32_t ms, timer_cb cb, void *data, uint8_t mode) |
| Initialize and activate an emtpy timer element in the base. More...
|
|
int | timer_remove (struct timer_base *base, uint32_t id, void **data) |
| Deactivate and remove a timer entry. More...
|
|
int | timer_activate (struct timer_base *base, uint32_t id) |
| Activate the timer entry with id . More...
|
|
int | timer_deactivate (struct timer_base *base, uint32_t id) |
| Deactivate the timer entry with id . More...
|
|
uint32_t | timer_update (struct timer_base *base) |
| Find and shoot all due timers in the base. More...
|
|
uint64_t | timer_tickcount (void) |
| Returns a monotonically increasing tickcount in milisecond resolution. More...
|
|
uint64_t | timer_tickdiff (uint64_t current, uint64_t last) |
| Calculate the difference between two tickcounts. More...
|
|
If timers are updated the elapsed time between their due time and the time they were actually updated is not considered.
So timers won't shoot at a constant frequency if update isn't called exactly at their due times, but they will always run for at least their configured interval.
A timer can be removed from inside the timer callback using one of the following options:
- Create the timer with the singleshot option, it will be removed after the first shot automatically.
- Return -1 in the timer callback.
- Call the timer_remove() function.
Note: Each of these is a valid option, but do not combine them!
typedef int timer_cb(uint64_t elapsed, void *data) |
Format of a timer callback.
- Parameters
-
elapsed | Actually elapesd time since the last callback in miliseconds. |
data | The data that was added to the timer with timer_add. |
- Returns
- Zero by default, -1 will remove the timer after the callback.
Mode for newly added timers.
- See also
- timer_add
Enumerator |
---|
TIMER_DEFAULT |
Default: periodic timer.
|
TIMER_SINGLESHOT |
Singleshot timer.
|
int timer_activate |
( |
struct timer_base * |
base, |
|
|
uint32_t |
id |
|
) |
| |
Activate the timer entry with id
.
The timer will resume shooting with the properties it was added. The timer will not shoot before ms time has past.
- Parameters
-
base | The timer base holding the timer to activate. |
id | The id of the timer to be activated. |
- Returns
- Zero on success or errocode on failure.
int timer_add |
( |
struct timer_base * |
base, |
|
|
uint32_t * |
id, |
|
|
uint32_t |
ms, |
|
|
timer_cb |
cb, |
|
|
void * |
data, |
|
|
uint8_t |
mode |
|
) |
| |
Initialize and activate an emtpy timer element in the base.
- Parameters
-
base | The timer base to add a new timer to. |
id | Output parameter that contains the id of the new timer (may be NULL). |
ms | The interval of the timer to shoot at in milliseconds. Zero is not allowed. |
cb | The callback function to be called when the timer shoots. |
data | The data passed to the callback function (may be NULL). |
mode | Timer mode like TIMER_DEFAULT, see timer_mode. |
- Returns
- Zero on success or errocode on failure.
Clear a timer base and free all its associated resources.
- Parameters
-
base | The base to be cleared. |
int timer_base_init |
( |
struct timer_base * |
base, |
|
|
uint32_t |
num_timers |
|
) |
| |
Initialize a timer base and allocate resources.
- Parameters
-
base | The base to be initialized. |
num_timers | The maximum number of timers the base can hold. |
- Returns
- Zero on success or errocode on failure.
int timer_deactivate |
( |
struct timer_base * |
base, |
|
|
uint32_t |
id |
|
) |
| |
Deactivate the timer entry with id
.
The timer will stop shooting, but its resources will still be allocated. Note: Calling deactivate and then activate is much faster than calling remove and add for a timer.
- Parameters
-
base | The timer base holding the timer to deactivate. |
id | The id of the timer to be deactivated. |
- Returns
- Zero on success or errocode on failure.
int timer_remove |
( |
struct timer_base * |
base, |
|
|
uint32_t |
id, |
|
|
void ** |
data |
|
) |
| |
Deactivate and remove a timer entry.
- Parameters
-
base | The timer base holding the timer to remove. |
id | The id of the timer to be removed. |
data | Output parameter that returns the data added with timer_add (may be NULL). |
- Returns
- Zero on success or errocode on failure.
uint64_t timer_tickcount |
( |
void |
| ) |
|
Returns a monotonically increasing tickcount in milisecond resolution.
Depending on the platform this value may or may not overflow.
uint64_t timer_tickdiff |
( |
uint64_t |
current, |
|
|
uint64_t |
last |
|
) |
| |
Calculate the difference between two tickcounts.
This function handles a possible overflow between the tickcounts, but doesn't work correctly if more than one epoch elapsed between the tickcounts.
- Parameters
-
current | The newer tickcount. |
last | The older tickcount. |
- Returns
- Difference between
current
and last
in miliseconds.
Find and shoot all due timers in the base.
Looks at every timer and calls the callback if its due time has passed.
- Parameters
-
base | Timer base whose timers need to be updated |
- Returns
- ms till the next timer is due, or UINT32_MAX if there is none.