Fix build and test button debounce

This commit is contained in:
hodasemi 2022-07-20 09:23:31 +02:00
parent dc30979dad
commit 09dcc9c44b
3 changed files with 26 additions and 51 deletions

4
.vscode/tasks.json vendored
View file

@ -6,13 +6,13 @@
{ {
"label": "Run Gui", "label": "Run Gui",
"type": "shell", "type": "shell",
"command": "cargo run --bin gui", "command": "cargo run --bin macroboard_gui",
"problemMatcher": [] "problemMatcher": []
}, },
{ {
"label": "Run Service", "label": "Run Service",
"type": "shell", "type": "shell",
"command": "cargo run --bin service", "command": "cargo run --bin macroboard_service",
"problemMatcher": [] "problemMatcher": []
} }
] ]

View file

@ -7,30 +7,37 @@ use arduino_hal::{
}; };
pub const BUTTON_COUNT: usize = 8; pub const BUTTON_COUNT: usize = 8;
// pub const DEBOUNCE_DELAY: u64 = 50;
pub struct Button { pub struct Button {
pub index: usize, pub index: usize,
pub pin: Pin<Input<PullUp>, Dynamic>, pub pin: Pin<Input<PullUp>, Dynamic>,
pub is_low: bool, pub is_low: bool,
// pub last_debounce_time: u64,
} }
impl Button { impl Button {
pub fn new(index: usize, pin: Pin<Input<PullUp>, Dynamic>) -> Self {
Button {
index,
pin,
is_low: true,
// last_debounce_time: 0,
}
}
pub fn check_state<F>(&mut self, mut f: F) pub fn check_state<F>(&mut self, mut f: F)
where where
F: FnMut(usize, bool), F: FnMut(usize, bool),
{ {
if self.pin.is_high() { match (self.pin.is_high(), self.is_low) {
if self.is_low { (true, true) | (false, false) => {
self.is_low = false; self.is_low = !self.is_low;
f(self.index, true); f(self.index, !self.is_low);
} }
} else {
if !self.is_low {
self.is_low = true;
f(self.index, false); _ => (),
}
} }
} }
} }

View file

@ -40,46 +40,14 @@ fn main() -> ! {
pins.d13.into_output().set_high(); pins.d13.into_output().set_high();
let mut buttons: [Button; BUTTON_COUNT] = [ let mut buttons: [Button; BUTTON_COUNT] = [
Button { Button::new(1, pins.d5.into_pull_up_input().downgrade()),
index: 1, Button::new(2, pins.d6.into_pull_up_input().downgrade()),
pin: pins.d5.into_pull_up_input().downgrade(), Button::new(3, pins.d7.into_pull_up_input().downgrade()),
is_low: true, Button::new(4, pins.d8.into_pull_up_input().downgrade()),
}, Button::new(5, pins.d9.into_pull_up_input().downgrade()),
Button { Button::new(6, pins.d10.into_pull_up_input().downgrade()),
index: 2, Button::new(7, pins.d11.into_pull_up_input().downgrade()),
pin: pins.d6.into_pull_up_input().downgrade(), Button::new(8, pins.d12.into_pull_up_input().downgrade()),
is_low: true,
},
Button {
index: 3,
pin: pins.d7.into_pull_up_input().downgrade(),
is_low: true,
},
Button {
index: 4,
pin: pins.d8.into_pull_up_input().downgrade(),
is_low: true,
},
Button {
index: 5,
pin: pins.d9.into_pull_up_input().downgrade(),
is_low: true,
},
Button {
index: 6,
pin: pins.d10.into_pull_up_input().downgrade(),
is_low: true,
},
Button {
index: 7,
pin: pins.d11.into_pull_up_input().downgrade(),
is_low: true,
},
Button {
index: 8,
pin: pins.d12.into_pull_up_input().downgrade(),
is_low: true,
},
]; ];
let mut serial = default_serial!(dp, pins, 57600); let mut serial = default_serial!(dp, pins, 57600);