atomvm_gleam/gpio

Typed Gleam wrappers for AtomVM GPIO.

ESP32 source: libs/avm_esp32/src/gpio.erl. RP2 source: libs/avm_rp2/src/gpio.erl. Edoc fallback: Module gpio.

On Pico-W, the onboard LED is the wireless-bank pin WlPin(0) (Erlang {wl, 0}). Prefer digital_write(wl(0), …) for that LED; the forthcoming pico module CYW43 helpers are an alternate API for the same hardware.

Types

Pin direction. Zero-arity variants encode as Erlang atoms matching gpio:set_direction/3 (input, output, output_od).

See direction().

pub type Direction {
  Input
  Output
  OutputOd
}

Constructors

  • Input
  • Output
  • OutputOd

Errors from the AtomVM gpio driver.

Known reason atoms are Gleam constructors (so {error, not_supported} is Error(NotSupported)). Bare AtomVM error becomes Failed. Anything else lands in Other as a string because AtomVM types reasons as open atom().

See Module gpio.

pub type Error {
  Failed
  NotSupported
  Badarg
  Timeout
  Other(String)
}

Constructors

  • Failed
  • NotSupported
  • Badarg
  • Timeout
  • Other(String)

Opaque handle for the AtomVM GPIO driver port (gpio:open/0 / gpio:start/0).

See Module gpio.

pub type Gpio

RP2 GPIO function select (gpio:set_function/2). Encodes as Erlang atoms matching Pico SDK gpio_function_t: spi, uart, i2c, pwm, sio, pio0, pio1.

See gpio:set_function/2.

pub type GpioFunction {
  Spi
  Uart
  I2c
  Pwm
  Sio
  Pio0
  Pio1
}

Constructors

  • Spi
  • Uart
  • I2c
  • Pwm
  • Sio
  • Pio0
  • Pio1

Digital level. Encodes through FFI as Erlang high / low (distinct constructors from Trigger, which also uses those atoms).

See level().

pub type Level {
  PinHigh
  PinLow
}

Constructors

  • PinHigh
  • PinLow

GPIO pin. PinNum is a plain GPIO number (ESP32 and RP2). WlPin is a Pico-W wireless-bank pin and encodes as Erlang {wl, N} in the FFI.

Use pin/1 and wl/1 helpers at call sites. On Pico-W the onboard LED is wl(0) / WlPin(0) ({wl, 0}); pico CYW43 helpers are an alternate API.

See RP2 pin().

pub type Pin {
  PinNum(Int)
  WlPin(Int)
}

Constructors

  • PinNum(Int)
  • WlPin(Int)

Internal resistor pull mode.

See pull().

pub type Pull {
  Up
  Down
  UpDown
  Floating
}

Constructors

  • Up
  • Down
  • UpDown
  • Floating

Interrupt trigger. Encodes as Erlang atoms matching gpio:set_int/3.

See trigger().

pub type Trigger {
  None
  Rising
  Falling
  Both
  Low
  High
}

Constructors

  • None
  • Rising
  • Falling
  • Both
  • Low
  • High

Values

pub fn attach_interrupt(
  pin: Pin,
  trigger: Trigger,
) -> Result(Nil, Error)

Convenience for set_int/3 using only pin and trigger. Prefer set_int when arming more than one pin.

See gpio:attach_interrupt/2.

pub fn close(gpio: Gpio) -> Result(Nil, Error)

Stop the GPIO interrupt port and free its resources.

See gpio:close/1.

pub fn deep_sleep_hold_dis() -> Result(Nil, Error)

Disable all gpio pad hold functions during deep sleep (ESP32).

See gpio:deep_sleep_hold_dis/0.

pub fn deep_sleep_hold_en() -> Result(Nil, Error)

Enable all hold functions to continue in deep sleep (ESP32).

See gpio:deep_sleep_hold_en/0.

pub fn deinit(pin: Pin) -> Result(Nil, Error)

Reset a pin back to the NULL function (RP2040).

See gpio:deinit/1.

pub fn detach_interrupt(pin: Pin) -> Result(Nil, Error)

