commit 5e7ef9c5f4d9ed28fcb66f6dec98513b77baeb3f Author: Jaup <270995079@qq.com> Date: Wed Sep 21 08:22:53 2016 +0000 first commit diff --git a/multi_timer.c b/multi_timer.c new file mode 100644 index 0000000..a7ef772 --- /dev/null +++ b/multi_timer.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016 Zibin Zheng + * All rights reserved + */ + +#include "multi_timer.h" + +//timer handle list head. +static struct Timer* head_handle = NULL; + +//Timer ticks +static uint32_t _timer_ticks = 0; + +/** + * @brief Initializes the timer struct handle. + * @param handle: the timer handle strcut. + * @param timeout_cb: timeout callback. + * @param repeat: repeat interval time. + * @retval None + */ +void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat) +{ + // memset(handle, sizeof(struct Timer), 0); + handle->timeout_cb = timeout_cb; + handle->timeout = _timer_ticks + timeout; + handle->repeat = repeat; +} + +/** + * @brief Start the timer work, add the handle into work list. + * @param btn: target handle strcut. + * @retval 0: succeed. -1: already exist. + */ +int timer_start(struct Timer* handle) +{ + struct Timer* target = head_handle; + while(target) { + if(target == handle) return -1; //already exist. + target = target->next; + } + handle->next = head_handle; + head_handle = handle; + return 0; +} + +/** + * @brief Stop the timer work, remove the handle off work list. + * @param handle: target handle strcut. + * @retval None + */ +void timer_stop(struct Timer* handle) +{ + struct Timer** curr; + for(curr = &head_handle; *curr; ) { + struct Timer* entry = *curr; + if (entry == handle) { + *curr = entry->next; +// free(entry); + } else + curr = &entry->next; + } +} + +/** + * @brief background ticks, timer repeat invoking interval 5ms. + * @param None. + * @retval None + */ +void timer_loop() +{ + struct Timer* target; + for(target=head_handle; target; target=target->next) { + if(_timer_ticks >= target->timeout) { + if(target->repeat == 0) { + timer_stop(target); + } else { + target->timeout = _timer_ticks + target->repeat; + } + target->timeout_cb(); + } + } +} + +/** + * @brief background ticks, timer repeat invoking interval 1ms. + * @param None. + * @retval None + */ +void timer_ticks() +{ + _timer_ticks++; +} + diff --git a/multi_timer.h b/multi_timer.h new file mode 100644 index 0000000..223f98a --- /dev/null +++ b/multi_timer.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 Zibin Zheng + * All rights reserved + */ + +#ifndef _MULTI_TIMER_H_ +#define _MULTI_TIMER_H_ + +#include "stdint.h" +#include "string.h" + +typedef struct Timer { + uint32_t timeout; + uint32_t repeat; + void (*timeout_cb)(void); + struct Timer* next; +}Timer; + +#ifdef __cplusplus +extern "C" { +#endif + +void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat); +int timer_start(struct Timer* handle); +void timer_stop(struct Timer* handle); +void timer_ticks(void); +void timer_loop(void); + +// void timer_again(struct Timer* handle); +// void timer_set_repeat(struct Timer* handle, uint32_t repeat); + +#ifdef __cplusplus +} +#endif + +#endif