diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index f5e750b..9cbf698 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -10,24 +10,6 @@ use std::time::Duration; 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_LED: &'static str = "18"; @@ -46,9 +28,13 @@ fn write(path: &str, value: &str) { file.write_all(value.as_bytes()) .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 { + return String::new(); let mut file = File::open(Path::new(path)) .expect(format!("Open file '{}' failed", path).as_str()); let mut contents = String::new(); @@ -56,6 +42,9 @@ fn read(path: &str) -> String { file.read_to_string(&mut contents) .expect(format!("Read from '{}' file failed", path).as_str()); + #[cfg(debug_assertions)] + println!("Read value '{}' from '{}'.", contents, path); + contents } @@ -81,45 +70,57 @@ fn get_value(port: &str) -> String { fn main() { let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); 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! { - signal.recv() -> signal => { - println!("received signal: {:?}", signal); - unexport(GPIO_PORT_BTN); - unexport(GPIO_PORT_LED); + default => { + worker.join(); }, - rdone.recv() => { - println!("Program completed normally."); + signal.recv() -> signal => { + #[cfg(debug_assertions)] + println!("received signal: {:?}", signal); + + sdone.send(()); } } } -fn run(_sdone: chan::Sender<()>) { +fn run(rdone: chan::Receiver<()>) { // init export(GPIO_PORT_BTN); export(GPIO_PORT_LED); set_direction(GPIO_PORT_BTN, GPIO_DIRECTION_IN); set_direction(GPIO_PORT_LED, GPIO_DIRECTION_OUT); + let tick = chan::tick(Duration::from_millis(1000 / 5)); let mut on = false; loop { - let btn = get_value(GPIO_PORT_BTN); + chan_select! { + tick.recv() => { + #[cfg(debug_assertions)] + println!("tick"); - if btn == GPIO_BTN_ON { - on = !on; - } else { - on = false; - } + let btn = get_value(GPIO_PORT_BTN); - if on { - set_value(GPIO_PORT_LED, GPIO_LED_ON); - } - else { - set_value(GPIO_PORT_LED, GPIO_LED_OFF); - } + if btn == GPIO_BTN_ON { + on = !on; + } else { + on = false; + } - thread::sleep(Duration::from_secs(1 / 5)); + if on { + set_value(GPIO_PORT_LED, GPIO_LED_ON); + } + else { + set_value(GPIO_PORT_LED, GPIO_LED_OFF); + } + }, + rdone.recv() => { + // unexport + unexport(GPIO_PORT_BTN); + unexport(GPIO_PORT_LED); + return; + }, + } } }