on/off on button press

This commit is contained in:
Simon Wörner
2017-03-31 18:16:49 +02:00
parent ce16cf7ab1
commit b4cc1c9b09

View File

@@ -103,7 +103,9 @@ fn run(rdone: chan::Receiver<()>) {
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 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 { loop {
chan_select! { chan_select! {
@@ -111,20 +113,22 @@ fn run(rdone: chan::Receiver<()>) {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
println!("tick"); println!("tick");
let btn = get_value(GPIO_PORT_BTN); // switch enabled if button state changed
let btn = get_value(GPIO_PORT_BTN) == GPIO_BTN_ON;
if btn == GPIO_BTN_ON { if btn && btn != btn_last {
on = !on; enabled = !enabled;
} else {
on = false;
} }
if on { // set led state
if led_on {
set_value(GPIO_PORT_LED, GPIO_LED_ON); set_value(GPIO_PORT_LED, GPIO_LED_ON);
} }
else { else {
set_value(GPIO_PORT_LED, GPIO_LED_OFF); set_value(GPIO_PORT_LED, GPIO_LED_OFF);
} }
btn_last = btn;
led_on = !led_on;
}, },
rdone.recv() => { rdone.recv() => {
// unexport // unexport