[][src]Crate debouncr

Debouncr

A simple and efficient no_std input debouncer that uses integer bit shifting to debounce inputs. The algorithm can detect rising and falling edges and only requires 1 byte of RAM for detecting up to 8 consecutive high/low states or 2 bytes of RAM for detecting up to 16 consecutive high/low states.

The algorithm is based on the Ganssle Guide to Debouncing (section "An Alternative").

API

Instantiate

First, decide how many consecutive logical-high states you want to detect. For example, if you poll the input pin every 5 ms and require 4 consecutive logical-high states to trigger a debounced press event, that event will happen after 20 ms.

use debouncr::{Debouncer, debounce_4};
let mut debouncer = debounce_4(); // Type: Debouncer<u8, Repeat4>

Update

In regular intervals, call the update(pressed) function to update the internal state.

let mut debouncer = debounce_3();
assert_eq!(debouncer.update(poll_button()), None);
assert_eq!(debouncer.update(poll_button()), None);
assert_eq!(debouncer.update(poll_button()), Some(Edge::Rising));

The update function will return a rising/falling edge, or None if the input is still bouncing.

Query debounced state

You can also query the current debounced state. If none of the n recent updates were pressed, then the debounced state will be low. If all n recent updates were pressed, then the debounced state will be high.

let mut debouncer = debounce_3();

// Initially low
assert!(debouncer.is_low());
assert!(!debouncer.is_high());

// Update, now it's neither high nor low
debouncer.update(true);
assert!(!debouncer.is_low());
assert!(!debouncer.is_high());

// After two more updates, it's high
debouncer.update(true);
debouncer.update(true);
assert!(debouncer.is_high());

Example: RTFM

If you want to debounce a pin in an RTFM project, register a resource and a timer.

This example is not tested
use debouncr::{Debouncer, debounce_12, Edge, Repeat12};

#[app(..., monotonic = rtfm::cyccnt::CYCCNT)]
const APP: () = {
    struct Resources {
        button: gpioa::PA11<Input<PullUp>>,
        button_state: Debounce<u16, Repeat12>,
    }

    #[init(spawn = [poll_button])]
    fn init(ctx: init::Context) -> init::LateResources {
        // ...
        ctx.spawn.poll_button().unwrap();
        init::LateResources {
            button,
            button_state: debounce_12(),
        }
    }

    /// Regularly called task that polls the buttons and debounces them.
    #[task(
        resources = [button, button_state],
        spawn = [button_pressed, button_released],
        schedule = [poll_button],
    )]
    fn poll_button(ctx: poll_button::Context) {
        // Poll button
        let pressed: bool = ctx.resources.button.is_low().unwrap();

        // Update state
        let edge = ctx.resources.button_state.update(pressed);

        // Dispatch event
        if edge == Some(Edge::Rising) {
            ctx.spawn.button_pressed().unwrap();
        } else if edge == Some(Edge::Falling) {
            ctx.spawn.button_released().unwrap();
        }

        // Re-schedule the timer interrupt
        ctx.schedule
            .poll_button(ctx.scheduled + POLL_PERIOD.cycles())
            .unwrap();
    }

    /// The button was pressed.
    #[task]
    fn button_pressed(ctx: button_pressed::Context) {
        // Button was pressed, handle event somehow
    }

    /// The button was released.
    #[task]
    fn button_released(ctx: button_pressed::Context) {
        // Button was released, handle event somehow
    }

};

Structs

Debouncer

The main debouncer instance.

Repeat2

Detect 2 consecutive logical-high states.

Repeat3

Detect 3 consecutive logical-high states.

Repeat4

Detect 4 consecutive logical-high states.

Repeat5

Detect 5 consecutive logical-high states.

Repeat6

Detect 6 consecutive logical-high states.

Repeat7

Detect 7 consecutive logical-high states.

Repeat8

Detect 8 consecutive logical-high states.

Repeat9

Detect 9 consecutive logical-high states.

Repeat10

Detect 10 consecutive logical-high states.

Repeat11

Detect 11 consecutive logical-high states.

Repeat12

Detect 12 consecutive logical-high states.

Repeat13

Detect 13 consecutive logical-high states.

Repeat14

Detect 14 consecutive logical-high states.

Repeat15

Detect 15 consecutive logical-high states.

Repeat16

Detect 16 consecutive logical-high states.

Enums

Edge

Rising or falling edge.

Functions

debounce_2

Create a new debouncer that can detect a rising edge of 2 consecutive logical-high states.

debounce_3

Create a new debouncer that can detect a rising edge of 3 consecutive logical-high states.

debounce_4

Create a new debouncer that can detect a rising edge of 4 consecutive logical-high states.

debounce_5

Create a new debouncer that can detect a rising edge of 5 consecutive logical-high states.

debounce_6

Create a new debouncer that can detect a rising edge of 6 consecutive logical-high states.

debounce_7

Create a new debouncer that can detect a rising edge of 7 consecutive logical-high states.

debounce_8

Create a new debouncer that can detect a rising edge of 8 consecutive logical-high states.

debounce_9

Create a new debouncer that can detect a rising edge of 9 consecutive logical-high states.

debounce_10

Create a new debouncer that can detect a rising edge of 10 consecutive logical-high states.

debounce_11

Create a new debouncer that can detect a rising edge of 11 consecutive logical-high states.

debounce_12

Create a new debouncer that can detect a rising edge of 12 consecutive logical-high states.

debounce_13

Create a new debouncer that can detect a rising edge of 13 consecutive logical-high states.

debounce_14

Create a new debouncer that can detect a rising edge of 14 consecutive logical-high states.

debounce_15

Create a new debouncer that can detect a rising edge of 15 consecutive logical-high states.

debounce_16

Create a new debouncer that can detect a rising edge of 16 consecutive logical-high states.