Add null driver

This commit is contained in:
Tobias Reisinger 2024-04-28 01:38:57 +02:00
parent 4ed1cd3182
commit 07d3322c5a
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
7 changed files with 58 additions and 28 deletions

View file

@ -19,52 +19,52 @@ logging {
relays { relays {
new { new {
driver = "gpio" driver = "null"
pin = 24 pin = 24
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 23 pin = 23
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 22 pin = 22
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 27 pin = 27
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 18 pin = 18
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 17 pin = 17
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 15 pin = 15
inverted = true inverted = true
} }
new { new {
driver = "gpio" driver = "null"
pin = 14 pin = 14
inverted = true inverted = true
} }
new { new {
driver = "piface" driver = "null"
pin = 1 pin = 1
inverted = false inverted = false
} }
new { new {
driver = "piface" driver = "null"
pin = 0 pin = 0
inverted = false inverted = false
} }

View file

@ -9,7 +9,7 @@ permissions: common.PermissionsConfig
logging: common.LoggingConfig logging: common.LoggingConfig
class RelayConfig { class RelayConfig {
driver: "gpio" | "piface" driver: "null"|"gpio"|"piface"
pin: Number pin: Number
inverted: Boolean inverted: Boolean
} }

View file

@ -2,8 +2,9 @@ use serde::{Deserialize, Deserializer};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Driver { pub enum Driver {
Null,
Gpio, Gpio,
Piface, PiFace,
} }
impl<'de> Deserialize<'de> for Driver { impl<'de> Deserialize<'de> for Driver {
@ -12,8 +13,9 @@ impl<'de> Deserialize<'de> for Driver {
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
match String::deserialize(deserializer)?.as_str() { match String::deserialize(deserializer)?.as_str() {
"null" => Ok(Driver::Null),
"gpio" => Ok(Driver::Gpio), "gpio" => Ok(Driver::Gpio),
"piface" => Ok(Driver::Piface), "piface" => Ok(Driver::PiFace),
_ => Err(serde::de::Error::custom("invalid driver")), _ => Err(serde::de::Error::custom("invalid driver")),
} }
} }

View file

@ -1,6 +1,5 @@
use emgauwa_lib::drivers::{GpioDriver, PifaceDriver, RelayDriver};
use emgauwa_lib::errors::EmgauwaError; use emgauwa_lib::errors::EmgauwaError;
use emgauwa_lib::settings; use emgauwa_lib::{drivers, settings};
use rppal_pfd::PiFaceDigital; use rppal_pfd::PiFaceDigital;
use serde_derive::Deserialize; use serde_derive::Deserialize;
@ -78,7 +77,7 @@ impl Settings {
pub fn relays_make_drivers( pub fn relays_make_drivers(
&self, &self,
pfd: &mut Option<PiFaceDigital>, pfd: &mut Option<PiFaceDigital>,
) -> Result<Vec<Box<dyn RelayDriver>>, EmgauwaError> { ) -> Result<Vec<Box<dyn drivers::RelayDriver>>, EmgauwaError> {
let mut drivers = Vec::new(); let mut drivers = Vec::new();
for relay in &self.relays { for relay in &self.relays {
drivers.push(relay.make_driver(pfd)?); drivers.push(relay.make_driver(pfd)?);
@ -91,14 +90,15 @@ impl Relay {
pub fn make_driver( pub fn make_driver(
&self, &self,
pfd: &mut Option<PiFaceDigital>, pfd: &mut Option<PiFaceDigital>,
) -> Result<Box<dyn RelayDriver>, EmgauwaError> { ) -> Result<Box<dyn drivers::RelayDriver>, EmgauwaError> {
let driver: Box<dyn RelayDriver> = match self.driver { let driver: Box<dyn drivers::RelayDriver> = match self.driver {
Driver::Gpio => Box::new(GpioDriver::new(self.pin, self.inverted)?), Driver::Null => Box::new(drivers::NullDriver::new(self.pin)),
Driver::Piface => { Driver::Gpio => Box::new(drivers::GpioDriver::new(self.pin, self.inverted)?),
Driver::PiFace => {
if pfd.is_none() { if pfd.is_none() {
*pfd = Some(PifaceDriver::init_piface()?); *pfd = Some(drivers::PiFaceDriver::init_piface()?);
} }
Box::new(PifaceDriver::new(self.pin, pfd)?) Box::new(drivers::PiFaceDriver::new(self.pin, pfd)?)
} }
}; };
Ok(driver) Ok(driver)

View file

@ -1,8 +1,10 @@
mod gpio; mod gpio;
mod null;
mod piface; mod piface;
pub use gpio::GpioDriver; pub use gpio::GpioDriver;
pub use piface::PifaceDriver; pub use null::NullDriver;
pub use piface::PiFaceDriver;
use crate::errors::EmgauwaError; use crate::errors::EmgauwaError;

View file

@ -0,0 +1,26 @@
use crate::drivers::RelayDriver;
use crate::errors::EmgauwaError;
pub struct NullDriver {
pub pin: u8,
}
impl NullDriver {
pub fn new(pin: u8) -> Self {
Self { pin }
}
}
impl RelayDriver for NullDriver {
fn set(&mut self, _value: bool) -> Result<(), EmgauwaError> {
Ok(())
}
fn get_pin(&self) -> u8 {
self.pin
}
fn get_inverted(&self) -> bool {
false
}
}

View file

@ -5,11 +5,11 @@ use rppal_pfd::{
use crate::drivers::RelayDriver; use crate::drivers::RelayDriver;
use crate::errors::EmgauwaError; use crate::errors::EmgauwaError;
pub struct PifaceDriver { pub struct PiFaceDriver {
pub pfd_pin: OutputPin, pub pfd_pin: OutputPin,
} }
impl PifaceDriver { impl PiFaceDriver {
pub fn new(pin: u8, pfd: &Option<PiFaceDigital>) -> Result<Self, EmgauwaError> { pub fn new(pin: u8, pfd: &Option<PiFaceDigital>) -> Result<Self, EmgauwaError> {
let pfd = pfd.as_ref().ok_or(EmgauwaError::Hardware(String::from( let pfd = pfd.as_ref().ok_or(EmgauwaError::Hardware(String::from(
"PiFaceDigital not initialized", "PiFaceDigital not initialized",
@ -32,7 +32,7 @@ impl PifaceDriver {
} }
} }
impl RelayDriver for PifaceDriver { impl RelayDriver for PiFaceDriver {
fn set(&mut self, value: bool) -> Result<(), EmgauwaError> { fn set(&mut self, value: bool) -> Result<(), EmgauwaError> {
if self.get_high(value) { if self.get_high(value) {
self.pfd_pin.set_high().map_err(PiFaceDigitalError::from)?; self.pfd_pin.set_high().map_err(PiFaceDigitalError::from)?;