use rppal::gpio::{Gpio, InputPin, Level, Trigger};
use std::time::Duration;
#[derive(Debug)]
pub struct InputDevice {
pin: InputPin,
active_state: bool,
inactive_state: bool,
}
impl InputDevice {
pub fn new(pin: u8) -> InputDevice {
match Gpio::new() {
Err(e) => panic!("{:?}", e),
Ok(gpio) => match gpio.get(pin) {
Err(e) => panic!("{:?}", e),
Ok(pin) => InputDevice {
pin: pin.into_input_pulldown(),
active_state: true,
inactive_state: false,
},
},
}
}
pub fn new_with_pullup(pin: u8) -> InputDevice {
match Gpio::new() {
Err(e) => panic!("{:?}", e),
Ok(gpio) => match gpio.get(pin) {
Err(e) => panic!("{:?}", e),
Ok(pin) => InputDevice {
pin: pin.into_input_pullup(),
active_state: false,
inactive_state: true,
},
},
}
}
impl_device!();
impl_gpio_device!();
impl_io_device!();
}
macro_rules! impl_events_mixin {
() => {
fn wait_for(&mut self, timeout:Option<f32>, active: bool){
match timeout{
None =>
if active {
self.pin.set_interrupt(Trigger::RisingEdge).unwrap();
self.pin.poll_interrupt(true, None).unwrap();
}else{
self.pin.set_interrupt(Trigger::FallingEdge).unwrap();
self.pin.poll_interrupt(true, None).unwrap();
}
,
Some(n) => if active {
self.pin.set_interrupt(Trigger::RisingEdge).unwrap();
self.pin.poll_interrupt(true, Some(Duration::from_millis((n * 1000.0) as u64))).unwrap();
}else{
self.pin.set_interrupt(Trigger::FallingEdge).unwrap();
self.pin.poll_interrupt(true, Some(Duration::from_millis((n * 1000.0) as u64))).unwrap();
}
}
}
}
}
#[derive(Debug)]
pub struct DigitalInputDevice {
pin: InputPin,
active_state: bool,
inactive_state: bool,
bounce_time: Option<f32>,
}
impl DigitalInputDevice {
pub fn new(pin: u8) -> DigitalInputDevice {
match Gpio::new() {
Err(e) => panic!("{:?}", e),
Ok(gpio) => match gpio.get(pin) {
Err(e) => panic!("{:?}", e),
Ok(pin) => DigitalInputDevice {
pin: pin.into_input_pulldown(),
active_state: true,
inactive_state: false,
bounce_time: None,
},
},
}
}
pub fn new_with_pullup(pin: u8) -> DigitalInputDevice {
match Gpio::new() {
Err(e) => panic!("{:?}", e),
Ok(gpio) => match gpio.get(pin) {
Err(e) => panic!("{:?}", e),
Ok(pin) => DigitalInputDevice {
pin: pin.into_input_pullup(),
active_state: false,
inactive_state: true,
bounce_time: None,
},
},
}
}
impl_device!();
impl_gpio_device!();
impl_io_device!();
impl_events_mixin!();
pub fn wait_for_inactive(&mut self, timeout: Option<f32>) {
self.wait_for(timeout, false)
}
pub fn wait_for_active(&mut self, timeout: Option<f32>) {
self.wait_for(timeout, true)
}
}
pub struct Button {
pin: InputPin,
active_state: bool,
inactive_state: bool,
bounce_time: Option<f32>,
}
impl Button {
pub fn new(pin: u8) -> Button {
match Gpio::new() {
Err(e) => panic!("{:?}", e),
Ok(gpio) => match gpio.get(pin) {
Err(e) => panic!("{:?}", e),
Ok(pin) => Button {
pin: pin.into_input_pullup(),
active_state: false,
inactive_state: true,
bounce_time: None,
},
},
}
}
pub fn new_with_pulldown(pin: u8) -> Button {
match Gpio::new() {
Err(e) => panic!("{:?}", e),
Ok(gpio) => match gpio.get(pin) {
Err(e) => panic!("{:?}", e),
Ok(pin) => Button {
pin: pin.into_input_pulldown(),
active_state: true,
inactive_state: false,
bounce_time: None,
},
},
}
}
impl_device!();
impl_gpio_device!();
impl_io_device!();
impl_events_mixin!();
pub fn wait_for_release(&mut self, timeout: Option<f32>) {
self.wait_for(timeout, false)
}
pub fn wait_for_press(&mut self, timeout: Option<f32>) {
self.wait_for(timeout, true)
}
}