From f94528ae2a9ad9cca6a3d9ca96372f8b335f915a Mon Sep 17 00:00:00 2001 From: occheung Date: Mon, 14 Sep 2020 17:33:32 +0800 Subject: [PATCH] scpi: add attenuator command --- src/scpi.rs | 86 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/src/scpi.rs b/src/scpi.rs index 7efec61..9df1f55 100644 --- a/src/scpi.rs +++ b/src/scpi.rs @@ -7,26 +7,9 @@ use scpi::NumericValues; use core::convert::{TryFrom, TryInto}; use core::str; -use core::str::Utf8Error; -use scpi::ieee488::commands::*; -use scpi::scpi::commands::*; use scpi::{ - ieee488_cls, - ieee488_ese, - ieee488_esr, - ieee488_idn, - ieee488_opc, - ieee488_rst, - ieee488_sre, - ieee488_stb, - ieee488_tst, - ieee488_wai, nquery, - //Helpers qonly, - scpi_crate_version, - scpi_status, - scpi_system, }; use scpi::suffix::{Amplitude, Db}; use uom::si::frequency::{gigahertz, hertz, kilohertz, megahertz, Frequency}; @@ -147,6 +130,10 @@ pub struct Channel2SwitchCommand {} pub struct Channel3SwitchCommand {} pub struct ClockSourceCommand {} pub struct ClockDivisionCommand {} +pub struct Channel0AttenuationCommand {} +pub struct Channel1AttenuationCommand {} +pub struct Channel2AttenuationCommand {} +pub struct Channel3AttenuationCommand {} impl Command for Channel0SwitchCommand { nquery!(); @@ -270,17 +257,68 @@ impl Command for ClockDivisionCommand { .map_or(Err(Error::new(ErrorCode::IllegalParameterValue)), |token| token.try_into())?; trace!("Received master clock division factor: {}", div); - match div { - 1.0 | 2.0 | 4.0 => { - debug!("Set master clock division as {}", div); - context.device.set_clock_division(div as u8) - .map_err(|_| Error::new(ErrorCode::HardwareError)) - } - _ => Err(Error::new(ErrorCode::IllegalParameterValue)), + if div == 1.0 || div == 2.0 || div == 4.0 { + debug!("Set master clock division as {}", div); + context.device.set_clock_division(div as u8) + .map_err(|_| Error::new(ErrorCode::HardwareError)) + } else { + Err(Error::new(ErrorCode::IllegalParameterValue)) } } } +impl Command for Channel0AttenuationCommand { + nquery!(); + + fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()> { + let attenuation: f32 = args.next_data(false)? + .map_or(Err(Error::new(ErrorCode::IllegalParameterValue)), + |token| token.try_into())?; + trace!("Received channel 0 attenuation input: {}", attenuation); + context.device.set_channel_attenuation(0, attenuation) + .map_err(|_| Error::new(ErrorCode::HardwareError)) + } +} + +impl Command for Channel1AttenuationCommand { + nquery!(); + + fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()> { + let attenuation: f32 = args.next_data(false)? + .map_or(Err(Error::new(ErrorCode::IllegalParameterValue)), + |token| token.try_into())?; + trace!("Received channel 1 attenuation input: {}", attenuation); + context.device.set_channel_attenuation(1, attenuation) + .map_err(|_| Error::new(ErrorCode::HardwareError)) + } +} + +impl Command for Channel2AttenuationCommand { + nquery!(); + + fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()> { + let attenuation: f32 = args.next_data(false)? + .map_or(Err(Error::new(ErrorCode::IllegalParameterValue)), + |token| token.try_into())?; + trace!("Received channel 2 attenuation input: {}", attenuation); + context.device.set_channel_attenuation(2, attenuation) + .map_err(|_| Error::new(ErrorCode::HardwareError)) + } +} + +impl Command for Channel3AttenuationCommand { + nquery!(); + + fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()> { + let attenuation: f32 = args.next_data(false)? + .map_or(Err(Error::new(ErrorCode::IllegalParameterValue)), + |token| token.try_into())?; + trace!("Received channel 3 attenuation input: {}", attenuation); + context.device.set_channel_attenuation(3, attenuation) + .map_err(|_| Error::new(ErrorCode::HardwareError)) + } +} + /* * Implement "Device" trait from SCPI * TODO: Implement mandatory commands