Signal Utilities

Two small helpers that don't own a peripheral: the shift-register functions and the random generator. For a square wave on a buzzer, a PWM channel at 50% duty does it in two calls — setPeriod(1_000_000_000 / hz) then setDuty(0.5); see Analog & PWM.

functions

shiftOut / shiftIn

Send and receive bytes through a shift register.

Example first

shift.ts
import { shiftOut, shiftIn } from '@typecad/hal';

shiftOut(dataPin, clockPin, 0b10110001);        // MSB-first by default
const b = shiftIn(dataPin, clockPin);           // reads 8 bits

All functions

FunctionDescription
shiftOut(data, clock, value, msbFirst?)Send a byte out
shiftIn(data, clock, msbFirst?)Read a byte in
singleton

Random

The random generator. Seeded from hardware entropy at boot, so seed() is only needed for deterministic sequences (tests, simulations).

Example first

random.ts
import { Random } from '@typecad/hal';

Random.seed(0xC0FFEE);               // only when you want reproducibility
const die = Random.upTo(6);          // 0-5
const pct = Random.between(1, 101);  // 1-100
const big = Random.int();            // non-negative 31-bit integer

upTo Random.upTo(max): number → 0 … max−1

between Random.between(min, max): number → min through max−1

int Random.int(): number → 31-bit non-negative

All methods

MethodDescription
Random.seed(v)Seed the PRNG — only needed for deterministic sequences (tests, simulations)
Random.upTo(max)0 up to (but not including) max
Random.between(min, max)Integer from min up to (but not including) max
Random.int()Non-negative 31-bit integer
© 2026 typeCAD — code-defined electronics github