Compare commits

...

3 Commits

Author SHA1 Message Date
0x1abin 8552905e23
Merge pull request #16 from Advanced-lj/master
修改拼写错误 && 格式化代码
2 years ago
Riggin ccb71779ca 【修改】timer_stop()返回值问题 && memset()参数顺序 2 years ago
Riggin 1d387e9f4c 【修改】拼写错误 && 格式化代码 2 years ago

@ -15,7 +15,7 @@ static uint32_t _timer_ticks = 0;
/** /**
* @brief Initializes the timer struct handle. * @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_cb: timeout callback.
* @param timeout: delay to start the timer. * @param timeout: delay to start the timer.
* @param repeat: repeat interval time. * @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), \ void timer_init(struct Timer* handle, void (*timeout_cb)(void *arg), \
uint32_t timeout, uint32_t repeat, 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_cb = timeout_cb;
handle->timeout = timeout; handle->timeout = timeout;
handle->repeat = repeat; handle->repeat = repeat;
handle->cur_ticks = _timer_ticks; handle->cur_ticks = _timer_ticks;
handle->cur_expired_time = handle->timeout; handle->cur_expired_time = handle->timeout;
handle->arg = arg; 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); // handle->cur_ticks, handle->cur_expired_time, _timer_ticks, timeout);
} }
/** /**
* @brief Start the timer work, add the handle into work list. * @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. * @retval 0: succeed. -1: already exist.
*/ */
int timer_start(struct Timer* handle) int timer_start(struct Timer* handle)
@ -48,7 +48,7 @@ int timer_start(struct Timer* handle)
while(target) { while(target) {
if(target == handle) { if(target == handle) {
return -1; //already exist. return -1; //already exist.
} }
target = target->next; target = target->next;
} }
handle->next = head_handle; 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. * @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. * @retval 0: succeed. -1: timer not exist.
*/ */
int timer_stop(struct Timer* handle) int timer_stop(struct Timer* handle)
@ -74,10 +74,10 @@ int timer_stop(struct Timer* handle)
return 0; // found specified timer return 0; // found specified timer
} else { } else {
curr = &entry->next; curr = &entry->next;
} }
} }
return 0; return -1;
} }
/** /**
@ -90,18 +90,18 @@ void timer_loop(void)
struct Timer* target; struct Timer* target;
for(target = head_handle; target; target = target->next) { for(target = head_handle; target; target = target->next) {
/* /*
More detail on tick-clock overflow, please see https://blog.csdn.net/szullc/article/details/115332326 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) { 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); // target->cur_ticks, target->cur_expired_time, _timer_ticks);
if(target->repeat == 0) { if(target->repeat == 0) {
timer_stop(target); timer_stop(target);
} else { } else {
target->cur_ticks = _timer_ticks; target->cur_ticks = _timer_ticks;
target->cur_expired_time = target->repeat; target->cur_expired_time = target->repeat;
} }
target->timeout_cb(target->arg); target->timeout_cb(target->arg);
} }
} }
@ -110,7 +110,7 @@ void timer_loop(void)
/** /**
* @brief background ticks, timer repeat invoking interval nms. * @brief background ticks, timer repeat invoking interval nms.
* @param None. * @param None.
* @retval None. * @retval None
*/ */
void timer_ticks(void) void timer_ticks(void)
{ {

@ -2,7 +2,7 @@
* Copyright (c) 2016 Zibin Zheng <znbin@qq.com> * Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
* All rights reserved * All rights reserved
*/ */
#ifndef _MULTI_TIMER_H_ #ifndef _MULTI_TIMER_H_
#define _MULTI_TIMER_H_ #define _MULTI_TIMER_H_
@ -10,7 +10,7 @@
#include <stddef.h> #include <stddef.h>
/* /*
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. Your can configurate for your tick time such as 5ms/10ms and so on.
*/ */
#define CFG_TIMER_1_TICK_N_MS 1 #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 { typedef struct Timer {
uint32_t cur_ticks; /* Record current timer start tick */ uint32_t cur_ticks; /* Record current timer start tick */
uint32_t cur_expired_time; /* Record current timer expired time */ 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) */ uint32_t repeat; /* Timer interval expired time (xx ms) */
void * arg; /* Input argument for timeout_cb function */ void * arg; /* Input argument for timeout_cb function */
void (*timeout_cb)(void *arg); /* Timer expired callback function */ void (*timeout_cb)(void *arg); /* Timer expired callback function */
struct Timer* next; /* Pointer to next timer */ struct Timer* next; /* Pointer to next timer */
} Timer; } Timer;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* @brief Initializes the timer struct handle. * @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_cb: timeout callback.
* @param timeout: delay to start the timer. * @param timeout: delay to start the timer.
* @param repeat: repeat interval time. * @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. * @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. * @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. * @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. * @retval 0: succeed. -1: timer not exist.
*/ */
int timer_stop(struct Timer* handle); int timer_stop(struct Timer* handle);
@ -58,7 +58,7 @@ int timer_stop(struct Timer* handle);
/** /**
* @brief background ticks, timer repeat invoking interval nms. * @brief background ticks, timer repeat invoking interval nms.
* @param None. * @param None.
* @retval None. * @retval None
*/ */
void timer_ticks(void); void timer_ticks(void);
@ -70,7 +70,7 @@ void timer_ticks(void);
void timer_loop(void); void timer_loop(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

Loading…
Cancel
Save