Skip to content

ESPToolKit/esp-timer

Repository files navigation

ESPTimer

CI Release License: MIT

Lightweight JS-like timers for ESP32 with non-blocking FreeRTOS tasks.

Features

  • setTimeout (one-shot), setInterval (periodic)
  • Counters: per-second, per-millisecond, per-minute with remaining time
  • Each timer type runs on its own FreeRTOS task
  • Pause, resume, toggle run status, clear, and status query per timer ID

Quick Start

  • Include #include <ESPTimer.h> and call timer.init() once (optionally with ESPTimerConfig).
  • Use the API similar to JS timers:
// Triggers once
uint32_t id1 = timer.setTimeout([](){
  Serial.println("1.5 sec is timed out!");
}, 1500);

// Retriggers every 1500ms
uint32_t id2 = timer.setInterval([](){
  Serial.println("1.5 sec is triggered!");
}, 1500);

// Called every sec for 10000 ms
uint32_t id3 = timer.setSecCounter([](int secLeft){
  Serial.printf("%d sec left so far\n", secLeft);
}, 10000);

// Called every ms for 10000 ms
uint32_t id4 = timer.setMsCounter([](uint32_t msLeft){
  // msLeft can be high frequency; keep work light
}, 10000);

// Called every min for 10000 ms
uint32_t id5 = timer.setMinCounter([](int minLeft){
  Serial.printf("%d min left so far\n", minLeft);
}, 10000);

// Pause, resume, toggle, and clear
timer.pauseInterval(id2);                         // Pauses if running
timer.resumeInterval(id2);                        // Resumes if paused
bool running = timer.toggleRunStatusInterval(id2); // Toggles; true if now running
timer.clearInterval(id2);

// Status
ESPTimerStatus status = timer.getStatus(id1);

Notes

  • pause* only pauses (idempotent). Use resume* to resume, or toggleRunStatus* to toggle (returns true if now running).
  • setMsCounter can be CPU intensive; use sparingly and keep callbacks very light.
  • Each type uses its own FreeRTOS task. Configure stack, priority, and core with ESPTimerConfig.

See examples/Basic/Basic.ino for a complete sketch.

ESPToolKit

About

Async timer helpers mirroring setTimeout / setInterval with dedicated countdown utilities for seconds, minutes and milliseconds.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published