Skip to content

gpio

stable

Simulate GPIO pin state for embedded hardware prototyping, supporting digital read/write, PWM duty cycle, pull resistors, and pin modes.

use plugin gpio::{GpioSimulator, set_mode, digital_write, …}
13 functions Systems
/ filter jk navigate Esc clear
Functions (13)
  1. GpioSimulator Creates a new GPIO simulator handle with no pins configured.
  2. set_mode Configures a pin's mode.
  3. digital_write Sets an output pin to high (true) or low (false).
  4. digital_read Reads the current digital state of a pin.
  5. get_mode Returns the current mode of a pin as a string, or "unset" if the pin has not been configured.
  6. get_all_pins Returns a sorted table of all configured pins, each entry containing mode and value.
  7. reset Clears all pin configuration from the simulator, returning it to a clean state.
  8. set_pull Configures a pull resistor on an input pin.
  9. pwm_write Writes a PWM duty cycle to a PWM-mode pin.
  10. pin_info Returns a table with pin, mode, value, and active fields for the given pin number.
  11. pin_count Returns how many pins have been configured on this simulator.
  12. pin_valid Standalone function.
  13. mode_names Standalone function.

Creates a new GPIO simulator handle with no pins configured.

Creates a new GPIO simulator handle with no pins configured. All pin state is stored in memory for testing embedded logic without real hardware.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
print("pins: {gpio.pin_count()}")

Configures a pin's mode.

Configures a pin's mode. Valid modes are "input", "output", "pwm", "analog", "i2c", "spi", "uart".

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(4, "output")
gpio.set_mode(17, "input")

Sets an output pin to high (true) or low (false).

Sets an output pin to high (true) or low (false). The pin must be in "output" mode or an error is returned.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(4, "output")
gpio.digital_write(4, true)
print(gpio.digital_read(4))

Reads the current digital state of a pin.

Reads the current digital state of a pin. Returns false if the pin has not been configured.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(17, "input")
let state = gpio.digital_read(17)
print("pin 17: {state}")

Returns the current mode of a pin as a string, or "unset" if the pin has not been configured.

Returns the current mode of a pin as a string, or "unset" if the pin has not been configured.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(4, "output")
print(gpio.get_mode(4))

Returns a sorted table of all configured pins, each entry containing mode and value.

Returns a sorted table of all configured pins, each entry containing mode and value.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(1, "output")
gpio.set_mode(2, "input")
let pins = gpio.get_all_pins()
print(pins[1]["mode"])

Clears all pin configuration from the simulator, returning it to a clean state.

Clears all pin configuration from the simulator, returning it to a clean state.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(4, "output")
gpio.reset()
print("pins: {gpio.pin_count()}")

Configures a pull resistor on an input pin.

Configures a pull resistor on an input pin. Valid values are "up", "down", "none". Pull-up sets the pin value to true; pull-down sets it to false. Returns the normalized pull string.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(17, "input")
gpio.set_pull(17, "up")
print(gpio.digital_read(17))

Writes a PWM duty cycle to a PWM-mode pin.

Writes a PWM duty cycle to a PWM-mode pin. duty must be between 0.0 and 1.0. The simulated digital state is true when duty > 0.5.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(18, "pwm")
gpio.pwm_write(18, 0.75)
print(gpio.digital_read(18))

Returns a table with pin, mode, value, and active fields for the given pin number.

Returns a table with pin, mode, value, and active fields for the given pin number. If the pin is not configured, active is false.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(4, "output")
gpio.digital_write(4, true)
let info = gpio.pin_info(4)
print("{info["mode"]} = {info["value"]}")

Returns how many pins have been configured on this simulator.

Returns how many pins have been configured on this simulator.

use plugin gpio::{GpioSimulator}

let gpio = GpioSimulator()
gpio.set_mode(1, "output")
gpio.set_mode(2, "input")
print(gpio.pin_count())

Standalone function.

Standalone function. Returns true if the pin number is in the valid range 0–53, covering Raspberry Pi, Arduino Mega, and similar boards.

use plugin gpio::{pin_valid}

print(pin_valid(4))
print(pin_valid(100))

Standalone function.

Standalone function. Returns a list of all valid mode strings: input, output, pwm, analog, i2c, spi, uart.

use plugin gpio::{mode_names}

let modes = mode_names()
print(modes[1])
enespt-br