#![cfg_attr(not(test), no_std)]
use embedded_hal::digital::v2::InputPin;
#[derive(Copy, Clone)]
pub enum ButtonState {
Down,
Press,
Pressing,
LongPress,
Idle,
}
pub enum ButtonPull {
PullUp,
PullDown,
}
pub struct ButtonConfig {
pub pressing_threshold: f32,
pub long_press_threshold: f32,
pub pull: ButtonPull,
}
impl Default for ButtonConfig {
fn default() -> Self {
Self {
pressing_threshold: 0.2,
long_press_threshold: 2.0,
pull: ButtonPull::PullUp,
}
}
}
pub struct Button<PIN> {
counter: u16,
debounced: u8,
long_press_threshold: u16,
pin: PIN,
pressing_threshold: u16,
pull: ButtonPull,
reset: bool,
state: ButtonState,
}
impl<PIN> Button<PIN>
where
PIN: InputPin,
{
pub fn new(pin: PIN, f_refresh: u16, config: ButtonConfig) -> Self {
Self {
counter: 0,
debounced: 0,
long_press_threshold: (f_refresh as f32 / (1.0 / config.long_press_threshold)) as u16,
pin,
pressing_threshold: (f_refresh as f32 / (1.0 / config.pressing_threshold)) as u16,
pull: config.pull,
reset: false,
state: ButtonState::Idle,
}
}
fn raw_state(&self) -> u8 {
match (&self.pull, self.pin.is_low()) {
(ButtonPull::PullUp, Ok(true)) => 1,
(ButtonPull::PullDown, Ok(false)) => 1,
_ => 0,
}
}
pub fn poll(&mut self) {
let state = self.raw_state();
self.debounced |= state;
if self.debounced > 0 {
if self.counter == 0 {
self.state = ButtonState::Down;
}
self.counter = self.counter.wrapping_add(1);
if self.counter >= self.pressing_threshold {
self.state = ButtonState::Pressing;
}
if self.counter >= self.long_press_threshold {
self.state = ButtonState::LongPress;
}
if state == 0 {
if self.reset {
self.state = ButtonState::Idle;
self.reset = false;
} else if self.counter <= self.pressing_threshold {
self.state = ButtonState::Press;
} else {
self.state = ButtonState::Idle;
}
self.counter = 0;
self.debounced = 0;
}
}
}
pub fn read(&mut self) -> ButtonState {
match self.state {
ButtonState::Pressing | ButtonState::LongPress | ButtonState::Idle => self.state,
_ => {
let state = self.state;
self.state = ButtonState::Idle;
state
}
}
}
pub fn reset(&mut self) {
self.reset = true;
}
}