From 07d3322c5a69c1ae9215bcf9d460db4b61f32002 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sun, 28 Apr 2024 01:38:57 +0200 Subject: [PATCH] Add null driver --- config/emgauwa-controller.pkl | 20 ++++++++++---------- config/lib/controller.pkl | 6 +++--- emgauwa-controller/src/driver.rs | 6 ++++-- emgauwa-controller/src/settings.rs | 18 +++++++++--------- emgauwa-lib/src/drivers/mod.rs | 4 +++- emgauwa-lib/src/drivers/null.rs | 26 ++++++++++++++++++++++++++ emgauwa-lib/src/drivers/piface.rs | 6 +++--- 7 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 emgauwa-lib/src/drivers/null.rs diff --git a/config/emgauwa-controller.pkl b/config/emgauwa-controller.pkl index 327c1bd..b0917f1 100644 --- a/config/emgauwa-controller.pkl +++ b/config/emgauwa-controller.pkl @@ -19,52 +19,52 @@ logging { relays { new { - driver = "gpio" + driver = "null" pin = 24 inverted = true } new { - driver = "gpio" + driver = "null" pin = 23 inverted = true } new { - driver = "gpio" + driver = "null" pin = 22 inverted = true } new { - driver = "gpio" + driver = "null" pin = 27 inverted = true } new { - driver = "gpio" + driver = "null" pin = 18 inverted = true } new { - driver = "gpio" + driver = "null" pin = 17 inverted = true } new { - driver = "gpio" + driver = "null" pin = 15 inverted = true } new { - driver = "gpio" + driver = "null" pin = 14 inverted = true } new { - driver = "piface" + driver = "null" pin = 1 inverted = false } new { - driver = "piface" + driver = "null" pin = 0 inverted = false } diff --git a/config/lib/controller.pkl b/config/lib/controller.pkl index 05a8efd..7533b98 100644 --- a/config/lib/controller.pkl +++ b/config/lib/controller.pkl @@ -9,8 +9,8 @@ permissions: common.PermissionsConfig logging: common.LoggingConfig class RelayConfig { - driver: "gpio" | "piface" - pin: Number - inverted: Boolean + driver: "null"|"gpio"|"piface" + pin: Number + inverted: Boolean } relays: Listing diff --git a/emgauwa-controller/src/driver.rs b/emgauwa-controller/src/driver.rs index ce54421..9df8d31 100644 --- a/emgauwa-controller/src/driver.rs +++ b/emgauwa-controller/src/driver.rs @@ -2,8 +2,9 @@ use serde::{Deserialize, Deserializer}; #[derive(Debug, Clone, Copy)] pub enum Driver { + Null, Gpio, - Piface, + PiFace, } impl<'de> Deserialize<'de> for Driver { @@ -12,8 +13,9 @@ impl<'de> Deserialize<'de> for Driver { D: Deserializer<'de>, { match String::deserialize(deserializer)?.as_str() { + "null" => Ok(Driver::Null), "gpio" => Ok(Driver::Gpio), - "piface" => Ok(Driver::Piface), + "piface" => Ok(Driver::PiFace), _ => Err(serde::de::Error::custom("invalid driver")), } } diff --git a/emgauwa-controller/src/settings.rs b/emgauwa-controller/src/settings.rs index 2eda813..3eead14 100644 --- a/emgauwa-controller/src/settings.rs +++ b/emgauwa-controller/src/settings.rs @@ -1,6 +1,5 @@ -use emgauwa_lib::drivers::{GpioDriver, PifaceDriver, RelayDriver}; use emgauwa_lib::errors::EmgauwaError; -use emgauwa_lib::settings; +use emgauwa_lib::{drivers, settings}; use rppal_pfd::PiFaceDigital; use serde_derive::Deserialize; @@ -78,7 +77,7 @@ impl Settings { pub fn relays_make_drivers( &self, pfd: &mut Option, - ) -> Result>, EmgauwaError> { + ) -> Result>, EmgauwaError> { let mut drivers = Vec::new(); for relay in &self.relays { drivers.push(relay.make_driver(pfd)?); @@ -91,14 +90,15 @@ impl Relay { pub fn make_driver( &self, pfd: &mut Option, - ) -> Result, EmgauwaError> { - let driver: Box = match self.driver { - Driver::Gpio => Box::new(GpioDriver::new(self.pin, self.inverted)?), - Driver::Piface => { + ) -> Result, EmgauwaError> { + let driver: Box = match self.driver { + Driver::Null => Box::new(drivers::NullDriver::new(self.pin)), + Driver::Gpio => Box::new(drivers::GpioDriver::new(self.pin, self.inverted)?), + Driver::PiFace => { 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) diff --git a/emgauwa-lib/src/drivers/mod.rs b/emgauwa-lib/src/drivers/mod.rs index 71b1cfa..abae75a 100644 --- a/emgauwa-lib/src/drivers/mod.rs +++ b/emgauwa-lib/src/drivers/mod.rs @@ -1,8 +1,10 @@ mod gpio; +mod null; mod piface; pub use gpio::GpioDriver; -pub use piface::PifaceDriver; +pub use null::NullDriver; +pub use piface::PiFaceDriver; use crate::errors::EmgauwaError; diff --git a/emgauwa-lib/src/drivers/null.rs b/emgauwa-lib/src/drivers/null.rs new file mode 100644 index 0000000..be4e8b9 --- /dev/null +++ b/emgauwa-lib/src/drivers/null.rs @@ -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 + } +} diff --git a/emgauwa-lib/src/drivers/piface.rs b/emgauwa-lib/src/drivers/piface.rs index 74c7857..7bc20da 100644 --- a/emgauwa-lib/src/drivers/piface.rs +++ b/emgauwa-lib/src/drivers/piface.rs @@ -5,11 +5,11 @@ use rppal_pfd::{ use crate::drivers::RelayDriver; use crate::errors::EmgauwaError; -pub struct PifaceDriver { +pub struct PiFaceDriver { pub pfd_pin: OutputPin, } -impl PifaceDriver { +impl PiFaceDriver { pub fn new(pin: u8, pfd: &Option) -> Result { let pfd = pfd.as_ref().ok_or(EmgauwaError::Hardware(String::from( "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> { if self.get_high(value) { self.pfd_pin.set_high().map_err(PiFaceDigitalError::from)?;