-
Notifications
You must be signed in to change notification settings - Fork 0
Timers
Loviisa provides simple and quite common set of timer functionality. The easiest way is to use timers that create events.
LVS_DEFINE_TIMER(timer) defines new timer. Shall be used outside of functions in a global C context.
LVS_USE_TIMER(timer) lets you use the defined timer wherever you need it.
LVS_INIT_TIMER(timer, event, period, periodic) is to be executed in any initialization function or task before timer is used. This primitive sets timer's parameters, but doesn't start timer.
- timer - name of previously defined timer
- event - name of event that is generated when timer triggers
- period - time in milliseconds when timer is to be triggered
- periodic - 0 if timer shall be triggered once or 1 if it triggers periodically each milliseconds
LVS_START_TIMER(timer) launches the given timer
LVS_STOP_TIMER(timer) stops the timer
The following macros are used to obtain timer properties:
LVS_GET_TIMER_PERIOD_MS(timer) - retrieves timer's period in milliseconds
LVS_GET_TIMER_EPOCH(timer) - how many times the timer was triggered
LVS_IS_TIMER_ENABLED(timer) - is timer running right now
LVS_IS_TIMER_PERIODIC(timer) - checks if timer is periodic
In addition to timers that provide events, tasks may use timeout functionality.
LVS_START_TIMEOUT(timeout_name) - starts counting time for the timeout
LVS_RESET_TIMEOUT(timeout_name) - stops counting time for the timeout
LVS_IF_TIMEOUT(timeout_name, ms) - the following block is executed if more than ms milliseconds passed since LVS_START_TIMEOUT. Timeout resets automatically. See conditional execution for more information about LVS_IF_... blocks, including LVS_IF_PASSED primitive that is very valuable for time-sensitive execution.
Important!
To make timers functionality working, user shall call the following function periodically using hardware/SDK timer or real time clock functionality, usually interrupt handlers:
void lvs_TimerTick(void);
The number of physical ticks per second (frequency of the system clock) is to be set in lvs_config.h as LVS_TICKS_PER_SECOND macro.
unsigned long lvs_GetTickCount(void); allows to obtain the current number of system ticks since the system start, but it is not recommended for a direct use.