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);
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