3 unstable releases

0.2.1 May 6, 2021
0.2.0 Feb 27, 2019
0.1.0 Mar 12, 2018

#1009 in Hardware support

Download history 80/week @ 2023-11-20 41/week @ 2023-11-27 33/week @ 2023-12-04 51/week @ 2023-12-11 74/week @ 2023-12-18 35/week @ 2023-12-25 27/week @ 2024-01-01 55/week @ 2024-01-08 60/week @ 2024-01-15 41/week @ 2024-01-22 25/week @ 2024-01-29 38/week @ 2024-02-05 56/week @ 2024-02-12 86/week @ 2024-02-19 123/week @ 2024-02-26 101/week @ 2024-03-04

377 downloads per month

MIT/Apache

45KB
801 lines

rust_gpiozero

Build Status

A simple interface to GPIO devices with Raspberry Pi.

This library is based on GPIOZero library.

The idea is to get started with physical computing using Rust with little coding by hiding the underlying complexity.

The library uses BCM Pin numbering

Example : Blinking an LED


use rust_gpiozero::*;

fn main() {
    // Create a new LED attached to Pin 17
    let mut led = LED::new(17);

    // on_time = 2 secs, off_time=3 secs
    led.blink(2.0,3.0);

    // prevent program from exiting immediately
    led.wait();
}

Example : Wait for a Button Press


use rust_gpiozero::*;

fn main() {
    // Create a button which is attached to Pin 17
    let mut button = Button::new(17);
    button.wait_for_press(None);
    println!("button pressed");
}

Compare this to using the crate sysfs_gpio to blink an LED on the Raspberry Pi :


extern crate sysfs_gpio;

use sysfs_gpio::{Direction, Pin};
use std::thread::sleep;
use std::time::Duration;

fn main() {
    let my_led = Pin::new(127); // number depends on chip, etc.
    my_led.with_exported(|| {
        loop {
            my_led.set_value(0).unwrap();
            sleep(Duration::from_millis(200));
            my_led.set_value(1).unwrap();
            sleep(Duration::from_millis(200));
        }
    }).unwrap();
}

Install/Use

To use rust_gpiozero, first add this to your Cargo.toml:

[dependencies]
 rust_gpiozero = "0.2.0"

Compiling your project on a Raspberry Pi directly can take significant time depending on the model. Ideally, you would cross compile your project then run it on the Raspberry Pi.

More information

Features

The following features are planned :

  • Support for embedded-hal
  • Support for common devices such as Accelerometer, Temperature sensors, etc

Changelog

CHANGELOG.md

License

Licensed under either of

at your option.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Thanks for your interest in rust_gpiozero. I am a newbie rustacean and just started using the language! I am using this project to learn more about Rust. Feel free to give feedback or send PRs. Your experiences and feedback will also benefit others who use this library.

Credits

This library would not be possible without the great work of the maintainers of GPIOZero and rppal

Dependencies

~380KB