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.
The follow example shows the low-level API. This API maps directly to the functionality provided by the sysfs GPIO interface.
```rust
extern crate sysfs_gpio;
use sysfsgpio::core::{Direction, Pin}; use std::time::Duration; use std::oldio::Timer; use std::io;
// export a GPIO for use. This will not fail // if already exported fn blinkmyled(led : u64, durationms : i64, periodms : i64) -> io::Result<()> { let myled = Pin::new(led); try!(myled.export()); try!(myled.setdirection(Direction::Low)); let mut tmr = match Timer::new() { Ok(tmr) => tmr, Err() => panic!("Could not create timer!"), }; let iterations = durationms / periodms / 2; for _ in 0..iterations { try!(myled.setvalue(0)); tmr.sleep(Duration::milliseconds(periodms)); // ms try!(myled.setvalue(1)); tmr.sleep(Duration::milliseconds(periodms)); // ms } try!(myled.setvalue(0)); // end with led off try!(myled.unexport()); return Ok(()); }
fn main() { match blinkmyled(66, 5000, 200) { Ok(()) => println!("Blinking Complete!"), Err(err) => println!("I have a blinking problem! {:?}", err), } }
```
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"
Copyright (c) 2015, Paul Osborne ospbau@gmail.com
Licensed under the Apache License, Version 2.0