Move relay drivers from common to controller

This commit is contained in:
Tobias Reisinger 2024-05-05 23:46:38 +02:00
parent e9ea0b625d
commit 340c4e9f15
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
9 changed files with 193 additions and 33 deletions

31
src/drivers/gpio.rs Normal file
View file

@ -0,0 +1,31 @@
use rppal::gpio::{Gpio, OutputPin};
use crate::drivers::RelayDriver;
use crate::errors::EmgauwaControllerError;
pub struct GpioDriver {
pub gpio: OutputPin,
pub inverted: bool,
}
impl GpioDriver {
pub fn new(pin: u8, inverted: bool) -> Result<Self, EmgauwaControllerError> {
let gpio = Gpio::new()?.get(pin)?.into_output();
Ok(Self { gpio, inverted })
}
}
impl RelayDriver for GpioDriver {
fn set(&mut self, value: bool) -> Result<(), EmgauwaControllerError> {
if self.get_high(value) {
self.gpio.set_high();
} else {
self.gpio.set_low();
}
Ok(())
}
fn get_inverted(&self) -> bool {
self.inverted
}
}