#gpio #linux

gpiochip

Package to use Linux /dev/gpiochip devices

2 releases

Uses old Rust 2015

0.1.1 Jun 27, 2018
0.1.0 Jun 12, 2018

#1432 in Hardware support

Download history 34/week @ 2023-10-30 13/week @ 2023-11-06 9/week @ 2023-11-13 25/week @ 2023-11-20 17/week @ 2023-11-27 9/week @ 2023-12-04 9/week @ 2023-12-11 10/week @ 2023-12-18 11/week @ 2023-12-25 8/week @ 2024-01-01 8/week @ 2024-01-08 17/week @ 2024-01-15 34/week @ 2024-01-22 11/week @ 2024-01-29 17/week @ 2024-02-05 25/week @ 2024-02-12

88 downloads per month

ISC license

22KB
362 lines

Rust gpiochip

The Rust gpiochip seeks to provide full access to the Linux gpiochip device in Rust without the need to wrap any C code or directly make low-level system calls.

Examples

extern crate gpiochip as gpio;

/// Print information about first gpiochip
fn main() {
    let chip = gpio::GpioChip::new("/dev/gpiochip0").unwrap();

    println!("GPIOChip 0:");
    println!(" Name:  {:?}", chip.name);
    println!(" Label: {:?}", chip.label);
    println!(" Lines: {:?}", chip.lines);
    println!("");

    for i in 0..chip.lines {
        let info = chip.info(i).unwrap();

        println!(" GPIO {:?}: {:?}", info.gpio, info.name);
        println!("     Consumer: {:?}", info.consumer);
        println!("     Flags: {:?}", info.flags);
    }
}
extern crate gpiochip as gpio;

/// Simple get/set example
fn main() {
    let chip = gpio::GpioChip::new("/dev/gpiochip0").unwrap();
    let gpio_a = chip.request("gpioA", gpio::RequestFlags::INPUT, 0, 0).unwrap();
    let gpio_b = chip.request("gpioA", gpio::RequestFlags::OUTPUT | gpio::RequestFlags::ACTIVE_LOW, 1, 0).unwrap();

    let val = gpio_a.get().unwrap();
    gpio_b.set(val).unwrap();
}
extern crate gpiochip as gpio;

/// GPIO events
fn main() {
    let chip = gpio::GpioChip::new("/dev/gpiochip0").unwrap();

    let gpio_a = chip.request_event("gpioA", 0, gpio::RequestFlags::INPUT, gpio::EventRequestFlags::BOTH_EDGES).unwrap();
    let gpio_b = chip.request_event("gpioB", 1, gpio::RequestFlags::INPUT, gpio::EventRequestFlags::BOTH_EDGES).unwrap();

    let bitmap = gpio::wait_for_event(&[&gpio_a, &gpio_b], 1000).unwrap();

    if bitmap & 0b01 == 0b01 {
        let event = gpio_a.read().unwrap();
        println!("gpioA: event @ {:?} - {:?}", event.timestamp, event.id);
    }

    if bitmap & 0b10 == 0b10 {
        let event = gpio_b.read().unwrap();
        println!("gpioB: event @ {:?} - {:?}", event.timestamp, event.id);
    }
}

License

© 2018 Sebastian Reichel

ISC License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Dependencies

~2MB
~40K SLoC