diff --git a/multi_timer.c b/multi_timer.c old mode 100644 new mode 100755 index feb9cae..3d49873 --- a/multi_timer.c +++ b/multi_timer.c @@ -18,12 +18,14 @@ static uint32_t _timer_ticks = 0; * @param repeat: repeat interval time. * @retval None */ -void timer_init(struct Timer* handle, void (*timeout_cb)(), uint32_t timeout, uint32_t repeat) +void timer_init(struct Timer* handle, void (*timeout_cb)(void *arg), \ + uint32_t timeout, uint32_t repeat, void *arg) { // memset(handle, sizeof(struct Timer), 0); handle->timeout_cb = timeout_cb; handle->timeout = _timer_ticks + timeout; handle->repeat = repeat; + handle->arg = arg; } /** @@ -86,18 +88,18 @@ void timer_loop(void) { target->timeout = _timer_ticks + target->repeat; } - target->timeout_cb(); + target->timeout_cb(target->arg); } } } /** - * @brief background ticks, timer repeat invoking interval 1ms. + * @brief background ticks, timer repeat invoking interval nms. * @param None. * @retval None. */ void timer_ticks(void) { - _timer_ticks++; + _timer_ticks += CFG_TIMER_1_TICK_N_MS; } diff --git a/multi_timer.h b/multi_timer.h old mode 100644 new mode 100755 index edc3e38..0f60453 --- a/multi_timer.h +++ b/multi_timer.h @@ -9,18 +9,26 @@ #include "stdint.h" #include "stddef.h" +/* +It means 1 tick for 1ms. +Your can configurate for your tick time such as 5ms/10ms and so on. +*/ +#define CFG_TIMER_1_TICK_N_MS 1 + typedef struct Timer { - uint32_t timeout; - uint32_t repeat; - void (*timeout_cb)(void); - struct Timer* next; -}Timer; + uint32_t timeout; + uint32_t repeat; + void * arg; /* Input argument for timeout_cb function */ + void (*timeout_cb)(void *arg); + struct Timer* next; +} Timer; #ifdef __cplusplus extern "C" { #endif -void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat); +void timer_init(struct Timer* handle, void(*timeout_cb)(void *arg), \ + uint32_t timeout, uint32_t repeat, void *arg); int timer_start(struct Timer* handle); void timer_stop(struct Timer* handle); void timer_ticks(void);