GPIO & Digital I/O

Digital pins are one thin class: new GPIO(pin, flags) configures the pin, and each method — set, get, toggle — lowers to exactly one Zephyr call. The flags are Zephyr’s own GPIO_* tokens verbatim. There is no pinMode() and no Wiring-style pin renumbering.


Basic Usage

import { GPIO } from '@typecad/hal';
import { LED, BUTTON } from '@typecad/board';

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 Carries the Configuration

Combine the flags with | — they are the flags gpio_pin_configure_dt receives:

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

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

Output Operations

set(value) drives the pin. On a pin with a board devicetree binding (LED, BUTTON) the level is logical: set(true) means “LED on”, and the node’s GPIO_ACTIVE_LOW polarity is honored for you.

toggle() is the driver’s atomic toggle — not a read-then-write.

Input Operations

get() reads the logical level. It is safe in a bare expression:

if (button.get()) { /* ... */ }
⚙️ Advanced details — the fused configure

The one-time configure is fused into the read’s lowering as a single statement-expression, so the pin is correctly configured even at a call site where a leading setup statement would be dropped (an if-condition, a comparison operand).

Interrupts

Attach with a GPIO.INT_* token and a handler; detach with offInterrupt():

button.onInterrupt(GPIO.INT_EDGE_FALLING, () => { pressed = true; });
// …
button.offInterrupt();

Handlers run in interrupt context and the ISR-safety rules apply — see Hardware Events.


API Reference

MemberDescription
new GPIO(pin, flags)Configure at construction; flags are GPIO.* tokens combined with \|.
set(value)Drive the pin (logical level on devicetree-bound pins).
get()Read the pin (logical level).
toggle()Atomic toggle.
onInterrupt(intFlags, fn)Attach an interrupt with a GPIO.INT_* token.
offInterrupt()Detach the interrupt.