Skip to content

Commit 5d03273

Browse files
committed
poll: add support for poll command
This lets you block for changes with a timeout on the command line. Kinda handy for some scripts related to button presses and the like (although some amount of debounce logic might be nice to add in the future). Signed-off-by: Paul Osborne <[email protected]>
1 parent 58e8cdd commit 5d03273

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

src/commands/gpio_poll.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pub mod gpio_exportall;
66
pub mod gpio_write;
77
pub mod gpio_unexport;
88
pub mod gpio_unexportall;
9+
pub mod gpio_poll;

src/main.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ fn main() {
4444
.arg(Arg::with_name("pin")
4545
.help("The pin name (or number)")
4646
.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)))
4860

4961
// gpio write
5062
.subcommand(SubCommand::with_name("write")
@@ -140,7 +152,21 @@ fn main() {
140152
};
141153
gpio_read::main(&cfg, &read_options);
142154
}
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+
}
144170
("write", Some(m)) => {
145171
let write_options = GpioWriteOptions {
146172
gpio_opts: gpio_options,

src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct GpioWriteOptions {
2121
#[derive(Debug)]
2222
pub struct GpioPollOptions {
2323
pub gpio_opts: GpioOptions,
24+
pub timeout: Option<isize>,
25+
pub edge: String,
2426
pub pin: String,
2527
}
2628

0 commit comments

Comments
 (0)