Skip to main content

nanonis_rs/client/oscilloscope/
osci_2t.rs

1use super::super::NanonisClient;
2use crate::error::NanonisError;
3use crate::types::NanonisValue;
4
5impl NanonisClient {
6    /// Set the channels to display in the Oscilloscope 2-Channels
7    /// channel_a_index: 0-23, channel A signal index  
8    /// channel_b_index: 0-23, channel B signal index
9    pub fn osci2t_ch_set(
10        &mut self,
11        channel_a_index: i32,
12        channel_b_index: i32,
13    ) -> Result<(), NanonisError> {
14        self.quick_send(
15            "Osci2T.ChSet",
16            vec![
17                NanonisValue::I32(channel_a_index),
18                NanonisValue::I32(channel_b_index),
19            ],
20            vec!["i", "i"],
21            vec![],
22        )?;
23        Ok(())
24    }
25
26    /// Get the channels displayed in the Oscilloscope 2-Channels
27    /// Returns: (channel_a_index, channel_b_index)
28    pub fn osci2t_ch_get(&mut self) -> Result<(i32, i32), NanonisError> {
29        let result =
30            self.quick_send("Osci2T.ChGet", vec![], vec![], vec!["i", "i"])?;
31        if result.len() >= 2 {
32            let channel_a = result[0].as_i32()?;
33            let channel_b = result[1].as_i32()?;
34            Ok((channel_a, channel_b))
35        } else {
36            Err(NanonisError::Protocol(
37                "Invalid channel response".to_string(),
38            ))
39        }
40    }
41
42    /// Set the timebase in the Oscilloscope 2-Channels
43    /// Use osci2t_timebase_get() first to obtain available timebases, then use the index
44    pub fn osci2t_timebase_set(
45        &mut self,
46        timebase_index: u16,
47    ) -> Result<(), NanonisError> {
48        self.quick_send(
49            "Osci2T.TimebaseSet",
50            vec![NanonisValue::U16(timebase_index)],
51            vec!["H"],
52            vec![],
53        )?;
54        Ok(())
55    }
56
57    /// Get the timebase in the Oscilloscope 2-Channels
58    /// Returns: (timebase_index, timebases_array)
59    pub fn osci2t_timebase_get(&mut self) -> Result<(u16, Vec<f32>), NanonisError> {
60        let result = self.quick_send(
61            "Osci2T.TimebaseGet",
62            vec![],
63            vec![],
64            vec!["H", "i", "*f"],
65        )?;
66        if result.len() >= 3 {
67            let timebase_index = result[0].as_u16()?;
68            let timebases = result[2].as_f32_array()?.to_vec();
69            Ok((timebase_index, timebases))
70        } else {
71            Err(NanonisError::Protocol(
72                "Invalid timebase response".to_string(),
73            ))
74        }
75    }
76
77    /// Set the oversampling in the Oscilloscope 2-Channels
78    /// oversampling_index: 0=50 samples, 1=20, 2=10, 3=5, 4=2, 5=1 sample (no averaging)
79    pub fn osci2t_oversampl_set(
80        &mut self,
81        oversampling_index: u16,
82    ) -> Result<(), NanonisError> {
83        self.quick_send(
84            "Osci2T.OversamplSet",
85            vec![NanonisValue::U16(oversampling_index)],
86            vec!["H"],
87            vec![],
88        )?;
89        Ok(())
90    }
91
92    /// Get the oversampling in the Oscilloscope 2-Channels
93    /// Returns: oversampling index (0=50 samples, 1=20, 2=10, 3=5, 4=2, 5=1 sample)
94    pub fn osci2t_oversampl_get(&mut self) -> Result<u16, NanonisError> {
95        let result =
96            self.quick_send("Osci2T.OversamplGet", vec![], vec![], vec!["H"])?;
97        match result.first() {
98            Some(value) => Ok(value.as_u16()?),
99            None => Err(NanonisError::Protocol(
100                "No oversampling index returned".to_string(),
101            )),
102        }
103    }
104
105    /// Set the trigger configuration in the Oscilloscope 2-Channels
106    /// trigger_mode: 0 = Immediate, 1 = Level, 2 = Auto
107    /// trig_channel: trigger channel
108    /// trigger_slope: 0 = Falling, 1 = Rising
109    pub fn osci2t_trig_set(
110        &mut self,
111        trigger_mode: u16,
112        trig_channel: u16,
113        trigger_slope: u16,
114        trigger_level: f64,
115        trigger_hysteresis: f64,
116        trig_position: f64,
117    ) -> Result<(), NanonisError> {
118        self.quick_send(
119            "Osci2T.TrigSet",
120            vec![
121                NanonisValue::U16(trigger_mode),
122                NanonisValue::U16(trig_channel),
123                NanonisValue::U16(trigger_slope),
124                NanonisValue::F64(trigger_level),
125                NanonisValue::F64(trigger_hysteresis),
126                NanonisValue::F64(trig_position),
127            ],
128            vec!["H", "H", "H", "d", "d", "d"],
129            vec![],
130        )?;
131        Ok(())
132    }
133
134    /// Get the trigger configuration in the Oscilloscope 2-Channels
135    /// Returns: (trigger_mode, trig_channel, trigger_slope, trigger_level, trigger_hysteresis, trig_position)
136    pub fn osci2t_trig_get(
137        &mut self,
138    ) -> Result<(u16, u16, u16, f64, f64, f64), NanonisError> {
139        let result = self.quick_send(
140            "Osci2T.TrigGet",
141            vec![],
142            vec![],
143            vec!["H", "H", "H", "d", "d", "d"],
144        )?;
145        if result.len() >= 6 {
146            let trigger_mode = result[0].as_u16()?;
147            let trig_channel = result[1].as_u16()?;
148            let trigger_slope = result[2].as_u16()?;
149            let trigger_level = result[3].as_f64()?;
150            let trigger_hysteresis = result[4].as_f64()?;
151            let trig_position = result[5].as_f64()?;
152            Ok((
153                trigger_mode,
154                trig_channel,
155                trigger_slope,
156                trigger_level,
157                trigger_hysteresis,
158                trig_position,
159            ))
160        } else {
161            Err(NanonisError::Protocol(
162                "Invalid trigger configuration response".to_string(),
163            ))
164        }
165    }
166
167    /// Start the Oscilloscope 2-Channels
168    pub fn osci2t_run(&mut self) -> Result<(), NanonisError> {
169        self.quick_send("Osci2T.Run", vec![], vec![], vec![])?;
170        Ok(())
171    }
172
173    /// Get the graph data from the Oscilloscope 2-Channels
174    /// data_to_get: 0 = Current, 1 = Next trigger, 2 = Wait 2 triggers
175    /// Returns: (t0, dt, channel_a_data, channel_b_data)
176    pub fn osci2t_data_get(
177        &mut self,
178        data_to_get: u16,
179    ) -> Result<(f64, f64, Vec<f64>, Vec<f64>), NanonisError> {
180        let result = self.quick_send(
181            "Osci2T.DataGet",
182            vec![NanonisValue::U16(data_to_get)],
183            vec!["H"],
184            vec!["d", "d", "i", "*d", "i", "*d"],
185        )?;
186
187        if result.len() >= 6 {
188            let t0 = result[0].as_f64()?;
189            let dt = result[1].as_f64()?;
190            let channel_a_data = result[3].as_f64_array()?.to_vec();
191            let channel_b_data = result[5].as_f64_array()?.to_vec();
192            Ok((t0, dt, channel_a_data, channel_b_data))
193        } else {
194            Err(NanonisError::Protocol(
195                "Invalid oscilloscope 2T data response".to_string(),
196            ))
197        }
198    }
199}