#[cfg(target_os = "none")]
mod button_watch;
pub mod button_watch_generated;
#[cfg(target_os = "none")]
#[doc(hidden)]
pub use button_watch::{ButtonWatchEsp, ButtonWatchStaticEsp};
#[doc(hidden)]
pub use device_envoy_core::button::__ButtonMonitor;
pub use device_envoy_core::button::Button;
pub use device_envoy_core::button::{PressDuration, PressedTo};
#[doc(hidden)]
pub use device_envoy_core::button::{
BUTTON_DEBOUNCE_DELAY, BUTTON_POLL_INTERVAL, LONG_PRESS_DURATION,
};
#[cfg(target_os = "none")]
use esp_hal::gpio::{Input, InputConfig, InputPin, Pull};
#[cfg(target_os = "none")]
pub struct ButtonEsp<'d> {
input: Input<'d>,
pressed_to: PressedTo,
}
#[cfg(target_os = "none")]
impl<'d> ButtonEsp<'d> {
#[must_use]
pub fn new(button_pin: impl InputPin + 'd, pressed_to: PressedTo) -> Self {
let pull = match pressed_to {
PressedTo::Voltage => Pull::Down,
PressedTo::Ground => Pull::Up,
};
let input = Input::new(button_pin, InputConfig::default().with_pull(pull));
Self { input, pressed_to }
}
}
#[cfg(target_os = "none")]
impl device_envoy_core::button::__ButtonMonitor for ButtonEsp<'_> {
fn is_pressed_raw(&self) -> bool {
self.pressed_to.is_pressed(self.input.is_high())
}
async fn wait_until_pressed_state(&mut self, pressed: bool) {
match (pressed, self.pressed_to) {
(true, PressedTo::Voltage) | (false, PressedTo::Ground) => {
self.input.wait_for_high().await;
}
(true, PressedTo::Ground) | (false, PressedTo::Voltage) => {
self.input.wait_for_low().await;
}
}
}
}
#[cfg(target_os = "none")]
impl device_envoy_core::button::Button for ButtonEsp<'_> {}
#[cfg(target_os = "none")]
#[doc(inline)]
pub use crate::button_watch;