https://github.com/bugadani/debounce
Simple debouncer library written in Rust
https://github.com/bugadani/debounce
Last synced: 9 months ago
JSON representation
Simple debouncer library written in Rust
- Host: GitHub
- URL: https://github.com/bugadani/debounce
- Owner: bugadani
- Created: 2020-05-04T08:57:31.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-04T11:52:12.000Z (about 6 years ago)
- Last Synced: 2025-03-02T15:28:32.435Z (over 1 year ago)
- Language: Rust
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Debounce
=========
Debounce is a Rust library used to process noisy binary state transitions, like button presses in embedded systems.
Creating a debouncer
--------------------
_**Note**: Debounce works by counting consecutive samples, so you'll need to figure out how many samples you need to consider the input **stable**._
As an example we want to process button presses. We determine, that 2 consecutive samples are enough to consider our input stable.
```rust
use debounce::{Debounce, State, Change};
use debounce::consts::U2; // 2 samples for stable signal
let debounce: Debounce = Debounce::new(State::Released);
```
Feeding data
------------
You can feed data into debouncer using the `update` method. Update takes the current state of your input as a `State`, which can be either `State::Touched` or `State::Released`.
You may also use `.into()` on a `bool` to convert it into `State`. `true` will be converted to `State::Touched` and false to `State::Released`.
Observing state
---------------
`update` returns with a `Change` value that indicates what happened. A `Change` can have three values:
* `Change::Touched` or `Change::Released` when the internal state changes.
* `Change::NoChange(state)` when input does not change the internal state. `state` will contain the current stable signal state, which can be diferent from the input state.
_Note: if you only care about the signal state and not the change, you can use `.into()` to convert the `Change` value to a corresponding `State` value._
You can also use the `state` method that returns a `State` value that indicated the current stable signal state.