#[cfg(feature = "esp")]
use esp_idf_hal::gpio::{Input, InputOutput, Pin, PinDriver};
pub trait PinWrapper {
fn is_high(&self) -> bool;
fn is_low(&self) -> bool {
!self.is_high()
}
}
#[cfg(feature = "esp")]
impl<'d, P: Pin> PinWrapper for PinDriver<'d, P, Input> {
fn is_high(&self) -> bool {
self.is_high()
}
}
#[cfg(feature = "esp")]
impl<'d, P: Pin> PinWrapper for PinDriver<'d, P, InputOutput> {
fn is_high(&self) -> bool {
self.is_high()
}
}
#[cfg(feature = "embedded_hal")]
impl<P> PinWrapper for P
where
Self: embedded_hal::digital::v2::InputPin,
{
fn is_high(&self) -> bool {
self.is_high().unwrap_or_default()
}
}
#[cfg(all(test, feature = "std"))]
pub(crate) mod tests {
use std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::sleep,
time::Duration,
};
use crate::{Button, ButtonConfig, Mode, PinWrapper, State};
pub const CONFIG: ButtonConfig = ButtonConfig {
hold: Duration::from_millis(500),
debounce: Duration::from_micros(700),
release: Duration::from_millis(30),
mode: Mode::PullDown,
};
#[derive(Debug, Default, Clone)]
pub struct MockPin(Arc<AtomicBool>);
impl PinWrapper for MockPin {
fn is_high(&self) -> bool {
self.0.load(Ordering::SeqCst)
}
}
impl Button<MockPin> {
pub fn press_button(&mut self) {
self.pin.press();
self.tick();
assert!(matches!(self.state, State::Down(_)));
sleep(CONFIG.debounce);
self.tick();
}
pub fn release_button(&mut self) {
self.pin.release();
self.tick();
}
}
impl MockPin {
pub fn press(&self) {
self.0.store(true, Ordering::SeqCst);
sleep(CONFIG.debounce);
}
pub fn release(&self) {
self.0.store(false, Ordering::SeqCst);
sleep(CONFIG.debounce);
}
pub fn click(&self) {
self.press();
self.release();
}
}
}