GPIO & Digital I/O
Digital pins use one small class: new GPIO(pin, flags) configures the pin, and set, get, and toggle control it.
GPIO
One digital pin. Construct it with the pin and its flags — everything is set at construction.
Example first
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.
| Token | Zephyr equivalent | Meaning |
|---|---|---|
| GPIO.INPUT / GPIO.OUTPUT | GPIO_INPUT / GPIO_OUTPUT | Direction |
| GPIO.OUTPUT_INIT_LOW / GPIO.OUTPUT_INIT_HIGH | GPIO_OUTPUT_INIT_LOW / GPIO_OUTPUT_INIT_HIGH | Output's initial level, set atomically — no configure-then-write glitch |
| GPIO.PULL_UP / GPIO.PULL_DOWN | GPIO_PULL_UP / GPIO_PULL_DOWN | Internal bias |
| GPIO.OPEN_DRAIN / GPIO.OPEN_SOURCE | GPIO_OPEN_DRAIN / GPIO_OPEN_SOURCE | Output topology (bus pins like I2C SDA) |
| GPIO.DISCONNECTED | GPIO_DISCONNECTED | Both buffers off |
set set(value: boolean): void
| Parameter | Type | Units / values | Description |
|---|---|---|---|
| value | boolean | logical level | true = "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:
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
| Parameter | Type | Units / values | Description |
|---|---|---|---|
| token | GPIO.INT_* | edge/level token | e.g. GPIO.INT_EDGE_FALLING. |
| handler | () => void | — | Called when the interrupt fires. |
button.onInterrupt(GPIO.INT_EDGE_FALLING, () => { pressed = true; });
// ...
button.offInterrupt();All methods
| Method | Signature | Purpose |
|---|---|---|
| set | set(value: boolean): void | Drive the pin (logical level) |
| get | get(): boolean | Read the logical level |
| toggle | toggle(): void | Toggle the pin |
| onInterrupt | onInterrupt(token, handler): void | Attach edge/level interrupt |
| offInterrupt | offInterrupt(): void | Detach the interrupt |
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
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 instanceAll three forms are equivalent for a plain GPIO; the board export is preferred because it carries polarity and binding information.