rust-sysfs-gpio is a rust library/crate providing access to the Linux sysfs GPIO interface (https://www.kernel.org/doc/Documentation). It seeks to provide an API that is safe, convenient, and efficient.
Many devices such as the Raspberry Pi or Beaglebone Black provide userspace access to a number of GPIO peripherals. The standard kernel API for providing access to these GPIOs is via sysfs.
You might want to also check out the gpio-utils Project for a convenient way to associate names with pins and export them as part of system boot. That project uses this library.
To use sysfs_gpio
, first add this to your Cargo.toml
:
toml
[dependencies]
sysfs_gpio = "0.4"
Then, add this to your crate root:
rust
extern crate sysfs_gpio;
Blinking an LED:
```rust extern crate sysfs_gpio;
use sysfs_gpio::{Direction, Pin}; use std::thread::sleep; use std::time::Duration;
fn main() { let myled = Pin::new(127); // number depends on chip, etc. myled.withexported(|| { loop { myled.setvalue(0).unwrap(); sleep(Duration::frommillis(200)); myled.setvalue(1).unwrap(); sleep(Duration::from_millis(200)); } }).unwrap(); } ```
More Examples:
The following features are planned for the library:
mio
or tokio-core
(requires
enabling the mio-evented
or tokio
crate features, respectively)Most likely, the machine you are running on is not your development machine (although it could be). In those cases, you will need to cross-compile. The rust-cross guide provides excellent, detailed instructions for cross-compiling.
Cross-compiling can be done by specifying an appropriate target. You can then move that to your device by whatever means and run it.
$ cargo build --target=arm-unknown-linux-gnueabihf --example blinky
$ scp target/arm-unknown-linux-gnueabihf/debug/examples/blinky ...
``` Copyright (c) 2015, Paul Osborne ospbau@gmail.com
Licensed under the Apache License, Version 2.0