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