Timing & Clocks

One object, four verbs. Time covers the whole timing vocabulary: a yielding sleep, two monotonic clocks, and a spin for sub-millisecond protocol timing.

singleton

Time

The whole timing vocabulary in one object. The clocks are monotonic and never wrap, so elapsed-time checks stay correct for the life of the program.

Example first

time.ts
Time.sleep(1000);        // blocks at the top level

async function poll() {
  while (true) {
    await Time.sleep(50);      // other tasks run during the wait
    // ...read a sensor, update a display...
  }
}

Time.busyWaitUs(3);       // spins — for sub-millisecond timing

sleep Time.sleep(ms): void

At the top level this blocks main until the time passes. Inside a Thread it blocks that thread only — everything else keeps running. Inside an async function, await the same call and other tasks run while you wait.

busyWaitUs Time.busyWaitUs(us): void

For sub-millisecond protocol timing where waiting would break the waveform. This is the only Time call that never yields — don't use it for long waits; it holds the CPU.

Clocks

Two monotonic clocks, both counting from boot. Both return doubles, so they never wrap. Elapsed-time arithmetic is plain subtraction — there is no unsigned wraparound to guard against.

clocks.ts
const start = Time.now();       // milliseconds since boot
const us = Time.nowUs();        // microseconds since boot

if (Time.now() - start > 5000) {   // correct at minute 1 and month 6
  // five seconds have passed
}

Periodic work

Time deliberately has no callback timers. Periodic work belongs to a Thread — or, when the period must be exact to the microsecond, to a hardware Counter alarm.

periodic.ts
import { Thread } from '@typecad/hal';

const blinker = new Thread(0, { stackKb: 2 });
blinker.start(() => {
  while (true) {
    led.toggle();
    Time.sleep(500);
  }
});

All methods

MemberDescription
Time.sleep(ms)Yielding sleep; await it inside async code and other tasks keep running
Time.now()Milliseconds since boot, monotonic, double — no wrap
Time.nowUs()Microseconds since boot, double; millisecond resolution on every board
Time.busyWaitUs(us)Spin-wait — never yields; sub-millisecond protocol timing only
© 2026 typeCAD — code-defined electronics github