Skip to content

A lightweight, non-blocking, and thread-safe timer library for Raspberry Pi Pico (RP2040). Supports millis/micros precision, finite bursts, and multi-core synchronisation.

License

Notifications You must be signed in to change notification settings

jit89/PicoTimer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PicoTimer

Build Status Latest Release License

A lightweight, non-blocking, template-based timer library specifically optimized for the Raspberry Pi Pico (RP2040) using the Earle Philhower Arduino Core.

Overview

PicoTimer allows you to execute functions at specific intervals without using delay(). It is designed to be "fire and forget," handling repetition logic and timing wrap-around (rollover) automatically.

Key Features

  • Non-Blocking: Uses polling via .update(), ensuring your main loop remains responsive.
  • Multiple Modes: * Infinite: Continuous repetition.
    • One-Shot: Runs once and stops (perfect for alarms/timeouts).
    • Finite Burst: Runs a fixed number of times (perfect for LED pulses).
  • Dual Precision: Supports MillisTimer (ms) and MicrosTimer (μs).
  • Multicore Ready: Includes PicoTimerSafe using hardware critical sections for safe communication between Core 0 and Core 1.
  • Zero Dependencies: No extra libraries required.

Installation

  1. Download this repository as a .zip file.
  2. In the Arduino IDE, go to Sketch -> Include Library -> Add .ZIP Library...
  3. Select the downloaded file.
  4. Alternatively, extract the folder into your Documents/Arduino/libraries/ directory.

Basic Usage

#include <PicoTimer.h>

void onTick() {
    Serial.println("1 Second Passed!");
}

// Create an infinite timer: 1000ms period, calls onTick, repeats = 0 (infinite)
MillisTimer heartbeat(1000, onTick, 0);

void setup() {
    Serial.begin(115200);
}

void loop() {
    // You must call update() in the infinite loop for the timer to work
    heartbeat.update();
}

Advanced: Multicore Safety

The RP2040 is a dual-core MCU. If Core 0 is running a timer but Core 1 needs to stop or reset it based on a sensor event, use MillisTimerSafe. This prevents memory corruption by using hardware-level locking.

#include <PicoTimer.h>

void emergencyCallback() {
    digitalWrite(LED_BUILTIN, HIGH);
}

// Thread-safe timer
MillisTimerSafe safetyTimer(500, emergencyCallback, 0);

void loop() {
    safetyTimer.update(); // Running on Core 0
}

void loop1() {
    // Running on Core 1: Safely stop the timer running on Core 0
    if (analogRead(A0) > 800) {
        safetyTimer.stop();
    }
}

API Reference

Classes

Class Timing Unit Thread Safe Description
MillisTimer Milliseconds No Standard timer for single-core logic.
MicrosTimer Microseconds No High-precision timer for single-core logic.
MillisTimerSafe Milliseconds Yes Safe for use across Core 0 and Core 1.
MicrosTimerSafe Microseconds Yes High-precision safety for multicore.

Method Details

All classes provide the following methods:

Method Return Type Description
update() void Polls the timer. Must be called in loop() or loop1().
reset() void Restarts the timer to the current time and restores the original repeat count.
stop() void Pauses the timer; update() will no longer trigger the callback.
resume() void Resumes a stopped timer from its current state.
setPeriod(val) void Dynamically updates the interval duration (uint32_t).
isRunning() bool Returns true if the timer is active and has repeats remaining.

Implementation Note

Thread Safety

The Safe variants utilize the RP2040's hardware critical_section to ensure that data is not corrupted when accessed simultaneously by Core 0 and Core 1. It is recommended to use the standard (non-safe) versions if you are only working on a single core to minimize CPU overhead.

About

A lightweight, non-blocking, and thread-safe timer library for Raspberry Pi Pico (RP2040). Supports millis/micros precision, finite bursts, and multi-core synchronisation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages