Skip to content

Instantly share code, notes, and snippets.

@lchagnoleau
Created March 16, 2023 08:58
Show Gist options
  • Save lchagnoleau/0a733852ec2969dd57951b86638ca44f to your computer and use it in GitHub Desktop.
Save lchagnoleau/0a733852ec2969dd57951b86638ca44f to your computer and use it in GitHub Desktop.
Timer
/**
* @file timer_functions.c
*
* @brief Functions for working with timers.
*/
#include "timer.h"
/**
* @brief Start a timer with the specified duration.
*
* @param timer A pointer to a Timer structure.
* @param duration The duration of the timer in milliseconds.
*
* @example
* Timer t;
* start_timer(&t, 5000); // Start a 5-second timer
*/
void start_timer(Timer* timer, uint32_t duration)
{
timer->start_time = get_time_ms();
timer->duration = duration;
}
/**
* @brief Check if a timer has elapsed.
*
* @param timer A pointer to a Timer structure.
* @return True if the timer has elapsed, false otherwise.
*
* @example
* Timer t;
* start_timer(&t, 5000); // Start a 5-second timer
* while (!is_timer_elapsed(&t)) {
* // Do something while the timer is running
* }
* // The timer has now elapsed
*/
bool is_timer_elapsed(Timer* timer)
{
uint32_t current_time = get_time_ms();
return (current_time - timer->start_time) > timer->duration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment