From 1d387e9f4ca12182ea8ec0ad02cff1673040bee2 Mon Sep 17 00:00:00 2001 From: Riggin Date: Sat, 5 Aug 2023 10:20:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91?= =?UTF-8?q?=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF=20&&=20=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- multi_timer.c | 24 ++++++++++++------------ multi_timer.h | 24 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/multi_timer.c b/multi_timer.c index 334d643..7fce40a 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. @@ -26,19 +26,19 @@ 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_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,7 +74,7 @@ int timer_stop(struct Timer* handle) return 0; // found specified timer } else { curr = &entry->next; - } + } } return 0; @@ -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 From ccb71779ca814889d9b03f31c8b9472ba4a161d3 Mon Sep 17 00:00:00 2001 From: Riggin Date: Sat, 5 Aug 2023 10:23:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91timer?= =?UTF-8?q?=5Fstop()=E8=BF=94=E5=9B=9E=E5=80=BC=E9=97=AE=E9=A2=98=20&&=20m?= =?UTF-8?q?emset()=E5=8F=82=E6=95=B0=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- multi_timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/multi_timer.c b/multi_timer.c index 7fce40a..1ea1d40 100755 --- a/multi_timer.c +++ b/multi_timer.c @@ -25,7 +25,7 @@ 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); + // memset(handle, 0, sizeof(struct Timer)); handle->timeout_cb = timeout_cb; handle->timeout = timeout; handle->repeat = repeat; @@ -77,7 +77,7 @@ int timer_stop(struct Timer* handle) } } - return 0; + return -1; } /**