btn-recognizer 0.2.2

Timestamp-driven button debouncing with multi-tap, hold, repeat, and multi-key chord gesture recognition
Documentation
use std::collections::HashMap;
use std::error::Error;
use std::time::Duration;

use fsm_engine::key::{
    ButtonConfig, ButtonEngine, ButtonSnapshot, ChordConfig, ChordId, ChordReleasePolicy,
    FrameworkConfig, GestureConfig, KeyId, RawLevel, Timestamp,
};

fn main() -> Result<(), Box<dyn Error>> {
    let config = FrameworkConfig {
        buttons: vec![
            ButtonConfig::active_low(KeyId(1), Duration::from_millis(20)),
            ButtonConfig::active_low(KeyId(2), Duration::from_millis(20)),
        ],
        chords: vec![ChordConfig {
            id: ChordId(1),
            members: vec![KeyId(1), KeyId(2)],
            window: Duration::from_millis(80),
            priority: 0,
            release: ChordReleasePolicy::AnyMember,
        }],
        default_gestures: GestureConfig::default(),
        gesture_overrides: HashMap::new(),
    };
    let mut engine = ButtonEngine::new(config)?;

    let frames = [
        (0, [RawLevel::High, RawLevel::High]),
        (10, [RawLevel::Low, RawLevel::High]),
        (30, [RawLevel::High, RawLevel::High]),
        (50, [RawLevel::Low, RawLevel::Low]),
        (70, [RawLevel::Low, RawLevel::Low]),
        (100, [RawLevel::High, RawLevel::Low]),
        (120, [RawLevel::High, RawLevel::Low]),
        (140, [RawLevel::High, RawLevel::High]),
        (160, [RawLevel::High, RawLevel::High]),
    ];

    for (millis, levels) in frames {
        let output = engine.update(ButtonSnapshot {
            at: Timestamp::from_millis(millis),
            levels: &levels,
        })?;
        for event in output.events {
            println!("{event:?}");
        }
    }

    let output = engine.advance_to(Timestamp::from_millis(620))?;
    for event in output.events {
        println!("{event:?}");
    }
    Ok(())
}