1 unstable release
| new 0.1.0 | Feb 10, 2026 |
|---|
#412 in Embedded development
67KB
1.5K
SLoC
soul-gpio
GPIO tools for soul-core — digital I/O, PWM, ADC, I2C, SPI.
WASM-first, cross-platform. All hardware access goes through the VirtualGpio trait, enabling full operation on real dev boards, in WebAssembly, and in simulated environments.
Install
[dependencies]
soul-gpio = "0.1"
For WASM targets:
[dependencies]
soul-gpio = { version = "0.1", default-features = false, features = ["wasm"] }
For Linux GPIO (Raspberry Pi, etc.):
[dependencies]
soul-gpio = { version = "0.1", features = ["linux-gpio"] }
Quick Start
use std::sync::Arc;
use soul_gpio::gpio::MemoryGpio;
use soul_gpio::presets;
// Simulated GPIO — works everywhere including WASM
let gpio = Arc::new(MemoryGpio::new());
let registry = presets::all_tools(gpio);
assert_eq!(registry.len(), 9);
Tools
| Tool | Description |
|---|---|
| gpio_mode | Set pin mode: input, output, input_pull_up, input_pull_down, pwm, adc |
| gpio_read | Read digital state (HIGH/LOW) from a pin |
| gpio_write | Write digital state (high/low) to an output pin |
| gpio_toggle | Toggle an output pin between HIGH and LOW |
| list_pins | List all configured pins with mode and state |
| pwm_write | Set PWM duty cycle (0-100%) and frequency on a pin |
| adc_read | Read analog voltage from a pin |
| i2c | Read/write data over I2C bus |
| spi | Full-duplex SPI data transfer |
Every tool implements soul_core::tool::Tool and plugs directly into soul-core's ToolRegistry and AgentLoop.
Presets
use std::sync::Arc;
use soul_gpio::gpio::MemoryGpio;
let gpio = Arc::new(MemoryGpio::new());
// Digital I/O: gpio_mode, gpio_read, gpio_write, gpio_toggle, list_pins
let digital = soul_gpio::gpio_tools(gpio.clone());
// Analog: pwm_write, adc_read
let analog = soul_gpio::analog_tools(gpio.clone());
// Bus protocols: i2c, spi
let bus = soul_gpio::bus_tools(gpio.clone());
// Everything: all 9 tools
let all = soul_gpio::all_tools(gpio);
VirtualGpio Trait
The core abstraction — all tools delegate to this:
pub trait VirtualGpio: Send + Sync {
fn set_mode(&self, pin: u32, mode: PinMode) -> ...;
fn digital_read(&self, pin: u32) -> ...;
fn digital_write(&self, pin: u32, state: PinState) -> ...;
fn toggle(&self, pin: u32) -> ...;
fn pwm_write(&self, pin: u32, duty: f32, frequency_hz: u32) -> ...;
fn adc_read(&self, pin: u32) -> ...;
fn i2c_write(&self, bus: u32, addr: u8, data: &[u8]) -> ...;
fn i2c_read(&self, bus: u32, addr: u8, len: usize) -> ...;
fn spi_transfer(&self, bus: u32, write_data: &[u8]) -> ...;
fn list_pins(&self) -> ...;
}
Implementations
| Environment | Implementation | Description |
|---|---|---|
| WASM / Browser | MemoryGpio |
In-memory simulated GPIO (always available) |
| Tests | MemoryGpio |
Pre-set ADC values, I2C/SPI responses for testing |
| Linux | LinuxGpio |
Real GPIO via gpiocdev (behind linux-gpio feature) |
MemoryGpio for Testing
use soul_gpio::gpio::MemoryGpio;
let gpio = MemoryGpio::new();
// Pre-set simulated sensor readings
gpio.set_adc_value(0, 2.5); // 2.5V on pin 0
gpio.set_i2c_response(1, 0x48, vec![0xAB, 0xCD]);
gpio.set_spi_response(0, vec![0x01, 0x02]);
// Assert pin states after tool execution
assert_eq!(gpio.get_pin_state(13), Some(PinState::High));
Architecture
soul-gpio
├── gpio/
│ ├── mod.rs VirtualGpio trait, PinMode, PinState, Edge types
│ ├── memory.rs MemoryGpio — simulated GPIO for WASM/testing
│ └── linux.rs LinuxGpio — real Linux GPIO via gpiocdev
├── tools/
│ ├── gpio_mode.rs Set pin mode (input/output/pullup/pwm/adc)
│ ├── gpio_read.rs Read digital pin state
│ ├── gpio_write.rs Write digital pin state
│ ├── gpio_toggle.rs Toggle output pin
│ ├── list_pins.rs List all configured pins
│ ├── pwm.rs PWM duty cycle and frequency
│ ├── adc.rs Analog voltage reading
│ ├── i2c.rs I2C bus read/write
│ └── spi.rs SPI bus transfer
├── presets.rs gpio_tools, analog_tools, bus_tools, all_tools
└── lib.rs Public API and re-exports
Features
| Feature | Default | Description |
|---|---|---|
native |
yes | Enables soul-core native runtime |
wasm |
no | Enables WASM-compatible dependencies |
linux-gpio |
no | Enables real Linux GPIO via gpiocdev |
License
MIT
Dependencies
~16–33MB
~391K SLoC