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",
"type": "shell",
"command": "cargo run --bin gui",
"command": "cargo run --bin macroboard_gui",
"problemMatcher": []
},
{
"label": "Run Service",
"type": "shell",
"command": "cargo run --bin service",
"command": "cargo run --bin macroboard_service",
"problemMatcher": []
}
]

View file

@ -7,30 +7,37 @@ use arduino_hal::{
};
pub const BUTTON_COUNT: usize = 8;
// pub const DEBOUNCE_DELAY: u64 = 50;
pub struct Button {
pub index: usize,
pub pin: Pin<Input<PullUp>, Dynamic>,
pub is_low: bool,
// pub last_debounce_time: u64,
}
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)
where
F: FnMut(usize, bool),
{
if self.pin.is_high() {
if self.is_low {
self.is_low = false;
match (self.pin.is_high(), self.is_low) {
(true, true) | (false, 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();
let mut buttons: [Button; BUTTON_COUNT] = [
Button {
index: 1,
pin: pins.d5.into_pull_up_input().downgrade(),
is_low: true,
},
Button {
index: 2,
pin: pins.d6.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,
},
Button::new(1, pins.d5.into_pull_up_input().downgrade()),
Button::new(2, pins.d6.into_pull_up_input().downgrade()),
Button::new(3, pins.d7.into_pull_up_input().downgrade()),
Button::new(4, pins.d8.into_pull_up_input().downgrade()),
Button::new(5, pins.d9.into_pull_up_input().downgrade()),
Button::new(6, pins.d10.into_pull_up_input().downgrade()),
Button::new(7, pins.d11.into_pull_up_input().downgrade()),
Button::new(8, pins.d12.into_pull_up_input().downgrade()),
];
let mut serial = default_serial!(dp, pins, 57600);