2 Star 4 Fork 6

David Hu / MultiTimer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

MultiTimer

简介

MultiTimer 是一个软件定时器扩展模块,可无限扩展你所需的定时器任务,取代传统的标志位判断方式, 更优雅更便捷地管理程序的时间触发时序。

使用方法

  1. 配置系统时间基准接口,安装定时器驱动;
uint64_t platform_ticks_get_interface(void) {
/* Platform implementation */
}

multi_timer_install_ticks(platform_ticks_get_interface);
  1. 实例化一个定时器对象;
multi_timer_t g_timer1_;
  1. 设置定时时间,超时回调处理函数, 用户上下指针,启动定时器;
int multi_timer_start(&g_timer1_, uint64_t timing, multi_timer_callback_t callback, void* user_data);
  1. 在主循环调用定时器后台处理函数
int main(int argc, char *argv[])
{
  ...
  while (1) {
    ...
    multi_timer_yield();
  }
}

功能限制

1.定时器的时钟频率直接影响定时器的精确度,尽可能采用1ms/5ms/10ms这几个精度较高的tick;

2.定时器的回调函数内不应执行耗时操作,否则可能因占用过长的时间,导致其他定时器无法正常超时;

3.由于定时器的回调函数是在 multi_timer_yield 内执行的,需要注意栈空间的使用不能过大,否则可能会导致栈溢出。

Examples

见example目录下的测试代码 test_linux.c为linux平台的测试demo。

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include "../multi_timer.h"

// global variable
multi_timer_t g_timer1_;
multi_timer_t g_timer2_;
multi_timer_t g_timer3_;

uint64_t platform_ticks_get_interface(void) {
  struct timespec current_time;
  clock_gettime(CLOCK_MONOTONIC, &current_time);
  return (uint64_t)((current_time.tv_sec * 1000) + (current_time.tv_nsec / 1000000)); // unit is millisecond.
}

void example_timer1_callback(multi_timer_t* timer, void *user_data) {
  printf("[%012ld] Timer:%p callback-> %s.\r\n", platform_ticks_get_interface(), timer, (char*)user_data);
  multi_timer_start(timer, 1000, example_timer1_callback, user_data);
}

void example_timer2_callback(multi_timer_t* timer, void *user_data) {
  printf("[%012ld] Timer:%p callback-> %s.\r\n", platform_ticks_get_interface(), timer, (char*)user_data);
}

void example_timer3_callback(multi_timer_t* timer, void *user_data) {
  printf("[%012ld] Timer:%p callback-> %s.\r\n", platform_ticks_get_interface(), timer, (char*)user_data);
  multi_timer_start(timer, 4567, example_timer3_callback, user_data);
}

int main(int argc, char *argv[]) {
  multi_timer_install_ticks(platform_ticks_get_interface);

  multi_timer_start(&g_timer1_, 1000, example_timer1_callback, "1000ms CYCLE timer");
  multi_timer_start(&g_timer2_, 5000, example_timer2_callback, "5000ms ONCE timer");
  multi_timer_start(&g_timer3_, 3456, example_timer3_callback, "3456ms delay start, 4567ms CYCLE timer");

  while (1) {
    multi_timer_yield();
  }
}
MIT License Copyright (c) 2018 Zibin Zheng Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Mirror of MultiTimer 展开 收起
C 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/mingdonghu/MultiTimer.git
git@gitee.com:mingdonghu/MultiTimer.git
mingdonghu
MultiTimer
MultiTimer
dev_hmd

搜索帮助

53164aa7 5694891 3bd8fe86 5694891