Add null driver
This commit is contained in:
parent
4ed1cd3182
commit
07d3322c5a
7 changed files with 58 additions and 28 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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<RelayConfig>
|
||||
|
|
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<PiFaceDigital>,
|
||||
) -> Result<Vec<Box<dyn RelayDriver>>, EmgauwaError> {
|
||||
) -> Result<Vec<Box<dyn drivers::RelayDriver>>, 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<PiFaceDigital>,
|
||||
) -> Result<Box<dyn RelayDriver>, EmgauwaError> {
|
||||
let driver: Box<dyn RelayDriver> = match self.driver {
|
||||
Driver::Gpio => Box::new(GpioDriver::new(self.pin, self.inverted)?),
|
||||
Driver::Piface => {
|
||||
) -> Result<Box<dyn drivers::RelayDriver>, EmgauwaError> {
|
||||
let driver: Box<dyn drivers::RelayDriver> = 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)
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
26
emgauwa-lib/src/drivers/null.rs
Normal file
26
emgauwa-lib/src/drivers/null.rs
Normal 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
|
||||
}
|
||||
}
|
|
@ -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<PiFaceDigital>) -> Result<Self, EmgauwaError> {
|
||||
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)?;
|
||||
|
|
Loading…
Reference in a new issue