#button #esp32 #switch #no-std

no-std button-driver

Advanced button handling crate

4 releases

0.1.4 Mar 28, 2024
0.1.3 Nov 26, 2023
0.1.2 Nov 7, 2023
0.1.1 Jun 4, 2023
0.1.0 Jun 4, 2023

#268 in Embedded development

Download history 13/week @ 2024-02-17 6/week @ 2024-02-24 4/week @ 2024-03-09 1/week @ 2024-03-16 119/week @ 2024-03-23 31/week @ 2024-03-30 5/week @ 2024-04-06

157 downloads per month

MIT license

19KB
390 lines

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

use button_driver::{Button, ButtonConfig};
use esp_idf_hal::{gpio::PinDriver, prelude::Peripherals};
use esp_idf_sys::EspError;
use log::info;

fn main() -> Result<(), EspError> {
    esp_idf_sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    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

Dependencies

~0–8MB
~32K SLoC