2024-06-16 16:16:45 +02:00
|
|
|
//! # API for the REB1 button
|
|
|
|
//!
|
|
|
|
//! ## Examples
|
|
|
|
//!
|
2024-07-04 17:10:01 +02:00
|
|
|
//! - [Button Blinky with IRQs](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/blinky-button-irq.rs)
|
|
|
|
//! - [Button Blinky with IRQs and RTIC](https://egit.irs.uni-stuttgart.de/rust/va108xx-rs/src/branch/main/vorago-reb1/examples/blinky-button-rtic.rs)
|
2024-06-16 16:16:45 +02:00
|
|
|
use embedded_hal::digital::InputPin;
|
|
|
|
use va108xx_hal::{
|
|
|
|
gpio::{FilterClkSel, FilterType, InputFloating, InterruptEdge, InterruptLevel, Pin, PA11},
|
2025-02-11 10:18:32 +01:00
|
|
|
pac, InterruptConfig,
|
2024-06-16 16:16:45 +02:00
|
|
|
};
|
|
|
|
|
2025-02-13 14:50:00 +01:00
|
|
|
#[derive(Debug)]
|
2024-06-16 16:16:45 +02:00
|
|
|
pub struct Button {
|
|
|
|
button: Pin<PA11, InputFloating>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Button {
|
|
|
|
pub fn new(pin: Pin<PA11, InputFloating>) -> Button {
|
|
|
|
Button { button: pin }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn pressed(&mut self) -> bool {
|
|
|
|
self.button.is_low().ok().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn released(&mut self) -> bool {
|
|
|
|
self.button.is_high().ok().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures an IRQ on edge.
|
2025-02-11 10:18:32 +01:00
|
|
|
pub fn configure_edge_interrupt(
|
|
|
|
&mut self,
|
2024-06-16 16:16:45 +02:00
|
|
|
edge_type: InterruptEdge,
|
2025-02-11 10:18:32 +01:00
|
|
|
irq_cfg: InterruptConfig,
|
2024-06-16 16:16:45 +02:00
|
|
|
syscfg: Option<&mut pac::Sysconfig>,
|
|
|
|
irqsel: Option<&mut pac::Irqsel>,
|
2025-02-11 10:18:32 +01:00
|
|
|
) {
|
|
|
|
self.button
|
|
|
|
.configure_edge_interrupt(edge_type, irq_cfg, syscfg, irqsel);
|
2024-06-16 16:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures an IRQ on level.
|
2025-02-11 10:18:32 +01:00
|
|
|
pub fn configure_level_interrupt(
|
|
|
|
&mut self,
|
2024-06-16 16:16:45 +02:00
|
|
|
level: InterruptLevel,
|
2025-02-11 10:18:32 +01:00
|
|
|
irq_cfg: InterruptConfig,
|
2024-06-16 16:16:45 +02:00
|
|
|
syscfg: Option<&mut pac::Sysconfig>,
|
|
|
|
irqsel: Option<&mut pac::Irqsel>,
|
2025-02-11 10:18:32 +01:00
|
|
|
) {
|
|
|
|
self.button
|
|
|
|
.configure_level_interrupt(level, irq_cfg, syscfg, irqsel);
|
2024-06-16 16:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configures a filter on the button. This can be useful for debouncing the switch.
|
|
|
|
///
|
|
|
|
/// Please note that you still have to set a clock divisor yourself using the
|
|
|
|
/// [`va108xx_hal::clock::set_clk_div_register`] function in order for this to work.
|
2025-02-11 10:18:32 +01:00
|
|
|
pub fn configure_filter_type(&mut self, filter: FilterType, clksel: FilterClkSel) {
|
|
|
|
self.button.configure_filter_type(filter, clksel);
|
2024-06-16 16:16:45 +02:00
|
|
|
}
|
|
|
|
}
|