1use super::dialect_io;
2use instrument_core::error::Result;
3use instrument_core::kind::InstrumentKind;
4use instrument_core::scpi::ScpiSession;
5use instrument_core::scpi_commands;
6use instrument_core::InstrumentSession;
7
8pub struct Dmm {
10 session: InstrumentSession,
11}
12
13impl Dmm {
14 pub fn new(session: InstrumentSession) -> Self {
15 Self { session }
16 }
17
18 pub fn from_session(_session: &InstrumentSession) -> Result<Self> {
19 Err(instrument_core::Error::Unsupported(
20 "use SessionPool for shared session views",
21 ))
22 }
23
24 pub fn session(&self) -> &InstrumentSession {
25 &self.session
26 }
27
28 pub fn session_mut(&mut self) -> &mut InstrumentSession {
29 &mut self.session
30 }
31
32 fn dialect(&self) -> &'static instrument_core::DialectProfile {
33 self.session.dialect_for(InstrumentKind::Dmm)
34 }
35
36 fn cmd(&self, key: &str, vars: &[(&str, String)], fallback: String) -> String {
37 dialect_io::try_formatted(self.dialect(), key, vars, fallback)
38 }
39
40 pub fn measure_voltage_dc(&mut self, range: Option<f64>) -> Result<f64> {
42 let cmd = self.cmd(
43 "measure_voltage_dc",
44 &dialect_io::range_vars(range),
45 scpi_commands::dmm_measure_voltage_dc(range),
46 );
47 self.query_f64(&cmd)
48 }
49
50 pub fn measure_voltage_ac(&mut self, range: Option<f64>) -> Result<f64> {
52 let cmd = self.cmd(
53 "measure_voltage_ac",
54 &dialect_io::range_vars(range),
55 scpi_commands::dmm_measure_voltage_ac(range),
56 );
57 self.query_f64(&cmd)
58 }
59
60 pub fn measure_current_dc(&mut self, range: Option<f64>) -> Result<f64> {
62 let cmd = self.cmd(
63 "measure_current_dc",
64 &dialect_io::range_vars(range),
65 scpi_commands::dmm_measure_current_dc(range),
66 );
67 self.query_f64(&cmd)
68 }
69
70 pub fn measure_current_ac(&mut self, range: Option<f64>) -> Result<f64> {
72 let cmd = self.cmd(
73 "measure_current_ac",
74 &dialect_io::range_vars(range),
75 scpi_commands::dmm_measure_current_ac(range),
76 );
77 self.query_f64(&cmd)
78 }
79
80 pub fn measure_resistance(&mut self, range: Option<f64>) -> Result<f64> {
82 self.measure_resistance_2wire(range)
83 }
84
85 pub fn measure_resistance_2wire(&mut self, range: Option<f64>) -> Result<f64> {
87 let cmd = self.cmd(
88 "measure_resistance_2w",
89 &dialect_io::range_vars(range),
90 scpi_commands::dmm_measure_resistance_2wire(range),
91 );
92 self.query_f64(&cmd)
93 }
94
95 pub fn measure_resistance_4wire(&mut self, range: Option<f64>) -> Result<f64> {
97 let cmd = self.cmd(
98 "measure_resistance_4w",
99 &dialect_io::range_vars(range),
100 scpi_commands::dmm_measure_resistance_4wire(range),
101 );
102 self.query_f64(&cmd)
103 }
104
105 pub fn measure_temperature(&mut self, range: Option<f64>) -> Result<f64> {
107 let cmd = self.cmd(
108 "measure_temperature",
109 &dialect_io::range_vars(range),
110 scpi_commands::dmm_measure_temperature(range),
111 );
112 self.query_f64(&cmd)
113 }
114
115 pub fn configure_voltage_dc(
117 &mut self,
118 range: Option<f64>,
119 resolution: Option<f64>,
120 ) -> Result<()> {
121 let cmd = self.cmd(
122 "configure_voltage_dc",
123 &dialect_io::range_resolution_vars(range, resolution),
124 scpi_commands::dmm_configure_voltage_dc(range, resolution),
125 );
126 self.session.scpi_mut().write(&cmd)
127 }
128
129 pub fn configure_voltage_ac(
131 &mut self,
132 range: Option<f64>,
133 resolution: Option<f64>,
134 ) -> Result<()> {
135 let cmd = self.cmd(
136 "configure_voltage_ac",
137 &dialect_io::range_resolution_vars(range, resolution),
138 scpi_commands::dmm_configure_voltage_ac(range, resolution),
139 );
140 self.session.scpi_mut().write(&cmd)
141 }
142
143 pub fn configure_current_dc(
145 &mut self,
146 range: Option<f64>,
147 resolution: Option<f64>,
148 ) -> Result<()> {
149 let cmd = self.cmd(
150 "configure_current_dc",
151 &dialect_io::range_resolution_vars(range, resolution),
152 scpi_commands::dmm_configure_current_dc(range, resolution),
153 );
154 self.session.scpi_mut().write(&cmd)
155 }
156
157 pub fn configure_current_ac(
159 &mut self,
160 range: Option<f64>,
161 resolution: Option<f64>,
162 ) -> Result<()> {
163 let cmd = self.cmd(
164 "configure_current_ac",
165 &dialect_io::range_resolution_vars(range, resolution),
166 scpi_commands::dmm_configure_current_ac(range, resolution),
167 );
168 self.session.scpi_mut().write(&cmd)
169 }
170
171 pub fn configure_resistance(
173 &mut self,
174 range: Option<f64>,
175 resolution: Option<f64>,
176 ) -> Result<()> {
177 let cmd = self.cmd(
178 "configure_resistance",
179 &dialect_io::range_resolution_vars(range, resolution),
180 scpi_commands::dmm_configure_resistance(range, resolution),
181 );
182 self.session.scpi_mut().write(&cmd)
183 }
184
185 pub fn configure_resistance_4wire(
187 &mut self,
188 range: Option<f64>,
189 resolution: Option<f64>,
190 ) -> Result<()> {
191 let cmd = self.cmd(
192 "configure_resistance_4w",
193 &dialect_io::range_resolution_vars(range, resolution),
194 scpi_commands::dmm_configure_resistance_4wire(range, resolution),
195 );
196 self.session.scpi_mut().write(&cmd)
197 }
198
199 pub fn initiate(&mut self) -> Result<()> {
201 let cmd = dialect_io::try_command(self.dialect(), "initiate", scpi_commands::DMM_INITIATE);
202 self.session.scpi_mut().write(cmd)
203 }
204
205 pub fn fetch(&mut self) -> Result<f64> {
207 self.query_f64(dialect_io::try_command(
208 self.dialect(),
209 "fetch",
210 scpi_commands::DMM_FETCH,
211 ))
212 }
213
214 pub fn read(&mut self) -> Result<f64> {
216 self.query_f64(dialect_io::try_command(
217 self.dialect(),
218 "read",
219 scpi_commands::DMM_READ,
220 ))
221 }
222
223 pub fn software_trigger(&mut self) -> Result<()> {
225 let cmd = dialect_io::try_command(
226 self.dialect(),
227 "software_trigger",
228 scpi_commands::DMM_SOFTWARE_TRIGGER,
229 );
230 self.session.scpi_mut().write(cmd)
231 }
232
233 fn query_f64(&mut self, cmd: &str) -> Result<f64> {
234 let resp = self.session.scpi_mut().query(cmd)?;
235 ScpiSession::parse_f64(&resp)
236 }
237}