use alloc::vec::Vec;
use crate::operation::{
CommandCode, DeviceReply, Operation, OperationError, PayloadReader, PayloadWriter,
};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SetDamping {
pub seconds: f32,
}
impl SetDamping {
pub fn new(seconds: f32) -> Result<Self, OperationError> {
if !seconds.is_finite() || seconds < 0.0 {
return Err(OperationError::InvalidValue("damping value"));
}
Ok(Self { seconds })
}
}
impl Operation for SetDamping {
type Output = f32;
fn command(&self) -> CommandCode {
CommandCode::new(34)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
if !self.seconds.is_finite() || self.seconds < 0.0 {
return Err(OperationError::InvalidValue("damping value"));
}
writer.f32(self.seconds);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let value = reader.f32("actual damping value")?;
reader.finish()?;
Ok(value)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RangeValues {
pub unit: u8,
pub upper: f32,
pub lower: f32,
}
impl RangeValues {
pub fn new(unit: u8, lower: f32, upper: f32) -> Result<Self, OperationError> {
if !upper.is_finite() || !lower.is_finite() {
return Err(OperationError::InvalidValue("range limits"));
}
Ok(Self { unit, upper, lower })
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SetRange(pub RangeValues);
impl SetRange {
pub fn new(unit: u8, lower: f32, upper: f32) -> Result<Self, OperationError> {
Ok(Self(RangeValues::new(unit, lower, upper)?))
}
}
impl Operation for SetRange {
type Output = RangeValues;
fn command(&self) -> CommandCode {
CommandCode::new(35)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
if !self.0.upper.is_finite() || !self.0.lower.is_finite() {
return Err(OperationError::InvalidValue("range limits"));
}
writer.u8(self.0.unit);
writer.f32(self.0.upper);
writer.f32(self.0.lower);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let value = RangeValues {
unit: reader.u8("range unit")?,
upper: reader.f32("upper range limit")?,
lower: reader.f32("lower range limit")?,
};
reader.finish()?;
Ok(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActionCommand {
command: CommandCode,
data: Vec<u8>,
}
impl ActionCommand {
pub fn new(command: u8, data: impl Into<Vec<u8>>) -> Result<Self, OperationError> {
if !matches!(command, 36..=38 | 41..=43) {
return Err(OperationError::InvalidValue("action command number"));
}
let data = data.into();
let command = CommandCode::from(command);
if !crate::request_constraint(command).allows(data.len()) {
return Err(OperationError::RequestLength {
command: command.get(),
actual: data.len(),
});
}
Ok(Self { command, data })
}
}
impl Operation for ActionCommand {
type Output = DeviceReply;
fn command(&self) -> CommandCode {
self.command
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
writer.bytes(&self.data);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
Ok(reply.clone())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EepromControl(pub u8);
impl Operation for EepromControl {
type Output = u8;
fn command(&self) -> CommandCode {
CommandCode::new(39)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
writer.u8(self.0);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let value = reader.u8("memory control code")?;
reader.finish()?;
Ok(value)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FixedCurrent(pub f32);
impl FixedCurrent {
pub fn new(milliamps: f32) -> Result<Self, OperationError> {
if !milliamps.is_finite() {
return Err(OperationError::InvalidValue("fixed current"));
}
Ok(Self(milliamps))
}
}
impl Operation for FixedCurrent {
type Output = f32;
fn command(&self) -> CommandCode {
CommandCode::new(40)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
if !self.0.is_finite() {
return Err(OperationError::InvalidValue("fixed current"));
}
writer.f32(self.0);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let value = reader.f32("actual current")?;
reader.finish()?;
Ok(value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WritePrimaryUnit(pub u8);
impl Operation for WritePrimaryUnit {
type Output = u8;
fn command(&self) -> CommandCode {
CommandCode::new(44)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
writer.u8(self.0);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let value = reader.u8("primary-variable unit")?;
reader.finish()?;
Ok(value)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AnalogTrim {
command: CommandCode,
pub reference_current: f32,
}
impl AnalogTrim {
pub fn new(command: u8, reference_current: f32) -> Result<Self, OperationError> {
if !matches!(command, 45 | 46) || !reference_current.is_finite() {
return Err(OperationError::InvalidValue("current-output trim"));
}
Ok(Self {
command: CommandCode::from(command),
reference_current,
})
}
pub fn zero(reference_current: f32) -> Result<Self, OperationError> {
Self::new(45, reference_current)
}
pub fn gain(reference_current: f32) -> Result<Self, OperationError> {
Self::new(46, reference_current)
}
}
impl Operation for AnalogTrim {
type Output = f32;
fn command(&self) -> CommandCode {
self.command
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
writer.f32(self.reference_current);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let current = reader.f32("actual trim current")?;
reader.finish()?;
Ok(current)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WriteTransferFunction(pub u8);
impl Operation for WriteTransferFunction {
type Output = u8;
fn command(&self) -> CommandCode {
CommandCode::new(47)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
writer.u8(self.0);
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let function = reader.u8("transfer function")?;
reader.finish()?;
Ok(function)
}
}