百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

arduino-timer

> 编程语言
开源

用于延迟函数调用的非阻塞库

357 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于延迟函数调用的非阻塞库

arduino-timer - library for delaying function calls

Simple non-blocking timer library for calling functions in / at / every specified units of time. Supports millis, micros, time rollover, and compile time configurable number of tasks.

Use It

Include the library and create a Timer instance.

cpp
#include 

auto timer = timer_create_default();

Or using the Timer constructors for different task limits / time resolution

cpp
Timer timer; // 10 concurrent tasks, using millis as resolution
Timer timer; // 10 concurrent tasks, using micros as resolution
Timer timer; // 10 concurrent tasks, using micros as resolution, with handler argument of type int

Call timer.tick() in the loop function

cpp
void loop() {
    timer.tick();
}

Make a function to call when the Timer expires

cpp
bool function_to_call(void *argument /* optional argument given to in/at/every */) {
    return true; // to repeat the action - false to stop
}

Call function_to_call in delay units of time (unit of time defaults to milliseconds).

cpp
timer.in(delay, function_to_call);
timer.in(delay, function_to_call, argument); // or with an optional argument for function_to_call

Call function_to_call at a specific time.

cpp
timer.at(time, function_to_call);
timer.at(time, function_to_call, argument); // with argument

Call function_to_call every interval units of time.

cpp
timer.every(interval, function_to_call);
timer.every(interval, function_to_call, argument); // with argument

To cancel a Task

cpp
auto task = timer.in(delay, function_to_call);
timer.cancel(task);

To cancel all Tasks

cpp
timer.cancel();

Check if a timer is empty - no active Tasks

cpp
if (timer.empty()) { /* no active tasks */ }

Get the number of active Tasks

cpp
auto active_tasks = timer.size();

Be fancy with lambdas

cpp
timer.in(1000, [](void*) -> bool { return false; });
timer.in(1000, [](void *argument) -> bool { return argument; }, argument);

Getting the number of ticks until the next Task

cpp
auto ticks = timer.ticks(); // usefull for sleeping until the next task
cpp
void loop {
    auto ticks = timer.tick(); // returns the number of ticks
}

Avoiding ticks calculation inside of tick

cpp
void loop {
    timer.tick(); // avoids ticks() calculation
}

API

…

Installation

Check out the instructions from Arduino.

OR copy src/arduino-timer.h into your project folder (you won't get managed updates this way).

Examples

Found in the examples/ folder.

The simplest example, blinking an LED every second (from examples/blink):

cpp
#include 

auto timer = timer_create_default(); // create a timer with default settings

bool toggle_led(void *) {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
  return true; // keep timer active? true
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT

  // call the toggle_led function every 1000 millis (1 second)
  timer.every(1000, toggle_led);
}

void loop() {
  timer.tick(); // tick the timer
}

LICENSE

Check the LICENSE file - 3-Clause BSD License

Notes

Currently only a software timer. Any blocking code delaying timer.tick() will prevent the timer from moving forward and calling any functions.

The library does not do any dynamic memory allocation.

The number of concurrent tasks is a compile time constant, meaning there is a limit to the number of concurrent tasks. The in / at / every functions return NULL if the Timer is full.

A Task value is valid only for the timer that created it, and only for the lifetime of that timer.

Change the number of concurrent tasks using the Timer constructors. Save memory by reducing the number, increase memory use by having more. The default is TIMER_MAX_TASKS which is currently 16.

If you find this project useful, consider becoming a sponsor.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++arduinoarduino-libraryarduino-timerconcurrent-tasks

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言