Skip to content

Commit 1b105ce

Browse files
Add softpwm
Add support for software-based PWM using wiringpi -PWMOutputDevice -PWMLed
1 parent 416c7d5 commit 1b105ce

File tree

10 files changed

+423
-166
lines changed

10 files changed

+423
-166
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust_gpiozero"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Rahul Thakoor <[email protected]>"]
55
repository = "https://github.com/rahul-thakoor/rust_gpiozero.git"
66
homepage = "https://github.com/rahul-thakoor/rust_gpiozero.git"
@@ -9,13 +9,15 @@ readme = "README.md"
99
description = "A library inspired by gpiozero written in Rust."
1010
keywords = ["embedded-hal", "raspberry","pi", "gpiozero", "rpi"]
1111
exclude = [".idea/*", "doc/**/*.html"]
12+
edition = "2018"
1213

1314
[features]
1415
nightly = ["linux-embedded-hal"]
1516

1617
[dependencies]
17-
sysfs_gpio = "0.5.1"
18+
sysfs_gpio = "0.5.3"
1819
linux-embedded-hal = {version="0.1.1",optional=true}
20+
wiringpi = "0.2.4"
1921

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

examples/blink.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
//! Blinks an LED : on_time: 2 seconds and off_time: 3 seconds
22
3-
extern crate rust_gpiozero;
43
use rust_gpiozero::*;
54

65
fn main() {
7-
86
// Create a new LED attached to Pin 17
97
let mut led = LED::new(17);
108
// blink the LED
11-
led.blink(2,3);
12-
13-
}
9+
led.blink(2, 3);
10+
}

examples/button.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
//! Display message in console when a Button is pressed
2-
extern crate rust_gpiozero;
3-
use rust_gpiozero::*;
42
3+
use rust_gpiozero::*;
54

65
fn main() {
76
// Create a button which is attached to Pin 17
87
let button = Button::new(17);
98
button.wait_for_press();
109
println!("button pressed");
11-
1210
}

examples/softpwm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use rust_gpiozero::*;
2+
use std::io;
3+
use std::io::prelude::*;
4+
5+
fn main() {
6+
// Create a new LED attached to Pin 17
7+
let mut led = PWMLED::new(17);
8+
9+
// blink the LED 5 times
10+
led.blink(2.0, 2.0, 1.0, 1.0, Some(5));
11+
12+
// wait for key press to exit
13+
let _ = io::stdin().read(&mut [0u8]).unwrap();
14+
}

src/devices.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
//! Describes generic devices such as `GPIODevice` and `CompositeDevice`
2+
use crate::traits::Device;
23
use sysfs_gpio::Pin;
3-
use traits::Device;
44

55
/// Represents a generic GPIO device and provides the services common to all single-pin GPIO devices
66
#[derive(Debug)]
77
pub struct GPIODevice {
8-
pub pin: Pin
8+
pub pin: Pin,
99
}
1010

