lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_timer

Overview

Platform-neutral system timers.

#include "lwpa_timer.h"

Data Structures

struct  LwpaTimer
 A millisecond-resolution timer. More...
 

Macros

#define lwpa_timer_start(lwpatimerptr, intval)
 Start a timer. More...
 
#define lwpa_timer_reset(lwpatimerptr)   (((lwpatimerptr)->reset_time) = lwpa_getms())
 Reset a timer while keeping the same interval. More...
 
#define lwpa_timer_elapsed(lwpatimerptr)   (lwpa_getms() - (lwpatimerptr)->reset_time)
 Get the time since a timer was reset. More...
 
#define lwpa_timer_isexpired(lwpatimerptr)    (((lwpatimerptr)->interval == 0) || (lwpa_timer_elapsed(lwpatimerptr) > (lwpatimerptr)->interval))
 Check to see if a timer is expired. More...
 

Typedefs

typedef struct LwpaTimer LwpaTimer
 A millisecond-resolution timer. More...
 

Functions

uint32_t lwpa_getms ()
 Get a monotonically-increasing millisecond value. More...
 
uint32_t lwpa_timer_remaining (const LwpaTimer *timer)
 Get the amount of time remaining in a timer. More...
 

Macro Definition Documentation

◆ lwpa_timer_elapsed

#define lwpa_timer_elapsed (   lwpatimerptr)    (lwpa_getms() - (lwpatimerptr)->reset_time)

Get the time since a timer was reset.

Parameters
lwpatimerptrPointer to the LwpaTimer of which to get the elapsed time.
Returns
Number of milliseconds since the timer was reset.

◆ lwpa_timer_isexpired

#define lwpa_timer_isexpired (   lwpatimerptr)     (((lwpatimerptr)->interval == 0) || (lwpa_timer_elapsed(lwpatimerptr) > (lwpatimerptr)->interval))

Check to see if a timer is expired.

Parameters
lwpatimerptrPointer to the LwpaTimer of which to check the expiration.
Returns
true (more than interval milliseconds have passed since the timer was started/reset) or false (less than or equal to interval milliseconds have passed since the timer was started/reset)

◆ lwpa_timer_reset

#define lwpa_timer_reset (   lwpatimerptr)    (((lwpatimerptr)->reset_time) = lwpa_getms())

Reset a timer while keeping the same interval.

Parameters
lwpatimerptrPointer to the LwpaTimer to reset.

◆ lwpa_timer_start

#define lwpa_timer_start (   lwpatimerptr,
  intval 
)
Value:
do \
{ \
(lwpatimerptr)->reset_time = lwpa_getms(); \
(lwpatimerptr)->interval = intval; \
} while (0)
uint32_t lwpa_getms()
Get a monotonically-increasing millisecond value.
Definition: lwpa_timer.c:24

Start a timer.

Parameters
lwpatimerptrPointer to the LwpaTimer to start.
intvalTimer interval in milliseconds. An interval of 0 will result in a timer that is always expired.

Typedef Documentation

◆ LwpaTimer

typedef struct LwpaTimer LwpaTimer

A millisecond-resolution timer.

The times are represented in milliseconds by a 32-bit unsigned integer. Since the timer is monotonically-increasing, wraparound is handled by doing comparisons of the form

timeA - timeB > 0

rather than

timeA > timeB

.

Function Documentation

◆ lwpa_getms()

uint32_t lwpa_getms ( )

Get a monotonically-increasing millisecond value.

Returns
The current timestamp in milliseconds.

◆ lwpa_timer_remaining()

uint32_t lwpa_timer_remaining ( const LwpaTimer timer)

Get the amount of time remaining in a timer.

Parameters
timerPointer to the LwpaTimer of which to get the remaining time.
Returns
Remaining time in milliseconds or 0 (timer is expired).