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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
mod types;
pub use types::*;
use super::NanonisClient;
use crate::error::NanonisError;
use crate::types::{NanonisValue, Position};
use std::time::Duration;
use crate::protocol::Protocol;
/// `Scan.PropsGet` reply layout for the newest documented firmware:
/// continuous, bouncy, autosave, series-name(size+str), comment(size+str),
/// modules-names(size+count+array), num-params-per-module(size+array),
/// parameters(rows+cols+2-D array), autopaste.
const SCAN_PROPS_FULL: &[&str] = &[
"I", "I", "I", "i", "*-c", "i", "*-c", "i", "i", "*+c", "i", "*+i", "i", "i", "*+c", "I",
];
/// Core fields returned by every firmware version. Older Nanonis releases omit
/// the modules-params block and autopaste that were added later (see the
/// TCPProtocol_SPM.pdf changelog), so their reply ends here.
const SCAN_PROPS_CORE: &[&str] = &["I", "I", "I", "i", "*-c", "i", "*-c"];
/// Parse a `Scan.PropsGet` response body, tolerant of older firmware.
///
/// The reply layout is purely additive across Nanonis versions, so we try the
/// full documented layout first and fall back to the core fields when the body
/// is too short. Without this, the cursor overruns the data into the error
/// trailer and the read fails with `UnexpectedEof`.
fn parse_scan_props(body: &[u8]) -> Result<ScanProps, NanonisError> {
let (result, data_end, full) = match Protocol::parse_response(body, SCAN_PROPS_FULL) {
Ok((values, cursor)) => (values, cursor, true),
Err(_) => {
let (values, cursor) = Protocol::parse_response(body, SCAN_PROPS_CORE)?;
(values, cursor, false)
}
};
// The error trailer sits immediately after the data we consumed.
Protocol::parse_error_info(body, data_end)?;
let continuous_scan = result[0].as_u32()? == 1;
let bouncy_scan = result[1].as_u32()? == 1;
let autosave = AutosaveMode::try_from(result[2].as_u32()?)?;
let series_name = result[4].as_string()?.to_string();
let comment = result[6].as_string()?.to_string();
let (modules_names, num_params_per_module, parameters, autopaste) = if full {
let modules_names = result[9].as_string_array()?.to_vec();
let num_params_per_module = result[11].as_i32_array()?.to_vec();
let rows = result[12].as_i32()?;
let cols = result[13].as_i32()?;
let params_flat = result[14].as_string_array()?;
// Convert flat array to 2D (rows x cols)
let mut parameters = Vec::new();
for row in 0..rows as usize {
let mut row_params = Vec::new();
for col in 0..cols as usize {
let idx = row * (cols as usize) + col;
if idx < params_flat.len() {
row_params.push(params_flat[idx].clone());
}
}
parameters.push(row_params);
}
let autopaste = AutopasteMode::try_from(result[15].as_u32()?)?;
(modules_names, num_params_per_module, parameters, autopaste)
} else {
// Older firmware does not report these fields; leave them empty.
(Vec::new(), Vec::new(), Vec::new(), AutopasteMode::Off)
};
Ok(ScanProps {
continuous_scan,
bouncy_scan,
autosave,
series_name,
comment,
modules_names,
num_params_per_module,
parameters,
autopaste,
})
}
impl NanonisClient {
/// Start, stop, pause or resume a scan
pub fn scan_action(
&mut self,
scan_action: ScanAction,
scan_direction: ScanDirection,
) -> Result<(), NanonisError> {
self.quick_send(
"Scan.Action",
vec![
NanonisValue::U16(scan_action.into()),
NanonisValue::U32(scan_direction.into()),
],
vec!["H", "I"],
vec![],
)?;
Ok(())
}
/// Configure the scan frame parameters
pub fn scan_frame_set(&mut self, frame: ScanFrame) -> Result<(), NanonisError> {
self.quick_send(
"Scan.FrameSet",
vec![
NanonisValue::F32(frame.center.x as f32),
NanonisValue::F32(frame.center.y as f32),
NanonisValue::F32(frame.width_m),
NanonisValue::F32(frame.height_m),
NanonisValue::F32(frame.angle_deg),
],
vec!["f", "f", "f", "f", "f"],
vec![],
)?;
Ok(())
}
/// Get the scan frame parameters
pub fn scan_frame_get(&mut self) -> Result<ScanFrame, NanonisError> {
let result = self.quick_send(
"Scan.FrameGet",
vec![],
vec![],
vec!["f", "f", "f", "f", "f"],
)?;
if result.len() >= 5 {
let center_x = result[0].as_f32()? as f64;
let center_y = result[1].as_f32()? as f64;
let width = result[2].as_f32()?;
let height = result[3].as_f32()?;
let angle = result[4].as_f32()?;
Ok(ScanFrame::new(
Position::new(center_x, center_y),
width,
height,
angle,
))
} else {
Err(NanonisError::Protocol(
"Invalid scan frame response".to_string(),
))
}
}
/// Get the scan buffer parameters
/// Returns: (channel_indexes, pixels, lines)
pub fn scan_buffer_get(&mut self) -> Result<(Vec<i32>, i32, i32), NanonisError> {
let result =
self.quick_send("Scan.BufferGet", vec![], vec![], vec!["i", "*i", "i", "i"])?;
if result.len() >= 4 {
let channel_indexes = result[1].as_i32_array()?.to_vec();
let pixels = result[2].as_i32()?;
let lines = result[3].as_i32()?;
Ok((channel_indexes, pixels, lines))
} else {
Err(NanonisError::Protocol(
"Invalid scan buffer response".to_string(),
))
}
}
/// Get the current scan status.
///
/// Returns whether a scan is currently running or not.
///
/// # Returns
/// `true` if scan is running, `false` if scan is not running.
///
/// # Errors
/// Returns `NanonisError` if communication fails or protocol error occurs.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// if client.scan_status_get()? {
/// println!("Scan is currently running");
/// } else {
/// println!("Scan is stopped");
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_status_get(&mut self) -> Result<bool, NanonisError> {
let result = self.quick_send("Scan.StatusGet", vec![], vec![], vec!["I"])?;
match result.first() {
Some(value) => Ok(value.as_u32()? == 1),
None => Err(NanonisError::Protocol(
"No scan status returned".to_string(),
)),
}
}
/// Configure the scan buffer parameters.
///
/// Sets which channels to record during scanning and the scan resolution.
/// The channel indexes refer to the 24 signals assigned in the Signals Manager (0-23).
///
/// **Important**: The number of pixels is coerced to the closest multiple of 16
/// because scan data is sent in packages of 16 pixels.
///
/// # Arguments
/// * `channel_indexes` - Indexes of channels to record (0-23 for signals in Signals Manager)
/// * `pixels` - Number of pixels per line (coerced to multiple of 16)
/// * `lines` - Number of scan lines
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid parameters provided.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Record channels 0, 1, and 2 with 512x512 resolution
/// client.scan_buffer_set(vec![0, 1, 2], 512, 512)?;
///
/// // High resolution scan with multiple channels
/// client.scan_buffer_set(vec![0, 1, 2, 3, 4], 1024, 1024)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_buffer_set(
&mut self,
channel_indexes: Vec<i32>,
pixels: i32,
lines: i32,
) -> Result<(), NanonisError> {
self.quick_send(
"Scan.BufferSet",
vec![
NanonisValue::ArrayI32(channel_indexes),
NanonisValue::I32(pixels),
NanonisValue::I32(lines),
],
vec!["+*i", "i", "i"],
vec![],
)?;
Ok(())
}
/// Configure scan speed parameters.
///
/// Sets the tip scanning speeds for both forward and backward scan directions.
/// You can specify either linear speed or time per line, and set speed ratios
/// between forward and backward scanning.
///
/// # Arguments
/// * `forward_linear_speed_m_s` - Forward linear speed in m/s
/// * `backward_linear_speed_m_s` - Backward linear speed in m/s
/// * `forward_time_per_line_s` - Forward time per line in seconds
/// * `backward_time_per_line_s` - Backward time per line in seconds
/// * `keep_parameter_constant` - Which parameter to keep constant: 0=no change, 1=linear speed, 2=time per line
/// * `speed_ratio` - Backward tip speed relative to forward speed
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid parameters provided.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
/// use nanonis_rs::scan::ScanConfig;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set 1 μm/s forward, 2 μm/s backward, keep linear speed constant
/// let config = ScanConfig {
/// forward_linear_speed_m_s: 1e-6,
/// backward_linear_speed_m_s: 2e-6,
/// forward_time_per_line_s: 0.1,
/// backward_time_per_line_s: 0.05,
/// keep_parameter_constant: 1,
/// speed_ratio: 2.0,
/// };
/// client.scan_config_set(config)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_config_set(&mut self, config: ScanConfig) -> Result<(), NanonisError> {
self.quick_send(
"Scan.SpeedSet",
vec![
NanonisValue::F32(config.forward_linear_speed_m_s),
NanonisValue::F32(config.backward_linear_speed_m_s),
NanonisValue::F32(config.forward_time_per_line_s),
NanonisValue::F32(config.backward_time_per_line_s),
NanonisValue::U16(config.keep_parameter_constant),
NanonisValue::F32(config.speed_ratio),
],
vec!["f", "f", "f", "f", "H", "f"],
vec![],
)?;
Ok(())
}
/// Get the current scan speed parameters.
///
/// Returns all scan speed configuration values including linear speeds,
/// time per line, and speed ratio settings.
///
/// # Returns
/// A tuple containing:
/// - `f32` - Forward linear speed (m/s)
/// - `f32` - Backward linear speed (m/s)
/// - `f32` - Forward time per line (s)
/// - `f32` - Backward time per line (s)
/// - `u16` - Keep parameter constant (0=linear speed, 1=time per line)
/// - `f32` - Speed ratio (backward relative to forward)
///
/// # Errors
/// Returns `NanonisError` if communication fails or protocol error occurs.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let config = client.scan_speed_get()?;
///
/// println!("Forward speed: {:.2e} m/s", config.forward_linear_speed_m_s);
/// println!("Backward speed: {:.2e} m/s", config.backward_linear_speed_m_s);
/// println!("Speed ratio: {:.1}", config.speed_ratio);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_speed_get(&mut self) -> Result<ScanConfig, NanonisError> {
let result = self.quick_send(
"Scan.SpeedGet",
vec![],
vec![],
vec!["f", "f", "f", "f", "H", "f"],
)?;
if result.len() >= 6 {
Ok(ScanConfig {
forward_linear_speed_m_s: result[0].as_f32()?,
backward_linear_speed_m_s: result[1].as_f32()?,
forward_time_per_line_s: result[2].as_f32()?,
backward_time_per_line_s: result[3].as_f32()?,
keep_parameter_constant: result[4].as_u16()?,
speed_ratio: result[5].as_f32()?,
})
} else {
Err(NanonisError::Protocol(
"Invalid scan speed response".to_string(),
))
}
}
/// Get the current XY position during scanning.
///
/// Returns the current values of the X and Y signals, useful for monitoring
/// tip position during scanning operations.
///
/// # Arguments
/// * `wait_newest_data` - If `true`, discards first value and waits for fresh data
///
/// # Returns
/// A tuple containing (X position in m, Y position in m)
///
/// # Errors
/// Returns `NanonisError` if communication fails or protocol error occurs.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Get current position immediately
/// let (x, y) = client.scan_xy_pos_get(false)?;
/// println!("Current position: ({:.6}, {:.6}) m", x, y);
///
/// // Wait for fresh position data
/// let (x, y) = client.scan_xy_pos_get(true)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_xy_pos_get(&mut self, wait_newest_data: bool) -> Result<(f32, f32), NanonisError> {
let wait_flag = if wait_newest_data { 1u32 } else { 0u32 };
let result = self.quick_send(
"Scan.XYPosGet",
vec![NanonisValue::U32(wait_flag)],
vec!["I"],
vec!["f", "f"],
)?;
if result.len() >= 2 {
Ok((result[0].as_f32()?, result[1].as_f32()?))
} else {
Err(NanonisError::Protocol(
"Invalid XY position response".to_string(),
))
}
}
/// Save the current scan data buffer to file.
///
/// Saves the current scan data into a file. If `wait_until_saved` is true,
/// the function waits for the save operation to complete before returning.
///
/// # Arguments
/// * `wait_until_saved` - If `true`, waits for save completion before returning
/// * `timeout_ms` - Timeout in milliseconds (-1 for indefinite wait)
///
/// # Returns
/// `true` if timeout occurred while waiting for save completion, `false` otherwise
///
/// # Errors
/// Returns `NanonisError` if communication fails or protocol error occurs.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Save immediately without waiting
/// let timed_out = client.scan_save(false, 5000)?;
///
/// // Save and wait up to 30 seconds for completion
/// let timed_out = client.scan_save(true, 30000)?;
/// if timed_out {
/// println!("Save operation timed out");
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_save(
&mut self,
wait_until_saved: bool,
timeout_ms: i32,
) -> Result<bool, NanonisError> {
let wait_flag = if wait_until_saved { 1u32 } else { 0u32 };
let result = self.quick_send(
"Scan.Save",
vec![NanonisValue::U32(wait_flag), NanonisValue::I32(timeout_ms)],
vec!["I", "i"],
vec!["I"],
)?;
match result.first() {
Some(value) => Ok(value.as_u32()? == 1),
None => Err(NanonisError::Protocol(
"No save status returned".to_string(),
)),
}
}
/// Get scan frame data for a specific channel and direction.
///
/// Returns the complete 2D scan data array for the selected channel.
/// The channel must be one of the channels configured in the scan buffer.
///
/// # Arguments
/// * `channel_index` - Index of channel to retrieve data from (must be in acquired channels)
/// * `data_direction` - Data direction: `true` for forward, `false` for backward
///
/// # Returns
/// A tuple containing:
/// - `String` - Channel name
/// - `Vec<Vec<f32>>` - 2D scan data array \[rows\]\[columns\]
/// - `bool` - Scan direction: `true` for up, `false` for down
///
/// # Errors
/// Returns `NanonisError` if:
/// - Invalid channel index (not in acquired channels)
/// - Communication fails or protocol error occurs
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Get forward scan data for channel 0
/// let (channel_name, data, scan_up) = client.scan_frame_data_grab(0, true)?;
/// println!("Channel: {}, Direction: {}", channel_name, if scan_up { "up" } else { "down" });
/// println!("Data size: {}x{}", data.len(), data[0].len());
///
/// // Get backward scan data
/// let (_, back_data, _) = client.scan_frame_data_grab(0, false)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_frame_data_grab(
&mut self,
channel_index: u32,
data_direction: bool,
) -> Result<(String, Vec<Vec<f32>>, bool), NanonisError> {
let direction_flag = if data_direction { 1u32 } else { 0u32 };
let result = self.quick_send(
"Scan.FrameDataGrab",
vec![
NanonisValue::U32(channel_index),
NanonisValue::U32(direction_flag),
],
vec!["I", "I"],
vec!["i", "*-c", "i", "i", "2f", "I"],
)?;
if result.len() >= 6 {
let channel_name = result[1].as_string()?.to_string();
// The protocol layer already parses "2f" into a 2D array
let data_2d = result[4].as_f32_2d_array()?.clone();
let scan_direction = result[5].as_u32()? == 1;
Ok((channel_name, data_2d, scan_direction))
} else {
Err(NanonisError::Protocol(
"Invalid frame data response".to_string(),
))
}
}
/// Wait for the End-of-Scan.
///
/// Waits for the current scan to complete or timeout to occur, whichever comes first.
/// This is useful for synchronizing operations with scan completion.
///
/// # Arguments
/// * `timeout` - Timeout duration (-1 for indefinite wait)
///
/// # Returns
/// A tuple containing:
/// - `bool` - `true` if timeout occurred, `false` if scan completed normally
/// - `String` - File path where data was auto-saved (empty if no auto-save)
///
/// # Errors
/// Returns `NanonisError` if communication fails or protocol error occurs.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
/// use nanonis_rs::scan::{ScanAction, ScanDirection};
/// use std::time::Duration;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Start a scan
/// client.scan_action(ScanAction::Start, ScanDirection::Up)?;
///
/// // Wait for scan to complete (up to 5 minutes)
/// let (timed_out, file_path) = client.scan_wait_end_of_scan(Duration::from_secs(300))?;
///
/// if timed_out {
/// println!("Scan timed out after 5 minutes");
/// } else {
/// println!("Scan completed");
/// if !file_path.is_empty() {
/// println!("Data saved to: {}", file_path);
/// }
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_wait_end_of_scan(
&mut self,
timeout: Duration,
) -> Result<(bool, String), NanonisError> {
let result = self.quick_send(
"Scan.WaitEndOfScan",
vec![NanonisValue::I32(
timeout.as_millis().min(i32::MAX as u128) as i32
)],
vec!["i"],
vec!["I", "I", "*-c"],
)?;
if result.len() >= 3 {
let timeout_occurred = result[0].as_u32()? == 1;
let file_path = result[2].as_string()?.to_string();
Ok((timeout_occurred, file_path))
} else {
Err(NanonisError::Protocol(
"Invalid scan wait response".to_string(),
))
}
}
/// Get scan properties configuration.
///
/// Returns current scan properties including continuous scan, bouncy scan,
/// autosave, series name, comment, modules names, and autopaste settings.
///
/// # Returns
/// `ScanProps` structure containing all scan property settings
///
/// # Errors
/// Returns `NanonisError` if communication fails or protocol error occurs.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// let props = client.scan_props_get()?;
/// println!("Continuous scan: {:?}", props.continuous_scan);
/// println!("Bouncy scan: {:?}", props.bouncy_scan);
/// println!("Series name: {}", props.series_name);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_props_get(&mut self) -> Result<ScanProps, NanonisError> {
// Read the raw body and parse defensively: the reply layout depends on
// the Nanonis firmware version (older firmware omits the modules-params
// block and autopaste). See `parse_scan_props`.
let body = self.quick_send_raw("Scan.PropsGet", vec![], vec![])?;
parse_scan_props(&body)
}
/// Set scan properties configuration.
///
/// Configures scan parameters including continuous scan, bouncy scan,
/// autosave behavior, series name, comment, and autopaste settings.
/// Use `ScanPropsBuilder` to set only the properties you want to change.
///
/// # Arguments
/// * `builder` - Builder with properties to set. Fields set to `None` will not be changed.
///
/// # Errors
/// Returns `NanonisError` if communication fails or invalid parameters provided.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
/// use nanonis_rs::scan::{ScanPropsBuilder, AutosaveMode, AutopasteMode};
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
///
/// // Set only specific properties using the builder
/// let builder = ScanPropsBuilder::new()
/// .continuous_scan(true) // Enable continuous scan
/// .bouncy_scan(true) // Enable bouncy scan
/// .autosave(AutosaveMode::Off); // Disable autosave
///
/// client.scan_props_set(builder)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_props_set(&mut self, builder: ScanPropsBuilder) -> Result<(), NanonisError> {
// Convert boolean options to u32 (0=no change, 1=On, 2=Off)
let continuous_flag = match builder.continuous_scan {
None => 0u32,
Some(true) => 1u32,
Some(false) => 2u32,
};
let bouncy_flag = match builder.bouncy_scan {
None => 0u32,
Some(true) => 1u32,
Some(false) => 2u32,
};
let autosave_flag = match builder.autosave {
None => 0u32,
Some(mode) => mode.into(),
};
let autopaste_flag = match builder.autopaste {
None => 0u32,
Some(mode) => mode.into(),
};
// For strings/arrays: empty string/array means no change
let series_name = builder.series_name.unwrap_or_default();
let comment = builder.comment.unwrap_or_default();
let modules_names = builder.modules_names.unwrap_or_default();
self.quick_send(
"Scan.PropsSet",
vec![
NanonisValue::U32(continuous_flag),
NanonisValue::U32(bouncy_flag),
NanonisValue::U32(autosave_flag),
NanonisValue::String(series_name),
NanonisValue::String(comment),
NanonisValue::ArrayString(modules_names),
NanonisValue::U32(autopaste_flag),
],
vec!["I", "I", "I", "+*c", "+*c", "+*c", "I"],
vec![],
)?;
Ok(())
}
/// Paste background image at specified location.
///
/// Pastes a previously copied background image to the scan buffer at the
/// specified position. This is used for background subtraction operations.
///
/// # Arguments
/// * `x` - X position in meters for paste location
/// * `y` - Y position in meters for paste location
///
/// # Errors
/// Returns `NanonisError` if communication fails or no background is available.
pub fn scan_background_paste(&mut self, x: f64, y: f64) -> Result<(), NanonisError> {
self.quick_send(
"Scan.BackgroundPaste",
vec![NanonisValue::F64(x), NanonisValue::F64(y)],
vec!["d", "d"],
vec![],
)?;
Ok(())
}
/// Delete the stored background image.
///
/// Removes the background image from memory, freeing resources and
/// disabling background subtraction until a new background is captured.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn scan_background_delete(&mut self) -> Result<(), NanonisError> {
self.quick_send("Scan.BackgroundDelete", vec![], vec![], vec![])?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
// Big-endian encoders for building synthetic Scan.PropsGet response bodies.
fn u32be(v: u32, out: &mut Vec<u8>) {
out.extend_from_slice(&v.to_be_bytes());
}
fn i32be(v: i32, out: &mut Vec<u8>) {
out.extend_from_slice(&v.to_be_bytes());
}
fn sized_str(s: &str, out: &mut Vec<u8>) {
i32be(s.len() as i32, out);
out.extend_from_slice(s.as_bytes());
}
// 8-byte success trailer: error status = 0, description size = 0.
fn ok_trailer(out: &mut Vec<u8>) {
i32be(0, out);
i32be(0, out);
}
/// Older firmware: the reply ends after the comment (no modules-params
/// block, no autopaste). This is the layout that previously overran into
/// the error trailer and failed with `UnexpectedEof`.
#[test]
fn parse_scan_props_core_only_layout() {
let mut body = Vec::new();
u32be(1, &mut body); // continuous = On
u32be(0, &mut body); // bouncy = Off
u32be(2, &mut body); // autosave = Off
sized_str("scan", &mut body);
sized_str("hello", &mut body);
ok_trailer(&mut body);
let props = parse_scan_props(&body).expect("core layout should parse");
assert!(props.continuous_scan);
assert!(!props.bouncy_scan);
assert_eq!(props.autosave, AutosaveMode::Off);
assert_eq!(props.series_name, "scan");
assert_eq!(props.comment, "hello");
assert!(props.modules_names.is_empty());
assert!(props.parameters.is_empty());
}
/// Newer firmware: full documented layout, with empty modules/params arrays.
#[test]
fn parse_scan_props_full_layout() {
let mut body = Vec::new();
u32be(0, &mut body); // continuous = Off
u32be(1, &mut body); // bouncy = On
u32be(0, &mut body); // autosave = All
sized_str("img", &mut body);
sized_str("", &mut body);
i32be(0, &mut body); // modules names size (bytes)
i32be(0, &mut body); // modules names count -> 0 strings
i32be(0, &mut body); // num-params-per-module count -> 0 ints
i32be(0, &mut body); // parameters rows
i32be(0, &mut body); // parameters cols -> 0 strings
u32be(2, &mut body); // autopaste = Off
ok_trailer(&mut body);
let props = parse_scan_props(&body).expect("full layout should parse");
assert!(!props.continuous_scan);
assert!(props.bouncy_scan);
assert_eq!(props.autosave, AutosaveMode::All);
assert_eq!(props.series_name, "img");
assert_eq!(props.comment, "");
assert_eq!(props.autopaste, AutopasteMode::Off);
}
}