Skip to main content

Cross-runtime millisecond tick helpers and periodic timing utilities.

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

# CPython (pip)
pip install chumicro-timing

# CircuitPython (circup)
circup bundle-add ChuMicro/chumicro-bundle
circup install chumicro-timing

# MicroPython (mip)
mpremote mip install github:ChuMicro/chumicro-bundle/chumicro_timing

For experimental (pre-release) versions from the develop branch:

pip install chumicro-timing-experimental
circup install chumicro-timing-experimental
mpremote mip install github:ChuMicro/chumicro-bundle/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

Docs

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.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

chumicro_timing_experimental-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for chumicro_timing_experimental-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ad81895f614438ac20fbd2c89ab9882c96b5b5b8e9a0316eea65e7e047d663c0
MD5 91c52187ba17a9a22590a9fe1f4c744b
BLAKE2b-256 d3a6d628aa69b916078ee8fdcd665010d5bff6b5849d0bc905fbb62031f0c4ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_timing_experimental-0.1.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for chumicro_timing_experimental-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cefc390633e3f1659155d3e003cc7b4ba19087a926aa6957a0d77f8732f4dccd
MD5 2394c91ac1c4003fe8ae91ca54e8614f
BLAKE2b-256 8b297bc734e5a254878b7c3eef0c3ff0bbe5b0f2e3d93cea21657d125b4e6acb

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_timing_experimental-0.1.0-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