fixed sync and sleep, added debug output, clean up

This commit is contained in:
Simon Wörner
2017-03-31 10:16:51 +02:00
parent 70e994eef9
commit f399a80bea

View File

@@ -10,24 +10,6 @@ use std::time::Duration;
use chan_signal::Signal; use chan_signal::Signal;
// static GPIO_BASE_PATH: &'static str = "/sys/class/gpio";
// static GPIO_PATH: &'static str = "gpio{}/";
// static GPIO_EXPORT: &'static str = "export";
// static GPIO_UNEXPORT: &'static str = "unexport";
// static GPIO_VALUE: &'static str = "value";
// static GPIO_DIRECTION: &'static str = "direction";
// static GPIO_BASE_PATH: &'static Path = Path::new("/sys/class/gpio");
// static GPIO_EXPORT_PATH: &'static Path = GPIO_BASE_PATH.join("export").as_path();
// static GPIO_UNEXPORT_PATH: &'static Path = GPIO_BASE_PATH.join("unexport").as_path();
// static GPIO_PATH: &'static Path = GPIO_BASE_PATH.join("gpio{}").as_path();
// static GPIO_VALUE_PATH: &'static Path = GPIO_PATH.join("value").as_path();
// static GPIO_DIRECTION_PATH: &'static Path = GPIO_PATH.join("direction").as_path();
// str0 = "asd"
// str1 = concat!("Test", "Test");
// str2 concat!(str1, "test");
static GPIO_PORT_BTN: &'static str = "17"; static GPIO_PORT_BTN: &'static str = "17";
static GPIO_PORT_LED: &'static str = "18"; static GPIO_PORT_LED: &'static str = "18";
@@ -46,9 +28,13 @@ fn write(path: &str, value: &str) {
file.write_all(value.as_bytes()) file.write_all(value.as_bytes())
.expect(format!("Write value '{}' to '{}' file failed", value, path).as_str()); .expect(format!("Write value '{}' to '{}' file failed", value, path).as_str());
#[cfg(debug_assertions)]
println!("Wrote value '{}' to '{}'.", value, path);
} }
fn read(path: &str) -> String { fn read(path: &str) -> String {
return String::new();
let mut file = File::open(Path::new(path)) let mut file = File::open(Path::new(path))
.expect(format!("Open file '{}' failed", path).as_str()); .expect(format!("Open file '{}' failed", path).as_str());
let mut contents = String::new(); let mut contents = String::new();
@@ -56,6 +42,9 @@ fn read(path: &str) -> String {
file.read_to_string(&mut contents) file.read_to_string(&mut contents)
.expect(format!("Read from '{}' file failed", path).as_str()); .expect(format!("Read from '{}' file failed", path).as_str());
#[cfg(debug_assertions)]
println!("Read value '{}' from '{}'.", contents, path);
contents contents
} }
@@ -81,30 +70,36 @@ fn get_value(port: &str) -> String {
fn main() { fn main() {
let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);
let (sdone, rdone) = chan::sync(0); let (sdone, rdone) = chan::sync(0);
thread::spawn(move || run(sdone)); let worker = thread::spawn(move || run(rdone));
// Wait for a signal or for work to be done.
chan_select! { chan_select! {
signal.recv() -> signal => { default => {
println!("received signal: {:?}", signal); worker.join();
unexport(GPIO_PORT_BTN);
unexport(GPIO_PORT_LED);
}, },
rdone.recv() => { signal.recv() -> signal => {
println!("Program completed normally."); #[cfg(debug_assertions)]
println!("received signal: {:?}", signal);
sdone.send(());
} }
} }
} }
fn run(_sdone: chan::Sender<()>) { fn run(rdone: chan::Receiver<()>) {
// init // init
export(GPIO_PORT_BTN); export(GPIO_PORT_BTN);
export(GPIO_PORT_LED); export(GPIO_PORT_LED);
set_direction(GPIO_PORT_BTN, GPIO_DIRECTION_IN); set_direction(GPIO_PORT_BTN, GPIO_DIRECTION_IN);
set_direction(GPIO_PORT_LED, GPIO_DIRECTION_OUT); set_direction(GPIO_PORT_LED, GPIO_DIRECTION_OUT);
let tick = chan::tick(Duration::from_millis(1000 / 5));
let mut on = false; let mut on = false;
loop { loop {
chan_select! {
tick.recv() => {
#[cfg(debug_assertions)]
println!("tick");
let btn = get_value(GPIO_PORT_BTN); let btn = get_value(GPIO_PORT_BTN);
if btn == GPIO_BTN_ON { if btn == GPIO_BTN_ON {
@@ -119,7 +114,13 @@ fn run(_sdone: chan::Sender<()>) {
else { else {
set_value(GPIO_PORT_LED, GPIO_LED_OFF); set_value(GPIO_PORT_LED, GPIO_LED_OFF);
} }
},
thread::sleep(Duration::from_secs(1 / 5)); rdone.recv() => {
// unexport
unexport(GPIO_PORT_BTN);
unexport(GPIO_PORT_LED);
return;
},
}
} }
} }