gpiocdev 0.1.0

A library to access GPIO lines on Linux using the GPIO character device
Documentation

gpiocdev

github crate MIT

A Rust library for accessing GPIO lines on Linux platforms using the GPIO character device.

This is the equivalent of libgpiod and my Go gpiod library, but in pure Rust.

Example Usage

Getting a line value:

    // request the line
    let req = Request::builder()
        .on_chip("/dev/gpiochip0")
        .with_line(23)
        .as_input()
        .request()?;
    // get the value
    let value = req.value(23)?;

Setting a line:

    // request the line and set its value
    let req = Request::builder()
        .on_chip("/dev/gpiochip0")
        .with_line(22)
        .as_output(Value.Active)
        .request()?;

    // do something...

    // change value later
    req.set_value(22, Value.Inactive)

Waiting for events on a line:

    // request the line
    let req = Request::builder()
        .on_chip("/dev/gpiochip0")
        .with_line(23)
        .with_edge_detection(EdgeDetection::BothEdges)
        .request()?;

    // wait for line edge events
    for event in req.edge_events() {
        println!("{:?}", event?);
    }

Multiple lines may be selected in a single request, and then be operated on as a unit.

Getting multiple lines:

    // request multiple input lines
    let req = Request::builder()
        .on_chip("/dev/gpiochip0")
        .with_lines(&[18,23])
        .as_input()
        .request()?;
    // get multiple line values at once
    let mut values = Values::default();
    req.values(&mut values)?;

Setting multiple lines:

    // request multiple output lines
    let req = Request::builder()
        .on_chip("/dev/gpiochip0")
        .with_lines(&[17,22])
        .as_output(Value::Active)
        .request()?;
    // set multiple line values at once
    let mut values = Values::default();
    values.set(17, Value::Inactive);
    values.set(12, Value::Active);
    req.set_values(&values)?;

All line attributes available via the kernel GPIO interface, such as pull-ups and debounce etc, can also be set:

    // request the line
    let req = Request::builder()
        .on_chip("/dev/gpiochip0")
        .with_consumer("myapp")
        .with_line(23)
        .as_input()
        .as_active_low()
        .with_bias(Bias::PullUp)
        .request()?;
    // get the value
    let value = req.value(23)?;

A good starting point to learn more is the gpiocdev::request::Request.