Skip to main content

Wraparound-safe millisecond tick helpers and heartbeat scheduling for CircuitPython, MicroPython, and CPython.

Project description

chumicro-timing

Cross-runtime millisecond tick helpers and periodic timing utilities for CircuitPython, MicroPython, and CPython.

All timing is non-blocking — nothing in this library calls time.sleep(). Capture ticks_ms() once per loop iteration and pass the shared timestamp to Heartbeat.poll().

Installation

CircuitPython (circup)

Register the ChuMicro bundle (remove the other channel first if switching):

circup bundle-remove ChuMicro/ChuMicro-Bundle-Experimental   # skip if never added
circup bundle-add ChuMicro/ChuMicro-Bundle
circup install chumicro-timing

MicroPython (mip)

mpremote mip install github:ChuMicro/ChuMicro-Bundle/chumicro_timing

CPython (pip)

pip install chumicro-timing

Experimental (pre-release) versions

Pre-release builds are published automatically when a library version is bumped. Do not register both bundles simultaneously — circup may pick either version for a given package.

# CircuitPython
circup bundle-remove ChuMicro/ChuMicro-Bundle              # skip if never added
circup bundle-add ChuMicro/ChuMicro-Bundle-Experimental
circup install chumicro-timing

# MicroPython
mpremote mip install github:ChuMicro/ChuMicro-Bundle-Experimental/chumicro_timing

# CPython
pip install chumicro-timing-experimental

Quick example

from chumicro_timing import Heartbeat, ticks_ms

heartbeat = Heartbeat(period_ms=1000)

while True:
    now = ticks_ms()
    if heartbeat.poll(now):
        print("one second elapsed")
    # ... do other work ...

What's included

Tick functions

Symbol Description
ticks_ms() Monotonic millisecond counter, wraps every ~6.2 days
ticks_diff(end, start) Wraparound-safe signed difference
ticks_add(ticks, delta) Wraparound-safe addition

Heartbeat

Symbol Description
Heartbeat(period_ms, ticks=None) Periodic timer that fires once per elapsed period
Heartbeat.poll(now_ms) Returns True once per period and advances the timer
Heartbeat.is_due(now_ms) Check whether the period has elapsed (without advancing)
Heartbeat.reset(now_ms) Restart the timer from the given timestamp
Heartbeat.period_ms The configured period (read-only property)

Testing

Symbol Description
FakeTicks(start_ms=0) Deterministic tick source for host-side tests
FakeTicks.advance(amount_ms) Move the fake clock forward

Platform support

The tick source is selected automatically at import time:

Priority Source Runtime
1 supervisor.ticks_ms CircuitPython 7+
2 time.ticks_ms MicroPython, some CircuitPython builds
3 time.monotonic_ns CPython, some CircuitPython boards
4 time.monotonic Final fallback (float seconds → int ms)

All sources are masked to a 2²⁹ ms period, so behavior is identical regardless of which source is used.

Testing your code

The chumicro_timing.testing module provides FakeTicks for deterministic host-side tests — no wall-clock waits:

from chumicro_timing import Heartbeat
from chumicro_timing.testing import FakeTicks

fake = FakeTicks()
heartbeat = Heartbeat(period_ms=100, ticks=fake)

now = fake.ticks_ms()
assert heartbeat.poll(now) is False

fake.advance(100)
now = fake.ticks_ms()
assert heartbeat.poll(now) is True

Examples

Example What it shows
heartbeat_blink.py Basic periodic timer loop
multiple_heartbeats.py Multiple heartbeats at different rates
timeout_check.py One-shot timeout using is_due
debounce.py Simulated button debounce
periodic_tick.py Manual periodic loop (what Heartbeat does under the hood)
circuitpython_blink.py LED blink on CircuitPython hardware
circuitpython_debounce.py GPIO button debounce on CircuitPython
micropython_blink.py LED blink on MicroPython hardware
micropython_debounce.py GPIO button debounce on MicroPython

Docs

📖 Stable docs · Experimental docs

Browse on GitHub:

Find this library

PyPI: chumicro-timing Bundle: ChuMicro-Bundle (CircuitPython & MicroPython) Source: ChuMicro/ChuMicro — cross-runtime Python libraries for ESP32, RP2040, and other microcontrollers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chumicro_timing_experimental-0.1.13.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file chumicro_timing_experimental-0.1.13.tar.gz.

File metadata

File hashes

Hashes for chumicro_timing_experimental-0.1.13.tar.gz
Algorithm Hash digest
SHA256 688d4031e8d72df6a9e9cac5870a16c702e9aa5cbf783b1002108d5884d32f47
MD5 20033b965f1b35bcb67b3935a97ba368
BLAKE2b-256 e59d13967527c67d3d0120ca76d31271e4c149e9f02a261f27ad36adf98ddc4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_timing_experimental-0.1.13.tar.gz:

Publisher: release.yml on ChuMicro/ChuMicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chumicro_timing_experimental-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for chumicro_timing_experimental-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 71b91aacdcad6b34f5df7b06750ab9f2b20245ca037a51da43a0fe70a28b1bc2
MD5 a57151fd737da00200823304b9f9eeba
BLAKE2b-256 d2010a100cd7699c935af6a4dfa9e4c273047ab1765fb8cbcbc6957f4617a192

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_timing_experimental-0.1.13-py3-none-any.whl:

Publisher: release.yml on ChuMicro/ChuMicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page