File tree Expand file tree Collapse file tree 4 files changed +83
-2
lines changed Expand file tree Collapse file tree 4 files changed +83
-2
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright (C) 2016, The gpio-utils Authors
2
+
3
+ use options:: GpioPollOptions ;
4
+ use config:: GpioConfig ;
5
+ use sysfs_gpio:: Edge ;
6
+ use std:: process:: exit;
7
+
8
+ pub fn main ( config : & GpioConfig , opts : & GpioPollOptions ) {
9
+ let timeout = opts. timeout . unwrap_or ( -1 ) ;
10
+ let pin_config = match config. get_pin ( & opts. pin [ ..] ) {
11
+ Some ( pin) => pin,
12
+ None => {
13
+ println ! ( "Unable to find config entry for pin '{}'" , opts. pin) ;
14
+ exit ( 1 )
15
+ }
16
+ } ;
17
+ let pin = pin_config. get_pin ( ) ;
18
+ let edge = match & opts. edge [ ..] {
19
+ "rising" => Edge :: RisingEdge ,
20
+ "falling" => Edge :: FallingEdge ,
21
+ "both" => Edge :: BothEdges ,
22
+ other => {
23
+ println ! ( "Unexpected edge value: {}" , other) ;
24
+ exit ( 1 ) ;
25
+ }
26
+ } ;
27
+
28
+ // set the pin direction
29
+ pin. set_edge ( edge) . unwrap_or_else ( |e| {
30
+ println ! ( "Error setting edge on pin: {:?}" , e) ;
31
+ exit ( 1 ) ;
32
+ } ) ;
33
+
34
+ let mut poller = pin. get_poller ( ) . unwrap_or_else ( |e| {
35
+ println ! ( "Error creating pin poller: {:?}" , e) ;
36
+ exit ( 1 ) ;
37
+ } ) ;
38
+ match poller. poll ( timeout) {
39
+ Ok ( Some ( value) ) => {
40
+ println ! ( "{}" , value) ;
41
+ exit ( 0 ) ;
42
+ }
43
+ Ok ( None ) => {
44
+ println ! ( "TIMEOUT" ) ;
45
+ exit ( 2 )
46
+ }
47
+ Err ( e) => {
48
+ println ! ( "Error on Poll: {:?}" , e) ;
49
+ exit ( 1 ) ;
50
+ }
51
+ }
52
+ }
Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ pub mod gpio_exportall;
6
6
pub mod gpio_write;
7
7
pub mod gpio_unexport;
8
8
pub mod gpio_unexportall;
9
+ pub mod gpio_poll;
Original file line number Diff line number Diff line change @@ -44,7 +44,19 @@ fn main() {
44
44
. arg ( Arg :: with_name ( "pin" )
45
45
. help ( "The pin name (or number)" )
46
46
. index ( 1 )
47
- . required ( true ) ) )
47
+ . required ( true ) )
48
+ . arg ( Arg :: with_name ( "timeout" )
49
+ . help ( "Timeout (in ms) for the poll operation (-1 to wait forever, default)" )
50
+ . takes_value ( true )
51
+ . short ( "t" )
52
+ . long ( "timeout" )
53
+ . required ( false ) )
54
+ . arg ( Arg :: with_name ( "edge" )
55
+ . help ( "The edge to poll on" )
56
+ . takes_value ( true )
57
+ . short ( "e" )
58
+ . long ( "edge" )
59
+ . required ( false ) ) )
48
60
49
61
// gpio write
50
62
. subcommand ( SubCommand :: with_name ( "write" )
@@ -140,7 +152,21 @@ fn main() {
140
152
} ;
141
153
gpio_read:: main ( & cfg, & read_options) ;
142
154
}
143
- ( "poll" , Some ( _) ) => { }
155
+ ( "poll" , Some ( m) ) => {
156
+ let timeout = m. value_of ( "timeout" ) . map ( |timeout| {
157
+ timeout. parse :: < isize > ( ) . unwrap_or_else ( |_| {
158
+ println ! ( "Unable to parse timeout value {:?} as integer" , timeout) ;
159
+ exit ( 1 ) ;
160
+ } )
161
+ } ) ;
162
+ let poll_options = GpioPollOptions {
163
+ gpio_opts : gpio_options,
164
+ edge : String :: from ( m. value_of ( "edge" ) . unwrap_or ( "both" ) ) ,
165
+ timeout : timeout,
166
+ pin : String :: from ( m. value_of ( "pin" ) . unwrap ( ) ) ,
167
+ } ;
168
+ gpio_poll:: main ( & cfg, & poll_options) ;
169
+ }
144
170
( "write" , Some ( m) ) => {
145
171
let write_options = GpioWriteOptions {
146
172
gpio_opts : gpio_options,
Original file line number Diff line number Diff line change @@ -21,6 +21,8 @@ pub struct GpioWriteOptions {
21
21
#[ derive( Debug ) ]
22
22
pub struct GpioPollOptions {
23
23
pub gpio_opts : GpioOptions ,
24
+ pub timeout : Option < isize > ,
25
+ pub edge : String ,
24
26
pub pin : String ,
25
27
}
26
28
You can’t perform that action at this time.
0 commit comments