Skip to content

Commit b08dc9f

Browse files
authored
Run cargo fmt and fix all warnings, including ones from clippy (#19)
* Run `cargo fmt` * Fix clippy warnings.
1 parent 8f3bdc7 commit b08dc9f

File tree

6 files changed

+220
-207
lines changed

6 files changed

+220
-207
lines changed

examples/blink.rs

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

99
// on_time = 2 secs, off_time=3 secs
10-
led.blink(2.0,3.0);
10+
led.blink(2.0, 3.0);
1111

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

examples/servo.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
use rust_gpiozero::*;
22
use std::io;
33
use std::io::prelude::*;
4+
use std::sync::atomic::{AtomicBool, Ordering};
45
use std::thread;
56
use std::time::Duration;
67

8+
static RUNNING: AtomicBool = AtomicBool::new(true);
9+
10+
fn watch_stdin() {
11+
// wait for key press to exit
12+
let _ = io::stdin().read(&mut [0u8]).unwrap();
13+
RUNNING.store(false, Ordering::Relaxed);
14+
}
15+
716
fn main() {
17+
std::thread::spawn(watch_stdin);
18+
819
// Create a new Servo attached to Pin 23
920
let mut servo = Servo::new(23);
1021

11-
loop{
22+
while RUNNING.load(Ordering::Relaxed) {
1223
servo.max();
13-
thread::sleep(Duration::from_millis(2000));
24+
thread::sleep(Duration::from_millis(2_000));
1425
servo.min();
15-
thread::sleep(Duration::from_millis(2000));
26+
thread::sleep(Duration::from_millis(2_000));
1627
}
17-
18-
// wait for key press to exit
19-
let _ = io::stdin().read(&mut [0u8]).unwrap();
2028
}

src/devices.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ pub trait Device {
1111
/// Returns ``True`` if the device is currently active and ``False`` otherwise.
1212
fn is_active(&self) -> bool;
1313
}
14+
1415
#[macro_export]
1516
macro_rules! impl_device {
1617
() => {
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-
}
18+
/// Returns ``True`` if the device is currently active and ``False`` otherwise.
19+
pub fn is_active(&self) -> bool {
20+
self.value()
21+
}
22+
/// Shut down the device and release all associated resources.
23+
pub fn close(self) {
24+
drop(self)
25+
}
26+
};
2627
}
2728

2829
/// Represents a generic GPIO device and provides the services common to all single-pin GPIO devices
@@ -35,20 +36,18 @@ pub struct GpioDevice {
3536

3637
macro_rules! impl_gpio_device {
3738
() => {
38-
/// The `Pin` that the device is connected to.
39-
pub fn pin(&self) -> u8 {
40-
self.pin.pin()
41-
}
42-
43-
}
39+
/// The `Pin` that the device is connected to.
40+
pub fn pin(&self) -> u8 {
41+
self.pin.pin()
42+
}
43+
};
4444
}
4545

4646
impl GpioDevice {
4747
/// Returns a GpioDevice with the pin number given
4848
/// # Arguments
4949
///
5050
/// * `pin` - The GPIO pin which the device is attached to
51-
///
5251
pub fn new(pin: u8) -> GpioDevice {
5352
match Gpio::new() {
5453
Err(e) => panic!("{:?}", e),

src/input_devices.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,33 @@ impl InputDevice {
6060
macro_rules! impl_events_mixin {
6161
() => {
6262
/// Pause the program until the device is activated, or the timeout is reached.
63-
fn wait_for(&mut self, timeout:Option<f32>, active: bool){
64-
match timeout{
65-
None =>
63+
fn wait_for(&mut self, timeout: Option<f32>, active: bool) {
64+
match timeout {
65+
None => {
6666
if active {
6767
self.pin.set_interrupt(Trigger::RisingEdge).unwrap();
6868
self.pin.poll_interrupt(true, None).unwrap();
69-
}else{
69+
} else {
7070
self.pin.set_interrupt(Trigger::FallingEdge).unwrap();
7171
self.pin.poll_interrupt(true, None).unwrap();
7272
}
73-
,
74-
Some(n) => if active {
73+
}
74+
Some(n) => {
75+
if active {
7576
self.pin.set_interrupt(Trigger::RisingEdge).unwrap();
76-
self.pin.poll_interrupt(true, Some(Duration::from_millis((n * 1000.0) as u64))).unwrap();
77-
}else{
77+
self.pin
78+
.poll_interrupt(true, Some(Duration::from_millis((n * 1000.0) as u64)))
79+
.unwrap();
80+
} else {
7881
self.pin.set_interrupt(Trigger::FallingEdge).unwrap();
79-
self.pin.poll_interrupt(true, Some(Duration::from_millis((n * 1000.0) as u64))).unwrap();
82+
self.pin
83+
.poll_interrupt(true, Some(Duration::from_millis((n * 1000.0) as u64)))
84+
.unwrap();
8085
}
86+
}
8187
}
8288
}
83-
84-
85-
}
89+
};
8690
}
8791

8892
/// Represents a generic input device with typical on/off behaviour.
@@ -167,6 +171,8 @@ pub struct Button {
167171
pin: InputPin,
168172
active_state: bool,
169173
inactive_state: bool,
174+
// FIXME: Implement debouncing
175+
#[allow(dead_code)]
170176
bounce_time: Option<f32>,
171177
}
172178

src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,13 @@
1919
//! ```
2020
//! use rust_gpiozero::*;
2121
//!
22-
//! fn main() {
23-
//!
2422
//! // Create a new LED attached to Pin 17
25-
//!
2623
//! let mut led = LED::new(17);
2724
//!
2825
//! // blink the LED
2926
//! // on_time: 2 seconds and off_time: 3 seconds
30-
//!
3127
//! led.blink(2.0,3.0);
32-
//!
33-
//! }
3428
//! ```
35-
//!
3629
3730
pub use self::devices::*;
3831
pub use self::input_devices::*;

0 commit comments

Comments
 (0)