scpi: add attenuator command

This commit is contained in:
occheung 2020-09-14 17:33:32 +08:00
parent 43ccb86697
commit f94528ae2a
1 changed files with 62 additions and 24 deletions

View File

@ -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<T: Device + UrukulTraits> Command<T> for Channel0SwitchCommand {
nquery!();
@ -270,14 +257,65 @@ impl<T:Device + UrukulTraits> Command<T> 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 => {
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))
}
_ => Err(Error::new(ErrorCode::IllegalParameterValue)),
}
}
impl<T:Device + UrukulTraits> Command<T> for Channel0AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, 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<T:Device + UrukulTraits> Command<T> for Channel1AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, 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<T:Device + UrukulTraits> Command<T> for Channel2AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, 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<T:Device + UrukulTraits> Command<T> for Channel3AttenuationCommand {
nquery!();
fn event(&self, context: &mut Context<T>, 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))
}
}