#linux-gpio #tokio #async #libgpiod

tokio-gpiod

Linux GPIO character device interfacing with tokio

3 releases

0.2.3 Jan 21, 2023
0.2.2 Oct 15, 2022
0.2.1 Sep 15, 2022
0.2.0 Jul 31, 2022

#776 in Unix APIs

Download history 615/week @ 2023-11-23 464/week @ 2023-11-30 513/week @ 2023-12-07 428/week @ 2023-12-14 64/week @ 2023-12-21 299/week @ 2023-12-28 446/week @ 2024-01-04 293/week @ 2024-01-11 420/week @ 2024-01-18 495/week @ 2024-01-25 426/week @ 2024-02-01 929/week @ 2024-02-08 809/week @ 2024-02-15 1297/week @ 2024-02-22 694/week @ 2024-02-29 276/week @ 2024-03-07

3,152 downloads per month
Used in boardswarm

MIT license

82KB
2K SLoC

libgpiod in Rust for tokio

github crate docs MIT CI

Rust crate for interfacing with Linux GPIO character devices. This crate supports tokio async runtime.

It provides an interface to the Linux GPIO using the chardev module. This interface involves calling ioctl funcions which are unsafe and require some unintuitive variable mapping. To ease this process, this crate provides a [Chip] struct which encapsulates the interface in safe Rust functions. The functionality provided here is highly inspired by libgpiod.

Since all functionality is dependent on Linux function calls, this crate only compiles for Linux systems.

ABI compatibility

Both v1 (>= 4.0) and v2 (>= v5.10) ABI currently supported but edge detection implemented for v2 only. Sysfs-based API (< 4.0) does not supported.

Crates

Usage examples

Input values:

use tokio_gpiod::{Chip, Options};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let chip = Chip::new("gpiochip0").await?; // open chip

    let opts = Options::input([27, 3, 11]) // configure lines offsets
        .consumer("my-inputs"); // optionally set consumer string

    let inputs = chip.request_lines(opts).await?;

    let values = inputs.get_values([false; 3]).await?;

    println!("values: {:?}", values);

    Ok(())
}

Output values:

use tokio_gpiod::{Chip, Options};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let chip = Chip::new("gpiochip0").await?; // open chip

    let opts = Options::output([9, 21]) // configure lines offsets
        .values([false, true]) // optionally set initial values
        .consumer("my-outputs"); // optionally set consumer string

    let outputs = chip.request_lines(opts).await?;

    outputs.set_values([true, false]).await?;

    Ok(())
}

Monitor values:

use tokio_gpiod::{Chip, Options, EdgeDetect};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    let chip = Chip::new("gpiochip0").await?; // open chip

    let opts = Options::input([4, 7]) // configure lines offsets
        .edge(EdgeDetect::Both) // configure edges to detect
        .consumer("my-inputs"); // optionally set consumer string

    let mut inputs = chip.request_lines(opts).await?;

    loop {
        let event = inputs.read_event().await?;

        println!("event: {:?}", event);
    }

    Ok(())
}

Dependencies

~4–15MB
~151K SLoC