Skip to content

Commit

Permalink
Merge pull request #438 from quartiq/feature/debounce-replacement
Browse files Browse the repository at this point in the history
Replacing pin debounce crate
  • Loading branch information
jordens authored Aug 29, 2024
2 parents 1e44d50 + 782ce00 commit 1096a71
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 58 deletions.
17 changes: 12 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cortex-m-log = { version = "0.8.0", features = ["log-integration"] }
log = "0.4.22"
heapless = { version = "0.8", features = ["serde"] }
bit_field = "0.10.2"
debounced-pin = {git = "https://github.com/quartiq/rust-debounced-pin"}
debouncr = "0.2"
serde = {version = "1.0", features = ["derive"], default-features = false }
bbqueue = "0.5"
embedded-hal-bus = "0.2"
Expand Down
65 changes: 13 additions & 52 deletions src/hardware/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

use super::Channel;
use bit_field::BitField;
use hal::hal::digital::{ErrorType, InputPin};
use stm32f4xx_hal as hal;

use debounced_pin::{Debounce, DebounceState, DebouncedInputPin};
use debouncr::{Debouncer, Repeat10};

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

/// Represents the two user input buttons on the front panel.
pub struct UserButtons {
button1: InputButton<Button1, <Button1 as ErrorType>::Error>,
button2: InputButton<Button2, <Button2 as ErrorType>::Error>,
button1: Button1,
button2: Button2,
button1_state: Debouncer<u16, Repeat10>,
button2_state: Debouncer<u16, Repeat10>,
}

impl UserButtons {
Expand All @@ -34,8 +35,10 @@ impl UserButtons {
/// The user interface button manager.
pub fn new(button1: Button1, button2: Button2) -> Self {
UserButtons {
button1: InputButton::new(button1),
button2: InputButton::new(button2),
button1,
button2,
button1_state: debouncr::debounce_10(false),
button2_state: debouncr::debounce_10(false),
}
}

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

if self.button1.update() {
let button1_pressed = self.button1.is_low();
if let Some(debouncr::Edge::Rising) = self.button1_state.update(button1_pressed) {
return Some(ButtonEvent::InterlockReset);
}

None
}
}

/// A structure representing one of the input buttons.
struct InputButton<INPUT, E>
where
INPUT: InputPin<Error = E>,
E: core::fmt::Debug,
{
button: DebouncedInputPin<INPUT, debounced_pin::ActiveLow>,
was_active: bool,
}

impl<INPUT, E> InputButton<INPUT, E>
where
INPUT: InputPin<Error = E>,
E: core::fmt::Debug,
{
/// Construct a new input button.
pub fn new(button: INPUT) -> Self {
InputButton {
was_active: false,
button: DebouncedInputPin::new(button, debounced_pin::ActiveLow),
}
}

/// Periodically check the state of the input button.
///
/// # Returns
/// True if the debounced button state has encountered an activation.
pub fn update(&mut self) -> bool {
match self.button.update().unwrap() {
DebounceState::Active => {
let result = !self.was_active;
self.was_active = true;
result
}
DebounceState::NotActive => {
self.was_active = false;
false
}
_ => false,
}
}
}

/// Represents LED colors on the front panel.
pub enum Color {
Red,
Expand Down

0 comments on commit 1096a71

Please sign in to comment.