From d877ec774fa2a6ad573b793a7b5c973e88645e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Thu, 30 Mar 2017 21:25:26 +0200 Subject: [PATCH 01/12] added led5 --- .gitignore | 3 ++ V1/led5/Cargo.lock | 66 +++++++++++++++++++++++ V1/led5/Cargo.toml | 8 +++ V1/led5/src/main.rs | 128 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 V1/led5/Cargo.lock create mode 100644 V1/led5/Cargo.toml create mode 100644 V1/led5/src/main.rs diff --git a/.gitignore b/.gitignore index f805e81..958a081 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ # Debug files *.dSYM/ *.su + +# Rust +target/ diff --git a/V1/led5/Cargo.lock b/V1/led5/Cargo.lock new file mode 100644 index 0000000..152edd2 --- /dev/null +++ b/V1/led5/Cargo.lock @@ -0,0 +1,66 @@ +[root] +name = "led5" +version = "0.1.0" +dependencies = [ + "chan 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", + "chan-signal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bit-set" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bit-vec" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "chan" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "chan-signal" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "chan 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9bf6104718e80d7b26a68fdbacff3481cfc05df670821affc7e9cbc1884400c" +"checksum bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5b97c2c8e8bbb4251754f559df8af22fb264853c7d009084a576cdf12565089d" +"checksum chan 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)" = "f93bfe971116428a9066c1c3c69a09ae3ef69432f8418be28ab50f96783e6a50" +"checksum chan-signal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0f3bb6c3bc387004ad914f0c5b7f33ace8bf7604bbec35f228b1a017f52cd3a0" +"checksum lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4732c563b9a21a406565c4747daa7b46742f082911ae4753f390dc9ec7ee1a97" +"checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135" +"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" diff --git a/V1/led5/Cargo.toml b/V1/led5/Cargo.toml new file mode 100644 index 0000000..345cf6b --- /dev/null +++ b/V1/led5/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "led5" +version = "0.1.0" +authors = ["Simon Wörner "] + +[dependencies] +chan = ">=0.1.19" +chan-signal = ">=0.2.0" diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs new file mode 100644 index 0000000..fea4313 --- /dev/null +++ b/V1/led5/src/main.rs @@ -0,0 +1,128 @@ +#[macro_use] +extern crate chan; +extern crate chan_signal; + +use std::io::prelude::*; +use std::fs::File; +use std::thread; +use std::path::Path; +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"; + +static GPIO_DIRECTION_IN: &'static str = "in"; +static GPIO_DIRECTION_OUT: &'static str = "out"; + +static GPIO_BTN_ON: &'static str = "0"; +//static GPIO_BTN_OFF: &'static str = "1"; + +static GPIO_LED_ON: &'static str = "0"; +static GPIO_LED_OFF: &'static str = "1"; + +fn open(path: &str) -> File { + return File::open(Path::new(path)) + .expect(format!("Open file '{}' failed", path).as_str()); +} + +fn write(path: &str, value: &str) { + let mut file = open(path); + + file.write_all(value.as_bytes()) + .expect(format!("Write value '{}' to '{}' file failed", value, path).as_str()); +} + +fn read(path: &str) -> String { + let mut file = open(path); + let mut contents = String::new(); + + file.read_to_string(&mut contents) + .expect(format!("Read from '{}' file failed", path).as_str()); + + contents +} + +fn export(port: &str) { + write("/sys/class/gpio/export", port) +} +fn unexport(port: &str) { + write("/sys/class/gpio/unexport", port) +} + +fn set_direction(port: &str, direction: &str) { + write(format!("/sys/class/gpio/gpio{}/direction", port).as_str(), direction); +} + +fn set_value(port: &str, value: &str) { + write(format!("/sys/class/gpio/gpio{}/value", port).as_str(), value); +} + +fn get_value(port: &str) -> String { + read(format!("/sys/class/gpio/gpio{}/value", port).as_str()) +} + +fn main() { + let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); + let (sdone, rdone) = chan::sync(0); + thread::spawn(move || run(sdone)); + + // 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); + }, + rdone.recv() => { + println!("Program completed normally."); + } + } +} +fn run(_sdone: chan::Sender<()>) { + // 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 mut on = false; + + loop { + let btn = get_value(GPIO_PORT_BTN); + + if btn == GPIO_BTN_ON { + on = !on; + } else { + on = false; + } + + if on { + set_value(GPIO_PORT_LED, GPIO_LED_ON); + } + else { + set_value(GPIO_PORT_LED, GPIO_LED_OFF); + } + + thread::sleep(Duration::from_secs(1 / 5)); + } +} From 80934a5c706c53796519efc6187c3a03ab7dd77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Thu, 30 Mar 2017 21:55:00 +0200 Subject: [PATCH 02/12] added ci scripts for rust --- .gitlab-ci.yml | 14 ++++++++++++-- ci/run-cargo-test.sh | 16 ++++++++++++++++ ci/run-rustfmt.sh | 13 +++++++++++++ ci/run-shellcheck.sh | 3 +++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100755 ci/run-cargo-test.sh create mode 100755 ci/run-rustfmt.sh create mode 100755 ci/run-shellcheck.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fae1209..9ff9204 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,8 +1,18 @@ -# image: scorpil/rust:stable +image: scorpil/rust:stable stages: - test +cargo:test: + stage: test + script: + - ci/run-cargo-test.sh + +rustfmt: + stage: test + script: + - ci/run-rustfmt.sh + shellcheck: stage: test image: ubuntu:yakkety @@ -13,4 +23,4 @@ shellcheck: - export LANGUAGE=en_US:en - export LC_ALL=en_US.UTF-8 script: - - find . -type f -name '*.sh' -print0 | xargs -n 1 -0 shellcheck --color + - ci/run-shellcheck.sh diff --git a/ci/run-cargo-test.sh b/ci/run-cargo-test.sh new file mode 100755 index 0000000..bc151bb --- /dev/null +++ b/ci/run-cargo-test.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +ERROR=0 + +while IFS= read -r -d '' f; do + dir="$(dirname "${f}")" + + echo "run 'cargo test' in ${dir}" + cd "${dir}" && cargo test + + if [[ $? -ne 0 ]]; then + ERROR=1 + fi +done < <(find . -type f -name 'Cargo.toml' -print0) + +exit ${ERROR} diff --git a/ci/run-rustfmt.sh b/ci/run-rustfmt.sh new file mode 100755 index 0000000..37403d1 --- /dev/null +++ b/ci/run-rustfmt.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +ERROR=0 + +while IFS= read -r -d '' f; do + echo "${f}" + + if [ "$(rustfmt --write-mode=diff "$f")" != $'' ] ; then + ERROR=1 + fi +done < <(find . -type f -name '*.rs' -print0) + +exit ${ERROR} diff --git a/ci/run-shellcheck.sh b/ci/run-shellcheck.sh new file mode 100755 index 0000000..4a2de49 --- /dev/null +++ b/ci/run-shellcheck.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +find . -type f -name '*.sh' -print0 | xargs -n 1 -0 shellcheck --color From 70e994eef9423358bba9842811f0052bddc01333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Thu, 30 Mar 2017 21:55:12 +0200 Subject: [PATCH 03/12] fixed file write --- V1/led5/src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index fea4313..f5e750b 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -40,20 +40,17 @@ static GPIO_BTN_ON: &'static str = "0"; static GPIO_LED_ON: &'static str = "0"; static GPIO_LED_OFF: &'static str = "1"; -fn open(path: &str) -> File { - return File::open(Path::new(path)) - .expect(format!("Open file '{}' failed", path).as_str()); -} - fn write(path: &str, value: &str) { - let mut file = open(path); + let mut file = File::create(Path::new(path)) + .expect(format!("Open file '{}' failed", path).as_str()); file.write_all(value.as_bytes()) .expect(format!("Write value '{}' to '{}' file failed", value, path).as_str()); } fn read(path: &str) -> String { - let mut file = open(path); + let mut file = File::open(Path::new(path)) + .expect(format!("Open file '{}' failed", path).as_str()); let mut contents = String::new(); file.read_to_string(&mut contents) From f399a80bea2cd0e0fb81bae86160b7b4a34bc582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 10:16:51 +0200 Subject: [PATCH 04/12] fixed sync and sleep, added debug output, clean up --- V1/led5/src/main.rs | 81 +++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 40 deletions(-) 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; + }, + } } } From 6d5cc0354c888c27cbaa10e550215c6b42faeab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 10:23:42 +0200 Subject: [PATCH 05/12] fixed read, code clean uo --- V1/led5/src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 9cbf698..07dab86 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -32,9 +32,7 @@ fn write(path: &str, value: &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(); @@ -62,7 +60,6 @@ fn set_direction(port: &str, direction: &str) { fn set_value(port: &str, value: &str) { write(format!("/sys/class/gpio/gpio{}/value", port).as_str(), value); } - fn get_value(port: &str) -> String { read(format!("/sys/class/gpio/gpio{}/value", port).as_str()) } @@ -70,11 +67,14 @@ fn get_value(port: &str) -> String { fn main() { let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); let (sdone, rdone) = chan::sync(0); - let worker = thread::spawn(move || run(rdone)); + let worker = thread::Builder::new() + .name("worker".to_string()) + .spawn(move || run(rdone)) + .expect("Create worker thread failed"); chan_select! { default => { - worker.join(); + worker.join().expect("Worker thread paniced"); }, signal.recv() -> signal => { #[cfg(debug_assertions)] @@ -84,6 +84,7 @@ fn main() { } } } + fn run(rdone: chan::Receiver<()>) { // init export(GPIO_PORT_BTN); From 4186c025fa84bd6ea7a0ddcf2c869e746de90c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 12:38:36 +0200 Subject: [PATCH 06/12] fixed signale handling --- V1/led5/src/main.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 07dab86..69d589a 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -5,6 +5,7 @@ extern crate chan_signal; use std::io::prelude::*; use std::fs::File; use std::thread; +use std::thread::JoinHandle; use std::path::Path; use std::time::Duration; @@ -67,15 +68,18 @@ fn get_value(port: &str) -> String { fn main() { let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); let (sdone, rdone) = chan::sync(0); + let (spanic, rpanic) = chan::sync(0); let worker = thread::Builder::new() .name("worker".to_string()) .spawn(move || run(rdone)) .expect("Create worker thread failed"); + let watchdog = thread::Builder::new() + .name("watchdog".to_string()) + .spawn(move || watchdog(worker, spanic)) + .expect("Create watchdog thread failed"); chan_select! { - default => { - worker.join().expect("Worker thread paniced"); - }, + rpanic.recv() => { }, signal.recv() -> signal => { #[cfg(debug_assertions)] println!("received signal: {:?}", signal); @@ -83,6 +87,12 @@ fn main() { sdone.send(()); } } + + watchdog.join().expect("Watchdog thread paniced"); +} + +fn watchdog(thread: JoinHandle<()>, sdone: chan::Sender<()>) { + thread.join().expect("Thread paniced"); } fn run(rdone: chan::Receiver<()>) { From ae814dbe0872de43690b24819388bf5210b750d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 12:40:52 +0200 Subject: [PATCH 07/12] send panic event --- V1/led5/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 69d589a..c400870 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -91,8 +91,9 @@ fn main() { watchdog.join().expect("Watchdog thread paniced"); } -fn watchdog(thread: JoinHandle<()>, sdone: chan::Sender<()>) { +fn watchdog(thread: JoinHandle<()>, spanic: chan::Sender<()>) { thread.join().expect("Thread paniced"); + spanic.send(()); } fn run(rdone: chan::Receiver<()>) { From 8443acb1291ca2eb8df2b33656157e49fbbf58aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 17:57:09 +0200 Subject: [PATCH 08/12] trim read value --- V1/led5/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index c400870..9361091 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -62,7 +62,7 @@ fn set_value(port: &str, value: &str) { write(format!("/sys/class/gpio/gpio{}/value", port).as_str(), value); } fn get_value(port: &str) -> String { - read(format!("/sys/class/gpio/gpio{}/value", port).as_str()) + String::from(read(format!("/sys/class/gpio/gpio{}/value", port).as_str()).trim()) } fn main() { From ce16cf7ab147c09f464577313fbe5f87fea1d3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 18:09:29 +0200 Subject: [PATCH 09/12] fixed watchdog --- V1/led5/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 9361091..193d6f4 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -91,9 +91,8 @@ fn main() { watchdog.join().expect("Watchdog thread paniced"); } -fn watchdog(thread: JoinHandle<()>, spanic: chan::Sender<()>) { +fn watchdog(thread: JoinHandle<()>, _spanic: chan::Sender<()>) { thread.join().expect("Thread paniced"); - spanic.send(()); } fn run(rdone: chan::Receiver<()>) { From b4cc1c9b09bcbc7976a6ca4334ee2c6fa05ea435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 18:16:49 +0200 Subject: [PATCH 10/12] on/off on button press --- V1/led5/src/main.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 193d6f4..6ef43cb 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -103,7 +103,9 @@ fn run(rdone: chan::Receiver<()>) { set_direction(GPIO_PORT_LED, GPIO_DIRECTION_OUT); let tick = chan::tick(Duration::from_millis(1000 / 5)); - let mut on = false; + let mut btn_last = false; + let mut led_on = false; + let mut enabled = true; loop { chan_select! { @@ -111,20 +113,22 @@ fn run(rdone: chan::Receiver<()>) { #[cfg(debug_assertions)] println!("tick"); - let btn = get_value(GPIO_PORT_BTN); - - if btn == GPIO_BTN_ON { - on = !on; - } else { - on = false; + // switch enabled if button state changed + let btn = get_value(GPIO_PORT_BTN) == GPIO_BTN_ON; + if btn && btn != btn_last { + enabled = !enabled; } - if on { + // set led state + if led_on { set_value(GPIO_PORT_LED, GPIO_LED_ON); } else { set_value(GPIO_PORT_LED, GPIO_LED_OFF); } + + btn_last = btn; + led_on = !led_on; }, rdone.recv() => { // unexport From 54a27c508acc940544995ebe0cec9c9bf5186a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 18:18:37 +0200 Subject: [PATCH 11/12] blink led only if enabled --- V1/led5/src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 6ef43cb..0812249 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -119,6 +119,13 @@ fn run(rdone: chan::Receiver<()>) { enabled = !enabled; } + // blink led + if enabled { + led_on = !led_on; + } else { + led_on = false; + } + // set led state if led_on { set_value(GPIO_PORT_LED, GPIO_LED_ON); @@ -128,7 +135,6 @@ fn run(rdone: chan::Receiver<()>) { } btn_last = btn; - led_on = !led_on; }, rdone.recv() => { // unexport From 48b4310a67d751e09d478d606c810b0d6cf7a157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Fri, 31 Mar 2017 18:26:00 +0200 Subject: [PATCH 12/12] faster tick rate for button check --- V1/led5/src/main.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs index 0812249..36aee08 100644 --- a/V1/led5/src/main.rs +++ b/V1/led5/src/main.rs @@ -102,16 +102,17 @@ fn run(rdone: chan::Receiver<()>) { 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 btn_tick = chan::tick(Duration::from_millis(50)); + let led_tick = chan::tick(Duration::from_millis(1000 / 5)); let mut btn_last = false; let mut led_on = false; let mut enabled = true; loop { chan_select! { - tick.recv() => { - #[cfg(debug_assertions)] - println!("tick"); + btn_tick.recv() => { + //#[cfg(debug_assertions)] + //println!("btn_tick"); // switch enabled if button state changed let btn = get_value(GPIO_PORT_BTN) == GPIO_BTN_ON; @@ -119,7 +120,13 @@ fn run(rdone: chan::Receiver<()>) { enabled = !enabled; } - // blink led + btn_last = btn; + }, + led_tick.recv() => { + #[cfg(debug_assertions)] + println!("led_tick"); + + // blink led if enabled if enabled { led_on = !led_on; } else { @@ -133,8 +140,6 @@ fn run(rdone: chan::Receiver<()>) { else { set_value(GPIO_PORT_LED, GPIO_LED_OFF); } - - btn_last = btn; }, rdone.recv() => { // unexport