Skip to content

Commit b776b45

Browse files
Switch to rppal (#5)
* Use rppal for gpio access - Device - GpioDevice - OutputDevice - Add servo example - Softpwm
1 parent 1b105ce commit b776b45

File tree

12 files changed

+902
-515
lines changed

12 files changed

+902
-515
lines changed

.travis.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
branches:
22
only:
3-
- master
4-
3+
- master
4+
55
language: rust
66
sudo: required
77
cache: cargo
8+
services: docker
89
rust:
910
- stable
1011
- nightly
12+
install:
13+
- cargo install cross || true
14+
- source ~/.cargo/env || true
15+
script:
16+
- cross build --target $TARGET
17+
- cross build --target $TARGET --release
18+
env:
19+
- TARGET=armv7-unknown-linux-gnueabihf
20+
- TARGET=aarch64-unknown-linux-gnu
1121
matrix:
1222
allow_failures:
1323
- rust: nightly

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ exclude = [".idea/*", "doc/**/*.html"]
1212
edition = "2018"
1313

1414
[features]
15-
nightly = ["linux-embedded-hal"]
15+
1616

1717
[dependencies]
18-
sysfs_gpio = "0.5.3"
19-
linux-embedded-hal = {version="0.1.1",optional=true}
20-
wiringpi = "0.2.4"
18+
rppal = "0.11.1"
2119

2220
[badges]
2321
travis-ci = { repository = "rahul-thakoor/rust_gpiozero", branch = "master" }

README.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ A simple interface to GPIO devices with Raspberry Pi.
77
This library is based on [GPIOZero](https://gpiozero.readthedocs.io/en/stable/index.html)
88
library.
99

10-
_Note: This is a work in progress. The library will eventually support `embedded-hal` based drivers_
11-
1210

1311
The idea is to get started with physical computing using Rust with little coding
1412
by hiding the underlying complexity.
@@ -19,37 +17,28 @@ The library uses [BCM Pin numbering](https://pinout.xyz/)
1917

2018
```rust
2119

22-
extern crate rust_gpiozero;
2320
use rust_gpiozero::*;
2421

2522
fn main() {
26-
27-
// Create a new LED attached to Pin 17
28-
29-
let mut led = LED::new(17);
30-
31-
// blink the LED
32-
// on_time: 2 seconds and off_time: 3 seconds
33-
34-
led.blink(2,3);
35-
23+
// Create a new LED attached to Pin 17
24+
let mut led = LED::new(17);
25+
// blink the LED
26+
led.blink(2.0, 3.0);
3627
}
3728

3829
```
3930

4031

4132
### Example : Wait for a Button Press
4233
```rust
43-
extern crate rust_gpiozero;
44-
use rust_gpiozero::*;
4534

35+
use rust_gpiozero::*;
4636

4737
fn main() {
4838
// Create a button which is attached to Pin 17
49-
let button = Button::new(17);
50-
button.wait_for_press();
39+
let mut button = Button::new(17);
40+
button.wait_for_press(None);
5141
println!("button pressed");
52-
5342
}
5443

5544
```
@@ -86,7 +75,7 @@ To use `rust_gpiozero`, first add this to your Cargo.toml:
8675

8776
```toml
8877
[dependencies]
89-
rust_gpiozero = "0.1.0"
78+
rust_gpiozero = "0.2.0"
9079
```
9180
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.
9281

@@ -96,7 +85,7 @@ Compiling your project on a Raspberry Pi directly can take significant time depe
9685

9786
The following features are planned :
9887

99-
- [ ] Support for `linux-embedded-hal`
88+
- [ ] Support for `embedded-hal`
10089
- [ ] Support for common devices such as Accelerometer, Temperature sensors, etc
10190

10291

@@ -105,6 +94,9 @@ The following features are planned :
10594

10695
[GNU General Public License v3.0](https://github.com/rahul-thakoor/rust_gpiozero/blob/master/LICENSE.md)
10796

97+
## Credits
98+
This library would not be possible without the great work of the maintainers of [GPIOZero](https://gpiozero.readthedocs.io/en/stable/index.html) and [rppal](https://github.com/golemparts/rppal)
99+
108100
## Contributing
109101
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.
110102

examples/blink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ fn main() {
66
// Create a new LED attached to Pin 17
77
let mut led = LED::new(17);
88
// blink the LED
9-
led.blink(2, 3);
9+
led.blink(2.0, 3.0);
1010
}

examples/button.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rust_gpiozero::*;
44

55
fn main() {
66
// Create a button which is attached to Pin 17
7-
let button = Button::new(17);
8-
button.wait_for_press();
7+
let mut button = Button::new(17);
8+
button.wait_for_press(None);
99
println!("button pressed");
1010
}

examples/servo.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use rust_gpiozero::*;
2+
use std::io;
3+
use std::io::prelude::*;
4+
use std::thread;
5+
use std::time::Duration;
6+
7+
fn main() {
8+
// Create a new Servo attached to Pin 23
9+
let mut servo = Servo::new(23);
10+
11+
loop{
12+
servo.max();
13+
thread::sleep(Duration::from_millis(2000));
14+
servo.min();
15+
thread::sleep(Duration::from_millis(2000));
16+
}
17+
18+
// wait for key press to exit
19+
let _ = io::stdin().read(&mut [0u8]).unwrap();
20+
}

examples/softpwm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ fn main() {
77
let mut led = PWMLED::new(17);
88

99
// blink the LED 5 times
10-
led.blink(2.0, 2.0, 1.0, 1.0, Some(5));
10+
led.set_blink_count(5);
11+
led.blink(2.0, 2.0, 1.0, 1.0);
1112

1213
// wait for key press to exit
1314
let _ = io::stdin().read(&mut [0u8]).unwrap();

src/devices.rs

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,80 @@
11
//! Describes generic devices such as `GPIODevice` and `CompositeDevice`
2-
use crate::traits::Device;
3-
use sysfs_gpio::Pin;
2+
3+
use rppal::gpio::{Gpio, Level, Pin};
4+
5+
/// Represents a single device of any type; GPIO-based, SPI-based, I2C-based,
6+
/// etc. It defines the basic services applicable to all devices
7+
pub trait Device {
8+
/// Shut down the device and release all associated resources.
9+
fn close(self);
10+
11+
/// Returns ``True`` if the device is currently active and ``False`` otherwise.
12+
fn is_active(&self) -> bool;
13+
}
14+
#[macro_export]
15+
macro_rules! impl_device {
16+
() => {
17+
/// Returns ``True`` if the device is currently active and ``False`` otherwise.
18+
pub fn is_active(&self) -> bool {
19+
self.value()
20+
}
21+
/// Shut down the device and release all associated resources.
22+
pub fn close(self) {
23+
drop(self)
24+
}
25+
}
26+
}
427

528
/// Represents a generic GPIO device and provides the services common to all single-pin GPIO devices
629
#[derive(Debug)]
7-
pub struct GPIODevice {
8-
pub pin: Pin,
30+
pub struct GpioDevice {
31+
pin: Pin,
32+
active_state: bool,
33+
inactive_state: bool,
934
}
1035

11-
impl GPIODevice {
12-
pub fn new(pin: u64) -> GPIODevice {
13-
//Create a new Pin with the provided pin_num
14-
let gpio = Pin::new(pin);
15-
//check if pin is not already exported
36+
macro_rules! impl_gpio_device {
37+
() => {
38+
/// The `Pin` that the device is connected to.
39+
pub fn pin(&self) -> u8 {
40+
self.pin.pin()
41+
}
1642

17-
//try to export the selected pin
18-
//Todo implement better error handling
19-
gpio.export().expect("Could not export the selected gpio");
20-
GPIODevice { pin: gpio }
2143
}
2244
}
2345

24-
impl Device for GPIODevice {
25-
fn pin(&self) -> Pin {
26-
self.pin
46+
impl GpioDevice {
47+
/// Returns a GpioDevice with the pin number given
48+
/// # Arguments
49+
///
50+
/// * `pin` - The GPIO pin which the device is attached to
51+
///
52+
pub fn new(pin: u8) -> GpioDevice {
53+
match Gpio::new() {
54+
Err(e) => panic!("{:?}", e),
55+
Ok(gpio) => match gpio.get(pin) {
56+
Err(e) => panic!("{:?}", e),
57+
Ok(pin) => GpioDevice {
58+
pin,
59+
active_state: true,
60+
inactive_state: false,
61+
},
62+
},
63+
}
2764
}
2865

2966
/// Returns a value representing the device's state.
30-
fn value(&self) -> i8 {
31-
let value = self
32-
.pin
33-
.get_value()
34-
.expect("Could not check if device is active");
35-
value as i8
67+
pub fn value(&self) -> bool {
68+
self.state_to_value()
3669
}
37-
}
3870

39-
//Todo CompositeDevice
71+
fn state_to_value(&self) -> bool {
72+
match self.pin.read() {
73+
Level::High => self.active_state,
74+
Level::Low => !self.active_state,
75+
}
76+
}
77+
78+
impl_device!();
79+
impl_gpio_device!();
80+
}

0 commit comments

Comments
 (0)