35 lines
653 B
Rust
35 lines
653 B
Rust
use rppal::gpio::{Gpio, OutputPin};
|
|
|
|
use crate::drivers::RelayDriver;
|
|
use crate::errors::EmgauwaError;
|
|
|
|
pub struct GpioDriver {
|
|
pub gpio: OutputPin,
|
|
pub inverted: bool,
|
|
}
|
|
|
|
impl GpioDriver {
|
|
pub fn new(pin: u8, inverted: bool) -> Result<Self, EmgauwaError> {
|
|
let gpio = Gpio::new()?.get(pin)?.into_output();
|
|
Ok(Self { gpio, inverted })
|
|
}
|
|
}
|
|
|
|
impl RelayDriver for GpioDriver {
|
|
fn set(&mut self, value: bool) -> Result<(), EmgauwaError> {
|
|
if self.get_high(value) {
|
|
self.gpio.set_high();
|
|
} else {
|
|
self.gpio.set_low();
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn get_pin(&self) -> u8 {
|
|
self.gpio.pin()
|
|
}
|
|
|
|
fn get_inverted(&self) -> bool {
|
|
self.inverted
|
|
}
|
|
}
|