1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
use super::NanonisClient;
use crate::error::NanonisError;
use crate::types::NanonisValue;
/// Trigger mode for PLL signal analyzer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PLLTriggerMode {
/// No change
#[default]
NoChange = 0,
/// Immediate trigger
Immediate = 1,
/// Level-based trigger
Level = 2,
}
impl From<PLLTriggerMode> for u16 {
fn from(m: PLLTriggerMode) -> Self {
m as u16
}
}
impl TryFrom<u16> for PLLTriggerMode {
type Error = NanonisError;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
0 => Ok(PLLTriggerMode::NoChange),
1 => Ok(PLLTriggerMode::Immediate),
2 => Ok(PLLTriggerMode::Level),
_ => Err(NanonisError::Protocol(format!(
"Invalid PLLTriggerMode value: {}",
value
))),
}
}
}
/// Trigger slope for PLL signal analyzer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PLLTriggerSlope {
/// No change
#[default]
NoChange = 0,
/// Rising edge
Rising = 1,
/// Falling edge
Falling = 2,
}
impl From<PLLTriggerSlope> for u16 {
fn from(s: PLLTriggerSlope) -> Self {
s as u16
}
}
impl TryFrom<u16> for PLLTriggerSlope {
type Error = NanonisError;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
0 => Ok(PLLTriggerSlope::NoChange),
1 => Ok(PLLTriggerSlope::Rising),
2 => Ok(PLLTriggerSlope::Falling),
_ => Err(NanonisError::Protocol(format!(
"Invalid PLLTriggerSlope value: {}",
value
))),
}
}
}
/// Arming mode for trigger.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ArmingMode {
/// No change
#[default]
NoChange = 0,
/// Manual rearm
Manual = 1,
/// Automatic rearm
Automatic = 2,
}
impl From<ArmingMode> for u16 {
fn from(m: ArmingMode) -> Self {
m as u16
}
}
impl TryFrom<u16> for ArmingMode {
type Error = NanonisError;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
0 => Ok(ArmingMode::NoChange),
1 => Ok(ArmingMode::Manual),
2 => Ok(ArmingMode::Automatic),
_ => Err(NanonisError::Protocol(format!(
"Invalid ArmingMode value: {}",
value
))),
}
}
}
/// FFT window function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FFTWindow {
/// No change
#[default]
NoChange = 0,
/// No window
None = 1,
/// Hanning window
Hanning = 2,
/// Hamming window
Hamming = 3,
/// Blackman-Harris window
BlackmanHarris = 4,
/// Exact Blackman window
ExactBlackman = 5,
/// Blackman window
Blackman = 6,
/// Flat Top window
FlatTop = 7,
/// 4-term Blackman-Harris
FourTermBH = 8,
/// 7-term Blackman-Harris
SevenTermBH = 9,
/// Low Sidelobe window
LowSidelobe = 10,
}
impl From<FFTWindow> for u16 {
fn from(w: FFTWindow) -> Self {
w as u16
}
}
/// FFT averaging mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FFTAveragingMode {
/// No change
#[default]
NoChange = 0,
/// No averaging
None = 1,
/// Vector averaging
Vector = 2,
/// RMS averaging
RMS = 3,
/// Peak hold
PeakHold = 4,
}
impl From<FFTAveragingMode> for u16 {
fn from(m: FFTAveragingMode) -> Self {
m as u16
}
}
/// FFT weighting mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FFTWeightingMode {
/// No change
#[default]
NoChange = 0,
/// Linear weighting
Linear = 1,
/// Exponential weighting
Exponential = 2,
}
impl From<FFTWeightingMode> for u16 {
fn from(m: FFTWeightingMode) -> Self {
m as u16
}
}
/// PLL signal analyzer trigger configuration.
#[derive(Debug, Clone, Default)]
pub struct PLLAnlzrTrigger {
/// Trigger mode
pub mode: PLLTriggerMode,
/// Signal index for trigger
pub source_index: i32,
/// Trigger slope
pub slope: PLLTriggerSlope,
/// Trigger level
pub level: f64,
/// Trigger position in seconds
pub position_s: f64,
/// Arming mode
pub arming: ArmingMode,
}
/// PLL signal analyzer timebase settings.
#[derive(Debug, Clone, Default)]
pub struct PLLAnlzrTimebase {
/// Current timebase index
pub timebase_index: i32,
/// Update rate (1 = fastest)
pub update_rate: i32,
/// Available timebases
pub available_timebases: Vec<String>,
}
/// FFT properties configuration.
#[derive(Debug, Clone, Copy, Default)]
pub struct FFTProps {
/// FFT window function
pub window: FFTWindow,
/// Averaging mode
pub averaging: FFTAveragingMode,
/// Weighting mode
pub weighting: FFTWeightingMode,
/// Number of averages
pub count: i32,
}
/// Oscilloscope data from analyzer.
#[derive(Debug, Clone, Default)]
pub struct OsciAnalyzerData {
/// Timestamp of first point
pub t0: f64,
/// Time between points
pub dt: f64,
/// Data array
pub data: Vec<f64>,
}
/// FFT data from analyzer.
#[derive(Debug, Clone, Default)]
pub struct FFTAnalyzerData {
/// Frequency of first point
pub f0: f64,
/// Frequency step
pub df: f64,
/// Data array
pub data: Vec<f64>,
}
impl NanonisClient {
// ==================== PLL Signal Analyzer ====================
/// Open the PLL signal analyzer.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_open(&mut self) -> Result<(), NanonisError> {
self.quick_send("PLLSignalAnlzr.Open", vec![], vec![], vec![])?;
Ok(())
}
/// Set the analyzer channel.
///
/// # Arguments
/// * `channel_index` - Channel index
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_ch_set(&mut self, channel_index: i32) -> Result<(), NanonisError> {
self.quick_send(
"PLLSignalAnlzr.ChSet",
vec![NanonisValue::I32(channel_index)],
vec!["i"],
vec![],
)?;
Ok(())
}
/// Get the analyzer channel.
///
/// # Returns
/// Channel index.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_ch_get(&mut self) -> Result<i32, NanonisError> {
let result = self.quick_send("PLLSignalAnlzr.ChGet", vec![], vec![], vec!["i"])?;
if !result.is_empty() {
Ok(result[0].as_i32()?)
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set the timebase and update rate.
///
/// # Arguments
/// * `timebase_index` - Timebase index (-1 = no change)
/// * `update_rate` - Update rate (1 = fastest, -1 = no change)
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_timebase_set(
&mut self,
timebase_index: i32,
update_rate: i32,
) -> Result<(), NanonisError> {
self.quick_send(
"PLLSignalAnlzr.TimebaseSet",
vec![
NanonisValue::I32(timebase_index),
NanonisValue::I32(update_rate),
],
vec!["i", "i"],
vec![],
)?;
Ok(())
}
/// Get the timebase and update rate.
///
/// # Returns
/// Timebase settings with available timebases.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_timebase_get(&mut self) -> Result<PLLAnlzrTimebase, NanonisError> {
let result = self.quick_send(
"PLLSignalAnlzr.TimebaseGet",
vec![],
vec![],
vec!["i", "i", "i", "i", "*+c"],
)?;
if result.len() >= 5 {
Ok(PLLAnlzrTimebase {
timebase_index: result[0].as_i32()?,
update_rate: result[1].as_i32()?,
available_timebases: result[4].as_string_array()?.to_vec(),
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set trigger to automatic mode.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_trig_auto(&mut self) -> Result<(), NanonisError> {
self.quick_send("PLLSignalAnlzr.TrigAuto", vec![], vec![], vec![])?;
Ok(())
}
/// Rearm the trigger.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_trig_rearm(&mut self) -> Result<(), NanonisError> {
self.quick_send("PLLSignalAnlzr.TrigRearm", vec![], vec![], vec![])?;
Ok(())
}
/// Set the trigger configuration.
///
/// # Arguments
/// * `trigger` - Trigger configuration
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_trig_set(
&mut self,
trigger: &PLLAnlzrTrigger,
) -> Result<(), NanonisError> {
self.quick_send(
"PLLSignalAnlzr.TrigSet",
vec![
NanonisValue::U16(trigger.mode.into()),
NanonisValue::I32(trigger.source_index),
NanonisValue::U16(trigger.slope.into()),
NanonisValue::F64(trigger.level),
NanonisValue::F64(trigger.position_s),
NanonisValue::U16(trigger.arming.into()),
],
vec!["H", "i", "H", "d", "d", "H"],
vec![],
)?;
Ok(())
}
/// Get the trigger configuration.
///
/// # Returns
/// Trigger configuration.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_trig_get(&mut self) -> Result<PLLAnlzrTrigger, NanonisError> {
let result = self.quick_send(
"PLLSignalAnlzr.TrigGet",
vec![],
vec![],
vec!["H", "i", "H", "d", "d", "H", "i", "i", "*+c"],
)?;
if result.len() >= 6 {
Ok(PLLAnlzrTrigger {
mode: result[0].as_u16()?.try_into()?,
source_index: result[1].as_i32()?,
slope: result[2].as_u16()?.try_into()?,
level: result[3].as_f64()?,
position_s: result[4].as_f64()?,
arming: result[5].as_u16()?.try_into()?,
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Get oscilloscope data from analyzer.
///
/// # Returns
/// Oscilloscope data.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_osci_data_get(&mut self) -> Result<OsciAnalyzerData, NanonisError> {
let result = self.quick_send(
"PLLSignalAnlzr.OsciDataGet",
vec![],
vec![],
vec!["d", "d", "i", "+*d"],
)?;
if result.len() >= 4 {
Ok(OsciAnalyzerData {
t0: result[0].as_f64()?,
dt: result[1].as_f64()?,
data: result[3].as_f64_array()?.to_vec(),
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set FFT properties.
///
/// # Arguments
/// * `props` - FFT configuration
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_fft_props_set(&mut self, props: &FFTProps) -> Result<(), NanonisError> {
self.quick_send(
"PLLSignalAnlzr.FFTPropsSet",
vec![
NanonisValue::U16(props.window.into()),
NanonisValue::U16(props.averaging.into()),
NanonisValue::U16(props.weighting.into()),
NanonisValue::I32(props.count),
],
vec!["H", "H", "H", "i"],
vec![],
)?;
Ok(())
}
/// Get FFT properties.
///
/// # Returns
/// FFT configuration.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_fft_props_get(&mut self) -> Result<FFTProps, NanonisError> {
let result = self.quick_send(
"PLLSignalAnlzr.FFTPropsGet",
vec![],
vec![],
vec!["H", "H", "H", "i"],
)?;
if result.len() >= 4 {
// Note: returned values have different offset than set values
Ok(FFTProps {
window: FFTWindow::NoChange, // Would need TryFrom for returned values
averaging: FFTAveragingMode::NoChange,
weighting: FFTWeightingMode::NoChange,
count: result[3].as_i32()?,
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Restart FFT averaging.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_fft_avg_restart(&mut self) -> Result<(), NanonisError> {
self.quick_send("PLLSignalAnlzr.FFTAvgRestart", vec![], vec![], vec![])?;
Ok(())
}
/// Get FFT data.
///
/// # Returns
/// FFT data.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_signal_anlzr_fft_data_get(&mut self) -> Result<FFTAnalyzerData, NanonisError> {
let result = self.quick_send(
"PLLSignalAnlzr.FFTDataGet",
vec![],
vec![],
vec!["d", "d", "i", "*d"],
)?;
if result.len() >= 4 {
Ok(FFTAnalyzerData {
f0: result[0].as_f64()?,
df: result[1].as_f64()?,
data: result[3].as_f64_array()?.to_vec(),
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
// ==================== PLL Zoom FFT ====================
/// Open the PLL Zoom FFT module.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_open(&mut self) -> Result<(), NanonisError> {
self.quick_send("PLLZoomFFT.Open", vec![], vec![], vec![])?;
Ok(())
}
/// Set the Zoom FFT channel.
///
/// # Arguments
/// * `channel_index` - Channel index
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_ch_set(&mut self, channel_index: i32) -> Result<(), NanonisError> {
self.quick_send(
"PLLZoomFFT.ChSet",
vec![NanonisValue::I32(channel_index)],
vec!["i"],
vec![],
)?;
Ok(())
}
/// Get the Zoom FFT channel.
///
/// # Returns
/// Channel index.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_ch_get(&mut self) -> Result<i32, NanonisError> {
let result = self.quick_send("PLLZoomFFT.ChGet", vec![], vec![], vec!["i"])?;
if !result.is_empty() {
Ok(result[0].as_i32()?)
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Restart Zoom FFT averaging.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_avg_restart(&mut self) -> Result<(), NanonisError> {
self.quick_send("PLLZoomFFT.AvgRestart", vec![], vec![], vec![])?;
Ok(())
}
/// Set Zoom FFT properties.
///
/// # Arguments
/// * `props` - FFT configuration
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_props_set(&mut self, props: &FFTProps) -> Result<(), NanonisError> {
self.quick_send(
"PLLZoomFFT.PropsSet",
vec![
NanonisValue::U16(props.window.into()),
NanonisValue::U16(props.averaging.into()),
NanonisValue::U16(props.weighting.into()),
NanonisValue::I32(props.count),
],
vec!["H", "H", "H", "i"],
vec![],
)?;
Ok(())
}
/// Get Zoom FFT properties.
///
/// # Returns
/// FFT configuration.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_props_get(&mut self) -> Result<FFTProps, NanonisError> {
let result = self.quick_send(
"PLLZoomFFT.PropsGet",
vec![],
vec![],
vec!["H", "H", "H", "i"],
)?;
if result.len() >= 4 {
Ok(FFTProps {
window: FFTWindow::NoChange,
averaging: FFTAveragingMode::NoChange,
weighting: FFTWeightingMode::NoChange,
count: result[3].as_i32()?,
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Get Zoom FFT data.
///
/// # Returns
/// FFT data.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn pll_zoom_fft_data_get(&mut self) -> Result<FFTAnalyzerData, NanonisError> {
let result = self.quick_send(
"PLLZoomFFT.DataGet",
vec![],
vec![],
vec!["d", "d", "i", "*d"],
)?;
if result.len() >= 4 {
Ok(FFTAnalyzerData {
f0: result[0].as_f64()?,
df: result[1].as_f64()?,
data: result[3].as_f64_array()?.to_vec(),
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
}