instrument/classes/
counter.rs1use 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 Counter {
10 session: InstrumentSession,
11}
12
13impl Counter {
14 pub fn new(session: InstrumentSession) -> Self {
15 Self { session }
16 }
17
18 pub fn session(&self) -> &InstrumentSession {
19 &self.session
20 }
21
22 pub fn session_mut(&mut self) -> &mut InstrumentSession {
23 &mut self.session
24 }
25
26 fn dialect(&self) -> &'static instrument_core::DialectProfile {
27 self.session.dialect_for(InstrumentKind::Counter)
28 }
29
30 fn cmd(&self, key: &str, vars: &[(&str, String)], fallback: String) -> String {
31 dialect_io::try_formatted(self.dialect(), key, vars, fallback)
32 }
33
34 pub fn measure_frequency(&mut self) -> Result<f64> {
36 self.query_f64(dialect_io::try_command(
37 self.dialect(),
38 "measure_frequency",
39 scpi_commands::COUNTER_MEASURE_FREQUENCY,
40 ))
41 }
42
43 pub fn measure_period(&mut self) -> Result<f64> {
45 self.query_f64(dialect_io::try_command(
46 self.dialect(),
47 "measure_period",
48 scpi_commands::COUNTER_MEASURE_PERIOD,
49 ))
50 }
51
52 pub fn set_gate_time(&mut self, seconds: f64) -> Result<()> {
54 let cmd = self.cmd(
55 "gate_time",
56 &[("seconds", dialect_io::f64_text(seconds))],
57 scpi_commands::counter_gate_time(seconds),
58 );
59 self.session.scpi_mut().write(&cmd)
60 }
61
62 pub fn select_channel(&mut self, channel: u32) -> Result<()> {
64 let cmd = self.cmd(
65 "channel_select",
66 &[("channel", channel.to_string())],
67 scpi_commands::counter_channel_select(channel),
68 );
69 self.session.scpi_mut().write(&cmd)
70 }
71
72 pub fn reset_totalize(&mut self) -> Result<()> {
74 let cmd = dialect_io::try_command(
75 self.dialect(),
76 "reset_totalize",
77 scpi_commands::COUNTER_RESET_TOTALIZE,
78 );
79 self.session.scpi_mut().write(cmd)
80 }
81
82 pub fn read_totalize(&mut self) -> Result<f64> {
84 self.query_f64(dialect_io::try_command(
85 self.dialect(),
86 "read_totalize",
87 scpi_commands::COUNTER_READ_TOTALIZE,
88 ))
89 }
90
91 fn query_f64(&mut self, cmd: &str) -> Result<f64> {
92 let resp = self.session.scpi_mut().query(cmd)?;
93 ScpiSession::parse_f64(&resp)
94 }
95}