1
1
//! 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 } ;
6
5
7
6
/// Represents a generic GPIO input device.
8
7
#[ derive( Debug ) ]
9
8
pub struct InputDevice {
10
- pub pin : Pin
9
+ pub pin : Pin ,
11
10
}
12
11
13
-
14
12
impl InputDevice {
15
13
/// Creates a new input device with pin number `pin`
16
- pub fn new ( pin : u64 ) -> InputDevice {
14
+ pub fn new ( pin : u64 ) -> InputDevice {
17
15
let gpiodevice = GPIODevice :: new ( pin) ;
18
16
// 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" ) ;
20
21
InputDevice {
21
- pin : gpiodevice. pin
22
- }
22
+ pin : gpiodevice. pin ,
23
+ }
23
24
}
24
25
}
25
26
26
27
impl Device for InputDevice {
27
28
fn pin ( & self ) -> Pin {
28
- self . pin
29
+ self . pin
29
30
}
30
31
31
32
/// Returns a value representing the device's state.
32
33
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" ) ;
35
38
value as i8
36
39
}
37
40
}
38
41
39
-
40
42
/// 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
42
44
/// that operate in a typical digital manner: straight forward on / off
43
45
/// states with (reasonably) clean transitions between the two.
44
46
45
47
pub struct DigitalInputDevice {
46
- pin : Pin
48
+ pin : Pin ,
47
49
}
48
50
49
- impl DigitalInputDevice {
51
+ impl DigitalInputDevice {
50
52
/// Create a new Digital Input Device
51
- pub fn new ( pin : u64 ) -> DigitalInputDevice {
53
+ pub fn new ( pin : u64 ) -> DigitalInputDevice {
52
54
let inpin = InputDevice :: new ( pin) ;
53
55
DigitalInputDevice { pin : inpin. pin }
54
56
}
@@ -57,34 +59,39 @@ impl DigitalInputDevice{
57
59
/// gives DigitalInputDevice Device behaviours such as close, is_active, etc
58
60
impl Device for DigitalInputDevice {
59
61
fn pin ( & self ) -> Pin {
60
- self . pin
62
+ self . pin
61
63
}
62
64
63
65
/// 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" ) ;
66
71
value as i8
67
72
}
68
73
}
69
74
70
- /// Give DigitalInputDevice event traits
75
+ /// Give DigitalInputDevice event traits
71
76
impl EventsTrait for DigitalInputDevice { }
72
77
73
-
74
78
/// Represents a simple push button or switch.
75
79
/// Connect one side of the button to a ground pin, and the other to any GPIO pin
76
80
77
81
pub struct Button {
78
- pin : Pin
82
+ pin : Pin ,
79
83
}
80
84
81
85
impl Device for Button {
82
86
fn pin ( & self ) -> Pin {
83
- self . pin
87
+ self . pin
84
88
}
85
89
/// 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" ) ;
88
95
value as i8
89
96
}
90
97
}
@@ -93,28 +100,23 @@ impl EventsTrait for Button {}
93
100
94
101
impl Button {
95
102
/// Create a new Button
96
- pub fn new ( pin : u64 ) -> Button {
103
+ pub fn new ( pin : u64 ) -> Button {
97
104
let din = DigitalInputDevice :: new ( pin) ;
98
- Button {
99
- pin : din. pin
100
- }
105
+ Button { pin : din. pin }
101
106
}
102
107
103
108
/// Pause the script until the device is activated
104
- pub fn wait_for_press ( & self ) {
109
+ pub fn wait_for_press ( & self ) {
105
110
self . wait_for_active ( ) ;
106
111
}
107
112
108
113
/// Pause the script until the device is deactivated
109
- pub fn wait_for_release ( & self ) {
114
+ pub fn wait_for_release ( & self ) {
110
115
self . wait_for_inactive ( ) ;
111
116
}
117
+ }
112
118
113
-
114
- }
115
-
116
-
117
- /// Represents a generic input device which takes its value
119
+ /// Represents a generic input device which takes its value
118
120
/// from the average of a queue of historical values.
119
121
120
122
pub struct SmoothedInputDevice ;
0 commit comments