This crate provides a time-based debouncer for handling signal noise in digital input signals, particularly suited for embedded firmware applications. It stabilizes input signals over a specified debounce period, effectively filtering out noise and ensuring signal integrity.
- Generic Implementation: Works with any data type that implements the
PartialEq
andCopy
traits. - Flexible Timing: Utilizes the
Monotonic
trait for time handling (which is already implemented for all instances ofrtic_time::Monotonic
), allowing for integration with any (global) timer implementation. - State Tracking: Tracks the current and previous states of the input, providing insight into signal transitions.
First you have to provide an implementation of the Monotonic
trait. The easiest way is to provide any rtic_time::Monotonic
trait implementation:
use fugit::ExtU32;
use rtic_monotonics::systick::*; // Replace with your Monotonic timer implementation
// Generate the required token. Replace this with something appropriate for your platform
let systick_token = rtic_monotonics::create_systick_token!();
// Start the monotonic
Systick::start(systick, 12_000_000, systick_token);
use stabilizer::TimedDebouncer;
// Initialize the debouncer with a starting value and a debounce duration
let mut debouncer = TimedDebouncer::<Systick, _>::new(false, 10.millis());
loop {
// Update the debouncer with new values as they come in
let state = debouncer.update(input.is_high());
if state.transitioned() {
let state = state.stable();
//TODO Do something with the newly stable state
}
delay(2.millis());
}
let mut debouncer = TimedDebouncer::<Systick, _>::new(false, 10.millis());
loop {
// Update the debouncer with new values as they come in
let state = debouncer.update(input.is_high());
// Check the state of the debouncer
match state {
State::Stable { value } => println!("Stable value: {:?}", value),
State::Unstable { stable, most_recent } => println!("Unstable - Stable: {:?}, Current: {:?}", stable, most_recent),
State::Transitioned { stable, previous_stable } => println!("Transitioned to {:?} from {:?}", stable, previous_stable),
}
delay(2.millis());
}
All source code (including code snippets) is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.