use std::fmt;
use rppal::gpio::{Gpio, InputPin};
pub const GPIO_TOUCH_A: u8 = 21;
pub const GPIO_TOUCH_B: u8 = 20;
pub const GPIO_TOUCH_C: u8 = 16;
#[derive(Debug)]
pub struct Button {
bcm_pin: u8,
pin: InputPin,
}
impl Button {
pub fn new(bcm_pin: u8) -> Result<Button, Error> {
let gpio = Gpio::new()?;
let pin = gpio.get(bcm_pin)?.into_input();
Ok(Self {
bcm_pin,
pin,
})
}
pub fn is_pressed(&mut self) -> bool {
!self.pin.is_high()
}
}
pub struct Buttons {
pub a : Button,
pub b: Button,
pub c: Button,
}
impl Buttons {
pub fn new() -> Result<Buttons, Error> {
Ok(Self {
a: Button::new(GPIO_TOUCH_A)?,
b: Button::new(GPIO_TOUCH_B)?,
c: Button::new(GPIO_TOUCH_C)?,
})
}
}
#[derive(Debug)]
pub enum Error {
Gpio(rppal::gpio::Error),
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &*self {
Error::Gpio(err) => write!(f, "Gpio error: {}", &err),
}
}
}
impl From<rppal::gpio::Error> for Error {
fn from(err: rppal::gpio::Error) -> Error {
Error::Gpio(err)
}
}