Skip to content

Commit 67b8885

Browse files
author
Felipe Balbi
committed
Add minimal button debounce tests
This commit adds a minimal set of tests for the button logic. Notice that this is, indeed, a breaking a change and coordination with dependent crates is required.
1 parent 62ddb55 commit 67b8885

8 files changed

Lines changed: 878 additions & 31 deletions

File tree

Cargo.lock

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

examples/rt685s-evk/src/bin/power_button.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async fn main(spawner: Spawner) {
119119
// Create a power button instance
120120
let button_a = Input::new(p.PIO1_1, Pull::Up, Inverter::Disabled);
121121
// Create a debouncer instance
122-
let debouncer = Debouncer::new(3, Duration::from_millis(10), ActiveState::ActiveLow);
122+
let debouncer = Debouncer::new(3, 10, ActiveState::ActiveLow);
123123
// Create a custom button configuration instance
124124
let config_a = ButtonConfig::new(debouncer, Duration::from_millis(1000), Duration::from_millis(2000));
125125

power-button-service/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ embassy-time.workspace = true
1313
embedded-hal-async.workspace = true
1414
embedded-hal.workspace = true
1515
log = { workspace = true, optional = true }
16+
17+
[dev-dependencies]
18+
embedded-hal-mock = { version = "0.11.1", features = ["eh1", "embedded-hal-async"] }
19+
tokio = { version = "1.45.0", features = ["rt", "macros"] }

power-button-service/src/button.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Button Service Definitions
22
3-
use embassy_time::{with_timeout, Duration, Instant, TimeoutError, Timer};
3+
use embassy_time::{with_timeout, Delay, Duration, Instant, TimeoutError, Timer};
44
use embedded_hal::digital::InputPin;
55
use embedded_hal_async::digital::Wait;
66

@@ -48,7 +48,9 @@ impl<I: InputPin + Wait> Button<I> {
4848

4949
/// Checks button state.
5050
pub async fn get_button_state(&mut self) -> ButtonState {
51-
match self.config.debouncer.debounce(&mut self.gpio).await {
51+
let mut delay = Delay;
52+
53+
match self.config.debouncer.debounce(&mut self.gpio, &mut delay).await {
5254
true => ButtonState::ButtonPressed(Instant::now()),
5355
false => ButtonState::ButtonReleased(Instant::now()),
5456
}

0 commit comments

Comments
 (0)