Skip to content

Commit 1096a71

Browse files
authored
Merge pull request #438 from quartiq/feature/debounce-replacement
Replacing pin debounce crate
2 parents 1e44d50 + 782ce00 commit 1096a71

File tree

3 files changed

+26
-58
lines changed

3 files changed

+26
-58
lines changed

Cargo.lock

Lines changed: 12 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cortex-m-log = { version = "0.8.0", features = ["log-integration"] }
4242
log = "0.4.22"
4343
heapless = { version = "0.8", features = ["serde"] }
4444
bit_field = "0.10.2"
45-
debounced-pin = {git = "https://github.com/quartiq/rust-debounced-pin"}
45+
debouncr = "0.2"
4646
serde = {version = "1.0", features = ["derive"], default-features = false }
4747
bbqueue = "0.5"
4848
embedded-hal-bus = "0.2"

src/hardware/user_interface.rs

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
33
use super::Channel;
44
use bit_field::BitField;
5-
use hal::hal::digital::{ErrorType, InputPin};
65
use stm32f4xx_hal as hal;
76

8-
use debounced_pin::{Debounce, DebounceState, DebouncedInputPin};
7+
use debouncr::{Debouncer, Repeat10};
98

109
/// Represents an event indicated through the GPIO buttons.
1110
pub enum ButtonEvent {
@@ -19,8 +18,10 @@ type Button2 = hal::gpio::gpiof::PF15<hal::gpio::Input>;
1918

2019
/// Represents the two user input buttons on the front panel.
2120
pub struct UserButtons {
22-
button1: InputButton<Button1, <Button1 as ErrorType>::Error>,
23-
button2: InputButton<Button2, <Button2 as ErrorType>::Error>,
21+
button1: Button1,
22+
button2: Button2,
23+
button1_state: Debouncer<u16, Repeat10>,
24+
button2_state: Debouncer<u16, Repeat10>,
2425
}
2526

2627
impl UserButtons {
@@ -34,8 +35,10 @@ impl UserButtons {
3435
/// The user interface button manager.
3536
pub fn new(button1: Button1, button2: Button2) -> Self {
3637
UserButtons {
37-
button1: InputButton::new(button1),
38-
button2: InputButton::new(button2),
38+
button1,
39+
button2,
40+
button1_state: debouncr::debounce_10(false),
41+
button2_state: debouncr::debounce_10(false),
3942
}
4043
}
4144

@@ -45,61 +48,19 @@ impl UserButtons {
4548
/// An option containing any event that is indicated by the button update.
4649
pub fn update(&mut self) -> Option<ButtonEvent> {
4750
// Prioritize entering standby.
48-
if self.button2.update() {
51+
let button2_pressed = self.button2.is_low();
52+
if let Some(debouncr::Edge::Rising) = self.button2_state.update(button2_pressed) {
4953
return Some(ButtonEvent::Standby);
5054
}
5155

52-
if self.button1.update() {
56+
let button1_pressed = self.button1.is_low();
57+
if let Some(debouncr::Edge::Rising) = self.button1_state.update(button1_pressed) {
5358
return Some(ButtonEvent::InterlockReset);
5459
}
55-
5660
None
5761
}
5862
}
5963

60-
/// A structure representing one of the input buttons.
61-
struct InputButton<INPUT, E>
62-
where
63-
INPUT: InputPin<Error = E>,
64-
E: core::fmt::Debug,
65-
{
66-
button: DebouncedInputPin<INPUT, debounced_pin::ActiveLow>,
67-
was_active: bool,
68-
}
69-
70-
impl<INPUT, E> InputButton<INPUT, E>
71-
where
72-
INPUT: InputPin<Error = E>,
73-
E: core::fmt::Debug,
74-
{
75-
/// Construct a new input button.
76-
pub fn new(button: INPUT) -> Self {
77-
InputButton {
78-
was_active: false,
79-
button: DebouncedInputPin::new(button, debounced_pin::ActiveLow),
80-
}
81-
}
82-
83-
/// Periodically check the state of the input button.
84-
///
85-
/// # Returns
86-
/// True if the debounced button state has encountered an activation.
87-
pub fn update(&mut self) -> bool {
88-
match self.button.update().unwrap() {
89-
DebounceState::Active => {
90-
let result = !self.was_active;
91-
self.was_active = true;
92-
result
93-
}
94-
DebounceState::NotActive => {
95-
self.was_active = false;
96-
false
97-
}
98-
_ => false,
99-
}
100-
}
101-
}
102-
10364
/// Represents LED colors on the front panel.
10465
pub enum Color {
10566
Red,

0 commit comments

Comments
 (0)