fix: Add timeout_cb function input param && add tick timer configuration

pull/6/head
recan-li 5 years ago
parent 9ac402d160
commit e1296bfcb2

@ -18,12 +18,14 @@ static uint32_t _timer_ticks = 0;
* @param repeat: repeat interval time. * @param repeat: repeat interval time.
* @retval None * @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); // memset(handle, sizeof(struct Timer), 0);
handle->timeout_cb = timeout_cb; handle->timeout_cb = timeout_cb;
handle->timeout = _timer_ticks + timeout; handle->timeout = _timer_ticks + timeout;
handle->repeat = repeat; handle->repeat = repeat;
handle->arg = arg;
} }
/** /**
@ -86,18 +88,18 @@ void timer_loop(void)
{ {
target->timeout = _timer_ticks + target->repeat; 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. * @param None.
* @retval None. * @retval None.
*/ */
void timer_ticks(void) void timer_ticks(void)
{ {
_timer_ticks++; _timer_ticks += CFG_TIMER_1_TICK_N_MS;
} }

@ -9,18 +9,26 @@
#include "stdint.h" #include "stdint.h"
#include "stddef.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 { typedef struct Timer {
uint32_t timeout; uint32_t timeout;
uint32_t repeat; uint32_t repeat;
void (*timeout_cb)(void); void * arg; /* Input argument for timeout_cb function */
struct Timer* next; void (*timeout_cb)(void *arg);
}Timer; struct Timer* next;
} Timer;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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); int timer_start(struct Timer* handle);
void timer_stop(struct Timer* handle); void timer_stop(struct Timer* handle);
void timer_ticks(void); void timer_ticks(void);

Loading…
Cancel
Save