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.
To use sysfs_gpio
, first add this to your Cargo.toml
:
toml
[dependencies]
sysfs-gpio = "*"
Then, add this to your crate root:
rust
extern crate sysfs_gpio;
Blinking an LED:
```rust extern crate sysfs_gpio;
use sysfsgpio::{Direction, Pin}; use std::thread::sleepms;
fn main() { let myled = Pin::new(127); // number depends on chip, etc. myled.withexported(|| { loop { myled.setvalue(0).unwrap() sleepms(200); myled.setvalue(1).unwrap(); sleep_ms(200); } }).unwrap(); } ```
More Examples:
The following features are planned for the library:
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 following basic instructions should work for the raspberry pi or beaglebone black:
sudo apt-get install g++-arm-linux-gnueabihf
.cargo build --target=arm-unknown-linux-gnueabi
.The following snippet added to my ~/.cargo/config worked for me:
[target.arm-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
Cargo supports running examples but does not currently support building these examples. In order to cross-compile the examples using cargo, this is what things currently look like:
``
$ cargo run --target=arm-unknown-linux-gnueabihf --example blinky
Compiling sysfs_gpio v0.1.0
(file:///home/posborne/Projects/rust-playground/rust-sysfs-gpio)
Running
target/arm-unknown-linux-gnueabihf/debug/examples/blinky`
target/arm-unknown-linux-gnueabihf/debug/examples/blinky: 1:
target/arm-unknown-linux-gnueabihf/debug/examples/blinky:
Syntax error: word unexpected (expecting ")")
An unknown error occurred
To learn more, run the command again with --verbose. $ scp target/arm-unknown-linux-gnueabihf/debug/examples/blinky ... ```
Basically, the run part of the process will fail, but the
cross-compiled example is still available and can be copied over to be
run on your target. Hopefully something like cargo build --example
<ex>
is added to cargo in the future.
``` Copyright (c) 2015, Paul Osborne ospbau@gmail.com
Licensed under the Apache License, Version 2.0