Timer

Overview #

The timer module provides the timer mechanism.

Index #

csp_timer_time_t #


csp_timer_time_t is the type of timestamp in nanoseconds.

Example:

csp_timer_time_t now;

csp_timer_now() #


csp_timer_now() returns current timestamp.

Example:

csp_timer_time_t now = csp_timer_now();

csp_timer_duration_t #


csp_timer_duration_t is the type of duration between two timestamps in nanoseconds. Libcsp provides several macros to describe the units.

  • csp_timer_nanosecond
  • csp_timer_microsecond
  • csp_timer_millisecond
  • csp_timer_second
  • csp_timer_minute
  • csp_timer_hour

Example:

csp_timer_time_t start = csp_timer_now();
csp_timer_duration_t duration = csp_timer_now() - start;
printf("Time slipped %ld nanoseconds\n", duration);

csp_timer_t #


csp_timer_t is the type of a timer. It is created by csp_timer_at or csp_timer_after.

csp_timer_at(when, task) #


csp_timer_at(when, task) creates and returns a timer which will fire at when.

  • when: The timestamp in nanosecond when the timer fires.
  • task: The task triggered when the timer fires.

Example:

void echo(const char *content) {
  printf("%s\n", content);
}
csp_timer_t timer = csp_timer_at(csp_timer_now() + csp_timer_second, echo("Timer fires"));
NOTE: The task should be a single function call.

csp_timer_after(duration, task) #


csp_timer_after(duration, task) creates and returns a timer which will fire after duration nanoseconds.

  • duration: The duration after which the timer will fire.
  • task: The task triggered when the timer fires.

Example:

csp_timer_t timer = csp_timer_after(csp_timer_second, echo("Timer fires"));
NOTE: The task should be a single function call.

bool csp_timer_cancel(csp_timer_t timer) #


csp_timer_cancel cancels the timer. It will be ignored if the timer has fired or been canceled. If success, it will return true, otherwise false.

Example:

csp_timer_cancel(timer);