Commit 7da0d779 authored by Groff, Todd's avatar Groff, Todd
Browse files

implemented key module

parent 0570f48a
......@@ -7,7 +7,7 @@ use crate::ModbusFloat;
type Error = crate::Error;
pub type ControlMode = enums::OffAutoManual;
pub use enums::OffAutoManual as ControlMode;
pub enum HeatAlgorithm {
PID,
......@@ -85,7 +85,7 @@ impl TryFrom<u16> for AutotuneAggressiveness {
}
}
pub type Autotune = enums::NoYes;
pub use enums::NoYes as Autotune;
pub enum CoolOutputCurve {
Off,
......@@ -114,7 +114,7 @@ impl TryFrom<u16> for CoolOutputCurve {
}
}
pub type OpenLoopDetectEnable = enums::NoYes;
pub use enums::NoYes as OpenLoopDetectEnable;
pub enum ControlLoopError {
None,
......@@ -197,7 +197,7 @@ impl TryFrom<u16> for AutoToManualPower {
}
}
// These are the same enum
pub type InputErrorPower = AutoToManualPower;
pub use AutoToManualPower as InputErrorPower;
pub enum RampAction {
Off,
......@@ -253,9 +253,9 @@ impl TryFrom<u16> for RampScale {
}
}
pub type RemoteSetpoint = enums::NoYes;
pub type RemoteSetpointType = enums::AutoManual;
pub type ControlModeActive = enums::OffAutoManual;
pub use enums::NoYes as RemoteSetpoint;
pub use enums::AutoManual as RemoteSetpointType;
pub use enums::OffAutoManual as ControlModeActive;
pub enum ControlAction {
Off,
......@@ -365,7 +365,7 @@ impl TryFrom<u16> for PidSetCrossover {
}
}
pub type TruTuneEnable = enums::NoYes;
pub use enums::NoYes as TruTuneEnable;
pub enum ProfileEndAction {
User,
......@@ -552,7 +552,7 @@ impl Parameter {
}
pub struct ControlLoop {
modbus_client: Arc<Mutex<Client>>,
modbus_client: Arc<Mutex<modbus::tcp::Transport>>,
id: u16,
}
impl crate::F4T {
......@@ -562,6 +562,10 @@ impl crate::F4T {
}
impl ControlLoop {
pub fn id(&self) -> u16 {
self.id
}
pub fn get_control_mode(&mut self) -> Result<ControlMode, Error> {
let repr = self.modbus_client.lock().unwrap().read_input_registers(
Parameter::ControlMode.addr_for(&self),
......
......@@ -75,4 +75,28 @@ impl TryFrom<u16> for NoYes {
_ => return Err(Error::InvalidCode)
})
}
}
pub enum OnOff {
On,
Off,
}
impl Into<u16> for OnOff {
fn into(self) -> u16 {
match self {
OnOff::On => 63,
OnOff::Off => 62,
}
}
}
impl TryFrom<u16> for OnOff {
type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Ok(match value {
63 => OnOff::On,
62 => OnOff::Off,
_ => return Err(Error::InvalidCode)
})
}
}
\ No newline at end of file
use std::convert::TryFrom;
use std::sync::{Arc, Mutex};
use crate::enums;
use crate::ModbusFloat;
use modbus::Client;
type Error = crate::Error;
pub use enums::OnOff as InputState;
pub enum Function {
Momentary,
Toggle,
OnPulse,
}
impl Into<u16> for Function {
fn into(self) -> u16 {
match self {
Self::Momentary => 1714,
Self::Toggle => 1713,
Self::OnPulse => 1471,
}
}
}
impl TryFrom<u16> for Function {
type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Ok(match value {
1714 => Self::Momentary,
1713 => Self::Toggle,
1471 => Self::OnPulse,
_ => return Err(Error::InvalidCode)
})
}
}
pub enum KeyAction {
Up,
Down,
}
impl Into<u16> for KeyAction {
fn into(self) -> u16 {
match self {
KeyAction::Up => 1456,
KeyAction::Down => 1457,
}
}
}
impl TryFrom<u16> for KeyAction {
type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Ok(match value {
1456 => KeyAction::Up,
1457 => KeyAction::Down,
_ => return Err(Error::InvalidCode)
})
}
}
pub enum KeyError {
None,
Open,
Shorted,
MeasurementError,
BadCalibrationData,
AmbientError,
RTDError,
Fail,
MathError,
NotSourced,
Stale,
}
impl Into<u16> for KeyError {
fn into(self) -> u16 {
match self {
KeyError::None => 61,
KeyError::Open => 65,
KeyError::Shorted => 127,
KeyError::MeasurementError => 140,
KeyError::BadCalibrationData => 139,
KeyError::AmbientError => 9,
KeyError::RTDError => 141,
KeyError::Fail => 32,
KeyError::MathError => 1423,
KeyError::NotSourced => 246,
KeyError::Stale => 1617,
}
}
}
impl TryFrom<u16> for KeyError {
type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Ok(match value {
61 => KeyError::None,
65 => KeyError::Open,
127 => KeyError::Shorted,
140 => KeyError::MeasurementError,
139 => KeyError::BadCalibrationData,
9 => KeyError::AmbientError,
141 => KeyError::RTDError,
32 => KeyError::Fail,
1423 => KeyError::MathError,
246 => KeyError::NotSourced,
1617 => KeyError::Stale,
_ => return Err(Error::InvalidCode)
})
}
}
pub enum Parameter {
Function,
Time,
InputState,
KeyAction,
KeyError,
}
impl Parameter {
pub fn addr_for(self, key: &Key) -> u16 {
let offset = (key.id() - 1) * 20;
let base_addr = match self {
Parameter::Function => 6840,
Parameter::Time => 6842,
Parameter::InputState => 6844,
Parameter::KeyAction => 6850,
Parameter::KeyError => 6852,
};
base_addr + offset
}
}
pub struct Key {
modbus_client: Arc<Mutex<modbus::tcp::Transport>>,
id: u16
}
impl crate::F4T {
pub fn get_key(&self, id: u16) -> Key {
Key { modbus_client: self.modbus_client.clone(), id, }
}
}
impl Key {
pub fn id(&self) -> u16 {
self.id
}
pub fn get_function(&mut self) -> Result<Function, Error> {
let repr = self.modbus_client.lock().unwrap().read_input_registers(
Parameter::Function.addr_for(&self),
1
)?[0];
let function = Function::try_from(repr)?;
Ok(function)
}
pub fn set_function(&mut self, function: Function) -> Result<(), Error> {
self.modbus_client.lock().unwrap().write_single_register(
Parameter::Function.addr_for(&self),
function.into()
)?;
Ok(())
}
pub fn get_time(&mut self) -> Result<f32, Error> {
let registers = self.modbus_client.lock().unwrap().read_input_registers(
Parameter::Time.addr_for(&self),
2,
)?;
let value = f32::from_registers([registers[0], registers[1]]);
Ok(value)
}
pub fn set_time(&mut self, time: f32) -> Result<(), Error> {
let registers = time.to_registers();
self.modbus_client.lock().unwrap().write_multiple_registers(
Parameter::Time.addr_for(&self),
&registers,
)?;
Ok(())
}
pub fn get_input_state(&mut self) -> Result<InputState, Error> {
let repr = self.modbus_client.lock().unwrap().read_input_registers(
Parameter::InputState.addr_for(&self),
1
)?[0];
let function = InputState::try_from(repr)?;
Ok(function)
}
pub fn get_key_action(&mut self) -> Result<KeyAction, Error> {
let repr = self.modbus_client.lock().unwrap().read_input_registers(
Parameter::KeyAction.addr_for(&self),
1
)?[0];
let key_action = KeyAction::try_from(repr)?;
Ok(key_action)
}
pub fn set_key_action(&mut self, key_action: KeyAction) -> Result<(), Error> {
self.modbus_client.lock().unwrap().write_single_register(
Parameter::KeyAction.addr_for(&self),
key_action.into()
)?;
Ok(())
}
pub fn get_error(&mut self) -> Result<KeyError, Error> {
let repr = self.modbus_client.lock().unwrap().read_input_registers(
Parameter::KeyError.addr_for(&self),
1
)?[0];
let error = KeyError::try_from(repr)?;
Ok(error)
}
}
pub mod variable;
pub mod universal_input;
pub mod rtc;
pub mod control_loop;
pub mod key;
pub mod rtc;
pub mod universal_input;
pub mod variable;
mod enums;
use std::convert::TryFrom;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment