GPIO & Digital I/O

Digital pins use one small class: new GPIO(pin, flags) configures the pin, and set, get, and toggle control it.

class

GPIO

One digital pin. Construct it with the pin and its flags — everything is set at construction.

Example first

basic-usage.ts
import { GPIO, LED, BUTTON } from '@typecad/hal';

const led = new GPIO(LED, GPIO.OUTPUT | GPIO.OUTPUT_INIT_LOW);
const button = new GPIO(BUTTON, GPIO.INPUT | GPIO.PULL_UP);

led.set(true);          // drive the pin
if (button.get()) {     // read it
  led.toggle();         // atomic toggle
}

Construction flags

Combine flags with |. The pin argument takes a board pin export (LED, BUTTON, PB5), a number, or a Pin instance.

TokenZephyr equivalentMeaning
GPIO.INPUT / GPIO.OUTPUTGPIO_INPUT / GPIO_OUTPUTDirection
GPIO.OUTPUT_INIT_LOW / GPIO.OUTPUT_INIT_HIGHGPIO_OUTPUT_INIT_LOW / GPIO_OUTPUT_INIT_HIGHOutput's initial level, set atomically — no configure-then-write glitch
GPIO.PULL_UP / GPIO.PULL_DOWNGPIO_PULL_UP / GPIO_PULL_DOWNInternal bias
GPIO.OPEN_DRAIN / GPIO.OPEN_SOURCEGPIO_OPEN_DRAIN / GPIO_OPEN_SOURCEOutput topology (bus pins like I2C SDA)
GPIO.DISCONNECTEDGPIO_DISCONNECTEDBoth buffers off

set set(value: boolean): void

ParameterTypeUnits / valuesDescription
valuebooleanlogical leveltrue = "on". For board pins like LED, the polarity is handled for you.

Drives the pin.

get get(): boolean → logical level

Reads the logical level. Safe in a bare expression:

expression-safe
if (button.get()) { /* ... */ }

toggle toggle(): void

Toggles the pin in a single step — never a read-then-write race.

onInterrupt / offInterrupt onInterrupt(token, handler): void

ParameterTypeUnits / valuesDescription
tokenGPIO.INT_*edge/level tokene.g. GPIO.INT_EDGE_FALLING.
handler() => void—Called when the interrupt fires.
interrupts.ts
button.onInterrupt(GPIO.INT_EDGE_FALLING, () => { pressed = true; });
// ...
button.offInterrupt();

All methods

MethodSignaturePurpose
setset(value: boolean): voidDrive the pin (logical level)
getget(): booleanRead the logical level
toggletoggle(): voidToggle the pin
onInterruptonInterrupt(token, handler): voidAttach edge/level interrupt
offInterruptoffInterrupt(): voidDetach the interrupt
class

Pin

Typed pin object from the generated board module, re-exported through @typecad/hal. Pass it to GPIO in place of a number — the board definition already knows its capabilities, so a wrong-pin assignment is caught at compile time, not on the board.

Example first

pin-arguments.ts
import { LED, BUTTON, PB5, GPIO } from '@typecad/hal';

new GPIO(LED, GPIO.OUTPUT);     // board export
new GPIO(5, GPIO.OUTPUT);       // raw number
new GPIO(PB5, GPIO.OUTPUT);     // Pin instance

All three forms are equivalent for a plain GPIO; the board export is preferred because it carries polarity and binding information.

© 2026 typeCAD — code-defined electronics github