diff --git a/multi_timer.c b/multi_timer.c index 334d643..1ea1d40 100755 --- a/multi_timer.c +++ b/multi_timer.c @@ -15,7 +15,7 @@ static uint32_t _timer_ticks = 0; /** * @brief Initializes the timer struct handle. - * @param handle: the timer handle strcut. + * @param handle: the timer handle struct. * @param timeout_cb: timeout callback. * @param timeout: delay to start the timer. * @param repeat: repeat interval time. @@ -25,20 +25,20 @@ static uint32_t _timer_ticks = 0; 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; + // memset(handle, 0, sizeof(struct Timer)); + handle->timeout_cb = timeout_cb; handle->timeout = timeout; handle->repeat = repeat; - handle->cur_ticks = _timer_ticks; + handle->cur_ticks = _timer_ticks; handle->cur_expired_time = handle->timeout; handle->arg = arg; - //printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u, timeout: %u\r\n", + //printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u, timeout: %u\r\n", // handle->cur_ticks, handle->cur_expired_time, _timer_ticks, timeout); } /** * @brief Start the timer work, add the handle into work list. - * @param btn: target handle strcut. + * @param handle: target handle struct. * @retval 0: succeed. -1: already exist. */ int timer_start(struct Timer* handle) @@ -48,7 +48,7 @@ int timer_start(struct Timer* handle) while(target) { if(target == handle) { return -1; //already exist. - } + } target = target->next; } handle->next = head_handle; @@ -59,7 +59,7 @@ int timer_start(struct Timer* handle) /** * @brief Stop the timer work, remove the handle off work list. - * @param handle: target handle strcut. + * @param handle: target handle struct. * @retval 0: succeed. -1: timer not exist. */ int timer_stop(struct Timer* handle) @@ -74,10 +74,10 @@ int timer_stop(struct Timer* handle) return 0; // found specified timer } else { curr = &entry->next; - } + } } - return 0; + return -1; } /** @@ -90,18 +90,18 @@ void timer_loop(void) struct Timer* target; for(target = head_handle; target; target = target->next) { - /* + /* More detail on tick-clock overflow, please see https://blog.csdn.net/szullc/article/details/115332326 */ if(_timer_ticks - target->cur_ticks >= target->cur_expired_time) { - //printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u\r\n", + //printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u\r\n", // target->cur_ticks, target->cur_expired_time, _timer_ticks); if(target->repeat == 0) { timer_stop(target); } else { target->cur_ticks = _timer_ticks; target->cur_expired_time = target->repeat; - } + } target->timeout_cb(target->arg); } } @@ -110,7 +110,7 @@ void timer_loop(void) /** * @brief background ticks, timer repeat invoking interval nms. * @param None. - * @retval None. + * @retval None */ void timer_ticks(void) { diff --git a/multi_timer.h b/multi_timer.h index cb3e533..004674a 100755 --- a/multi_timer.h +++ b/multi_timer.h @@ -2,7 +2,7 @@ * Copyright (c) 2016 Zibin Zheng * All rights reserved */ - + #ifndef _MULTI_TIMER_H_ #define _MULTI_TIMER_H_ @@ -10,7 +10,7 @@ #include /* -It means 1 tick for 1ms. +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 @@ -18,20 +18,20 @@ Your can configurate for your tick time such as 5ms/10ms and so on. typedef struct Timer { uint32_t cur_ticks; /* Record current timer start tick */ uint32_t cur_expired_time; /* Record current timer expired time */ - uint32_t timeout; /* Delay (xx ms) time to start tiemr */ + uint32_t timeout; /* Delay (xx ms) time to start timer */ uint32_t repeat; /* Timer interval expired time (xx ms) */ void * arg; /* Input argument for timeout_cb function */ void (*timeout_cb)(void *arg); /* Timer expired callback function */ struct Timer* next; /* Pointer to next timer */ } Timer; -#ifdef __cplusplus -extern "C" { -#endif +#ifdef __cplusplus +extern "C" { +#endif /** * @brief Initializes the timer struct handle. - * @param handle: the timer handle strcut. + * @param handle: the timer handle struct. * @param timeout_cb: timeout callback. * @param timeout: delay to start the timer. * @param repeat: repeat interval time. @@ -43,14 +43,14 @@ void timer_init(struct Timer* handle, void(*timeout_cb)(void *arg), \ /** * @brief Start the timer work, add the handle into work list. - * @param btn: target handle strcut. + * @param handle: target handle struct. * @retval 0: succeed. -1: already exist. */ -int timer_start(struct Timer* handle); +int timer_start(struct Timer* handle); /** * @brief Stop the timer work, remove the handle off work list. - * @param handle: target handle strcut. + * @param handle: target handle struct. * @retval 0: succeed. -1: timer not exist. */ int timer_stop(struct Timer* handle); @@ -58,7 +58,7 @@ int timer_stop(struct Timer* handle); /** * @brief background ticks, timer repeat invoking interval nms. * @param None. - * @retval None. + * @retval None */ void timer_ticks(void); @@ -70,7 +70,7 @@ void timer_ticks(void); void timer_loop(void); #ifdef __cplusplus -} +} #endif #endif