Button driver

crates doc

This crate is a button driver for embedded Rust projects. It offers various usage scenarios, supports ESP, embedded_hal, embassy and no_std targets.

This crate aims to be as flexible as possible to support various HALs and use-cases.

Example

For ESP32C3 with std:

Required features: std, esp ```rust use buttondriver::{Button, ButtonConfig}; use espidfhal::{gpio::PinDriver, prelude::Peripherals}; use espidf_sys::EspError; use log::info;

fn main() -> Result<(), EspError> { espidfsys::linkpatches(); espidfsvc::log::EspLogger::initializedefault();

let peripherals = Peripherals::take().unwrap();
let pin = PinDriver::input(peripherals.pins.gpio9)?;

let mut button = Button::new(pin, ButtonConfig::default());

loop {
    button.tick();

    if button.is_clicked() {
        info!("Click");
    } else if button.is_double_clicked() {
        info!("Double click");
    } else if button.is_triple_clicked() {
        info!("Triple click");
    } else if let Some(dur) = button.current_holding_time() {
        info!("Held for {dur:?}");
    } else if let Some(dur) = button.held_time() {
        info!("Total holding time {time:?}");
    }

    button.reset();
}

} ```

TODO

  1. embassy async support
  2. External timing interface
  3. no_std ESP32 support

Algorithm

High level state-machine diagram