1111
impl GPIODevice {
12-
pub fn new(pin:u64) -> GPIODevice{
12+
pub fn new(pin: u64) -> GPIODevice {
1313
//Create a new Pin with the provided pin_num
14-
let gpio = Pin::new(pin);
15-
//check if pin is not already exported
16-
14+
let gpio = Pin::new(pin);
15+
//check if pin is not already exported
16+
1717
//try to export the selected pin
1818
//Todo implement better error handling
1919
gpio.export().expect("Could not export the selected gpio");
20-
GPIODevice {pin:gpio}
20+
GPIODevice { pin: gpio }
2121
}
22-
23-
2422
}
2523

26-
2724
impl Device for GPIODevice {
2825
fn pin(&self) -> Pin {
29-
self.pin
26+
self.pin
3027
}
3128

32-
/// Returns a value representing the device's state.
29+
/// Returns a value representing the device's state.
3330
fn value(&self) -> i8 {
34-
let value = self.pin.get_value().expect("Could not check if device is active");
31+
let value = self
32+
.pin
33+
.get_value()
34+
.expect("Could not check if device is active");
3535
value as i8
3636
}
37-
3837
}
3938

4039
//Todo CompositeDevice

src/input_devices.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
11
//! Input device component interfaces for devices such as `Button`
2-
use devices::GPIODevice;
3-
use traits::*;
4-
use sysfs_gpio::{Direction,Pin};
5-
2+
use crate::devices::GPIODevice;
3+
use crate::traits::*;
4+
use sysfs_gpio::{Direction, Pin};
65

76
/// Represents a generic GPIO input device.
87
#[derive(Debug)]
98
pub struct InputDevice {
10-
pub pin : Pin
9+
pub pin: Pin,
1110
}
1211

13-
1412
impl InputDevice {
1513
/// Creates a new input device with pin number `pin`
16-
pub fn new(pin:u64) -> InputDevice{
14+
pub fn new(pin: u64) -> InputDevice {
1715
let gpiodevice = GPIODevice::new(pin);
1816
// set direction to input
19-
gpiodevice.pin.set_direction(Direction::In).expect("Could not set pin to Input mode");
17+
gpiodevice
18+
.pin
19+
.set_direction(Direction::In)
20+
.expect("Could not set pin to Input mode");
2021
InputDevice {
21-
pin: gpiodevice.pin
22-
}
22+
pin: gpiodevice.pin,
23+
}
2324
}
2425
}
2526

2627
impl Device for InputDevice {
2728
fn pin(&self) -> Pin {
28-
self.pin
29+
self.pin
2930
}
3031

3132
/// Returns a value representing the device's state.
3233
fn value(&self) -> i8 {
33-
34-
let value = self.pin.get_value().expect("Could not check if device is active");
34+
let value = self
35+
.pin
36+
.get_value()
37+
.expect("Could not check if device is active");
3538
value as i8
3639
}
3740
}
3841

39-
4042
/// Represents a generic input device with typical on/off behaviour.
41-
/// Adds machinery to fire the active and inactive events for devices
43+
/// Adds machinery to fire the active and inactive events for devices
4244
/// that operate in a typical digital manner: straight forward on / off
4345
/// states with (reasonably) clean transitions between the two.
4446
4547
pub struct DigitalInputDevice {
46-
pin : Pin
48+
pin: Pin,
4749
}
4850

49-
impl DigitalInputDevice{
51+
impl DigitalInputDevice {
5052
/// Create a new Digital Input Device
51-
pub fn new(pin:u64) -> DigitalInputDevice {
53+
pub fn new(pin: u64) -> DigitalInputDevice {
5254
let inpin = InputDevice::new(pin);
5355
DigitalInputDevice { pin: inpin.pin }
5456
}
@@ -57,34 +59,39 @@ impl DigitalInputDevice{
5759
/// gives DigitalInputDevice Device behaviours such as close, is_active, etc
5860
impl Device for DigitalInputDevice {
5961
fn pin(&self) -> Pin {
60-
self.pin
62+
self.pin
6163
}
6264

6365
/// Returns a value representing the device's state.
64-
fn value(&self) -> i8 {
65-
let value = self.pin.get_value().expect("Could not check if device is active");
66+
fn value(&self) -> i8 {
67+
let value = self
68+
.pin
69+
.get_value()
70+
.expect("Could not check if device is active");
6671
value as i8
6772
}
6873
}
6974

70-
/// Give DigitalInputDevice event traits
75+
/// Give DigitalInputDevice event traits
7176
impl EventsTrait for DigitalInputDevice {}
7277

73-
7478
/// Represents a simple push button or switch.
7579
/// Connect one side of the button to a ground pin, and the other to any GPIO pin
7680
7781
pub struct Button {
78-
pin: Pin
82+
pin: Pin,
7983
}
8084

8185
impl Device for Button {
8286
fn pin(&self) -> Pin {
83-
self.pin
87+
self.pin
8488
}
8589
/// Returns a value representing the device's state.
86-
fn value(&self) -> i8 {
87-
let value = self.pin.get_value().expect("Could not check if device is active");
90+
fn value(&self) -> i8 {
91+
let value = self
92+
.pin
93+
.get_value()
94+
.expect("Could not check if device is active");
8895
value as i8
8996
}
9097
}
@@ -93,28 +100,23 @@ impl EventsTrait for Button {}
93100

94101
impl Button {
95102
/// Create a new Button
96-
pub fn new(pin:u64) -> Button{
103+
pub fn new(pin: u64) -> Button {
97104
let din = DigitalInputDevice::new(pin);
98-
Button{
99-
pin : din.pin
100-
}
105+
Button { pin: din.pin }
101106
}
102107

103108
/// Pause the script until the device is activated
104-
pub fn wait_for_press(&self){
109+
pub fn wait_for_press(&self) {
105110
self.wait_for_active();
106111
}
107112

108113
/// Pause the script until the device is deactivated
109-
pub fn wait_for_release(&self){
114+
pub fn wait_for_release(&self) {
110115
self.wait_for_inactive();
111116
}
117+
}
112118

113-
114-
}
115-
116-
117-
/// Represents a generic input device which takes its value
119+
/// Represents a generic input device which takes its value
118120
/// from the average of a queue of historical values.
119121
120122
pub struct SmoothedInputDevice;

src/lib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,15 @@
5252
//!
5353
//! ```
5454
55-
extern crate sysfs_gpio;
56-
5755
#[cfg(nightly)]
5856
extern crate embedded_hal as hal;
5957

60-
pub use self::output_devices::*;
6158
pub use self::devices::*;
62-
pub use self::traits::*;
6359
pub use self::input_devices::*;
60+
pub use self::output_devices::*;
61+
pub use self::traits::*;
6462
//pub mod led;
6563
pub mod devices;
66-
pub mod output_devices;
6764
pub mod input_devices;
65+
pub mod output_devices;
6866
pub mod traits;
69-
70-
71-

src/main.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
extern crate rust_gpiozero;
2-
31
use std::thread;
42
use std::time::Duration;
53

64
use rust_gpiozero::*;
75

8-
9-
106
fn main() {
11-
12-
let mut motor = Motor::new(17,27);
7+
let mut motor = Motor::new(17, 27);
138
motor.forward();
149
thread::sleep(Duration::from_secs(3));
1510
motor.stop();
16-
1711
}

0 commit comments

Comments
 (0)