Convenience for remove_int/2 using only the pin.

See gpio:detach_interrupt/1.

pub fn digital_read(pin: Pin) -> Result(Level, Error)

Read pin level via the NIF-style API.

On Pico-W, VBUS detect is readable as wl(2) without prior mode/pull setup.

See gpio:digital_read/1.

pub fn digital_write(
  pin: Pin,
  level: Level,
) -> Result(Nil, Error)

Write pin level via the NIF-style API.

On Pico-W, the onboard LED is wl(0) / {wl, 0} and does not require set_pin_mode or set_pin_pull before use. The pico module CYW43 helpers are an alternate API for the same LED.

See gpio:digital_write/2.

pub fn error_to_string(error: Error) -> String

Format an Error for logging.

pub fn hold_dis(pin: Pin) -> Result(Nil, Error)

Release a pin from a hold state (ESP32).

See gpio:hold_dis/1.

pub fn hold_en(pin: Pin) -> Result(Nil, Error)

Hold the state of a pin (ESP32).

See gpio:hold_en/1.

pub fn init(pin: Pin) -> Result(Nil, Error)

Initialize a pin for GPIO use (required on RP2040; some ESP32 pins).

See gpio:init/1.

pub fn open() -> Result(Gpio, Error)

Start the GPIO driver port. Fails if it is already running.

See gpio:open/0.

pub fn pin(n: Int) -> Pin

Wrap a numeric GPIO pin as PinNum.

pub fn read(gpio: Gpio, pin: Pin) -> Result(Level, Error)

Read the digital state of a pin via the port API.

See gpio:read/2.

pub fn remove_int(gpio: Gpio, pin: Pin) -> Result(Nil, Error)

Remove a GPIO interrupt.

See gpio:remove_int/2.

pub fn set_direction(
  gpio: Gpio,
  pin: Pin,
  direction: Direction,
) -> Result(Nil, Error)

Set the operational mode of a pin (input, output, or output_od).

See gpio:set_direction/3.

pub fn set_function(
  pin: Int,
  function: GpioFunction,
) -> Result(Nil, Error)

Select the function for a GPIO pin (RP2 / Pico SDK gpio_set_function). Takes a numeric pin only; wireless-bank pins are not supported by upstream.

See gpio:set_function/2.

pub fn set_int(
  gpio: Gpio,
  pin: Pin,
  trigger: Trigger,
) -> Result(Nil, Error)

Set a GPIO interrupt. Delivers {gpio_interrupt, Pin} to the caller.

See gpio:set_int/3.

pub fn set_int_to(
  gpio: Gpio,
  pin: Pin,
  trigger: Trigger,
  pid: process.Pid,
) -> Result(Nil, Error)

Set a GPIO interrupt, delivering {gpio_interrupt, Pin} to pid.

See gpio:set_int/4.

pub fn set_level(
  gpio: Gpio,
  pin: Pin,
  level: Level,
) -> Result(Nil, Error)

Set GPIO digital output level via the port API.

See gpio:set_level/3.

pub fn set_pin_mode(
  pin: Pin,
  direction: Direction,
) -> Result(Nil, Error)

Set pin mode without a port handle (NIF-style API).

See gpio:set_pin_mode/2.

pub fn set_pin_pull(pin: Pin, pull: Pull) -> Result(Nil, Error)

Set the internal resistor of a pin (not used on STM32).

See gpio:set_pin_pull/2.

pub fn start() -> Result(Gpio, Error)

Start the GPIO driver port, or return the existing one.

See gpio:start/0.

pub fn stop() -> Result(Nil, Error)

Stop the GPIO interrupt port (no handle required).

See gpio:stop/0.

pub fn wakeup_enable(
  pin: Pin,
  level: Level,
) -> Result(Nil, Error)

Configure a GPIO as a light-sleep wakeup pin (ESP32).

See gpio:wakeup_enable/2.

pub fn wl(n: Int) -> Pin

Wrap a Pico-W wireless-bank pin as WlPin (Erlang {wl, N}).

Example: Pico-W LED is wl(0).

✨ Search Document