Skip to content

Commit d342db3

Browse files
committed
siggen: better shots/sweeps encoding using a rust enum
1 parent d433b2f commit d342db3

File tree

5 files changed

+37
-56
lines changed

5 files changed

+37
-56
lines changed

common/src/enums.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,15 @@ pub enum PicoSweepType
238238
DownUp = 3,
239239
}
240240

241-
// Rust addition: encode the potential values of sweeps and shots
241+
// Rust addition: encode the potential values of sweeps and shots, to avoid invalid states
242+
// like >0 & >0
242243
#[derive(Debug, Clone)]
243244
pub enum SweepShotCount {
244-
Regular(u32),
245-
Continuous,
245+
None,
246+
Sweeps(u32),
247+
Shots(u32),
248+
ContinuousSweeps,
249+
ContinuousShots,
246250
}
247251

248252

@@ -251,30 +255,21 @@ pub enum SweepShotCount {
251255
// should build.rs check for identity?
252256
const COPY_PS2000A_SHOT_SWEEP_TRIGGER_CONTINUOUS_RUN: u32 = 4294967295;
253257

254-
impl num_traits::FromPrimitive for SweepShotCount {
255-
fn from_u64(n: u64) -> Option<Self> {
256-
if n as u32 == COPY_PS2000A_SHOT_SWEEP_TRIGGER_CONTINUOUS_RUN {
257-
Some(SweepShotCount::Continuous)
258-
} else {
259-
Some(SweepShotCount::Regular(n as u32))
258+
impl SweepShotCount {
259+
pub fn to_sweeps(&self) -> u32 {
260+
match self {
261+
SweepShotCount::Sweeps(sweeps) => *sweeps,
262+
SweepShotCount::ContinuousSweeps => COPY_PS2000A_SHOT_SWEEP_TRIGGER_CONTINUOUS_RUN,
263+
_ => 0,
260264
}
261265
}
262266

263-
fn from_i64(_: i64) -> Option<Self> {
264-
None
265-
}
266-
}
267-
268-
impl num_traits::ToPrimitive for SweepShotCount {
269-
fn to_u64(&self) -> Option<u64> {
270-
Some((match self {
271-
SweepShotCount::Continuous => COPY_PS2000A_SHOT_SWEEP_TRIGGER_CONTINUOUS_RUN,
272-
SweepShotCount::Regular(x) => *x,
273-
}) as u64)
274-
}
275-
276-
fn to_i64(&self) -> Option<i64> {
277-
None
267+
pub fn to_shots(&self) -> u32 {
268+
match self {
269+
SweepShotCount::Shots(shots) => *shots,
270+
SweepShotCount::ContinuousShots => COPY_PS2000A_SHOT_SWEEP_TRIGGER_CONTINUOUS_RUN,
271+
_ => 0,
272+
}
278273
}
279274
}
280275

driver/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pico-sys-dynamic = {path = "../sys", version = "0.3.1"}
2323
thiserror = "^1.0"
2424
tracing = {version = "0.1", features = ["attributes"]}
2525
version-compare = "0.0.11"
26-
num-traits = "0.2"
2726

2827
[target.'cfg(target_os = "windows")'.dependencies]
2928
winapi = {version = "0.3", features = ["setupapi", "impl-default"]}

driver/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ pub trait PicoDriver: fmt::Debug + Send + Sync {
170170
_increment: f64,
171171
_dwell_time: f64,
172172
_sweep_type: PicoSweepType,
173-
_shots: SweepShotCount,
174-
_sweeps: SweepShotCount,
173+
_sweeps_shots: SweepShotCount,
175174
_trigger_type: PicoSigGenTrigType,
176175
_trigger_source: PicoSigGenTrigSource,
177176
_ext_in_threshold: i16
@@ -201,8 +200,7 @@ pub trait PicoDriver: fmt::Debug + Send + Sync {
201200
_dwell_time: f64,
202201
_sweep_type: PicoSweepType,
203202
_operation: PicoExtraOperations,
204-
_shots: SweepShotCount,
205-
_sweeps: SweepShotCount,
203+
_sweeps_shots: SweepShotCount,
206204
_trigger_type: PicoSigGenTrigType,
207205
_trigger_source: PicoSigGenTrigSource,
208206
_ext_in_threshold: i16,
@@ -223,8 +221,7 @@ pub trait PicoDriver: fmt::Debug + Send + Sync {
223221
_sweep_type: PicoSweepType,
224222
_operation: PicoExtraOperations,
225223
_index_mode: PicoIndexMode,
226-
_shots: SweepShotCount,
227-
_sweeps: SweepShotCount,
224+
_sweeps_shots: SweepShotCount,
228225
_trigger_type: PicoSigGenTrigType,
229226
_trigger_source: PicoSigGenTrigSource,
230227
_ext_in_threshold: i16,

driver/src/ps2000a.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::{
44
trampoline::split_closure,
55
EnumerationResult, PicoDriver,
66
};
7-
use num_traits::cast::ToPrimitive;
87
use parking_lot::RwLock;
98
use pico_common::{
109
ChannelConfig, DownsampleMode, Driver, FromPicoStr, PicoChannel, PicoError, PicoInfo, PicoRange,
@@ -294,8 +293,7 @@ impl PicoDriver for PS2000ADriver {
294293
increment: f64,
295294
dwell_time: f64,
296295
sweep_type: PicoSweepType,
297-
shots: SweepShotCount,
298-
sweeps: SweepShotCount,
296+
sweeps_shots: SweepShotCount,
299297
trigger_type: PicoSigGenTrigType,
300298
trigger_source: PicoSigGenTrigSource,
301299
ext_in_threshold: i16
@@ -308,8 +306,8 @@ impl PicoDriver for PS2000ADriver {
308306
increment,
309307
dwell_time,
310308
sweep_type as u32,
311-
shots.to_u32().unwrap(),
312-
sweeps.to_u32().unwrap(),
309+
sweeps_shots.to_shots(),
310+
sweeps_shots.to_sweeps(),
313311
trigger_type as u32,
314312
trigger_source as u32,
315313
ext_in_threshold
@@ -342,8 +340,7 @@ impl PicoDriver for PS2000ADriver {
342340
dwell_time: f64,
343341
sweep_type: PicoSweepType,
344342
operation: PicoExtraOperations,
345-
shots: SweepShotCount,
346-
sweeps: SweepShotCount,
343+
sweeps_shots: SweepShotCount,
347344
trigger_type: PicoSigGenTrigType,
348345
trigger_source: PicoSigGenTrigSource,
349346
ext_in_threshold: i16,
@@ -360,8 +357,8 @@ impl PicoDriver for PS2000ADriver {
360357
dwell_time,
361358
sweep_type as PS2000A_SWEEP_TYPE,
362359
operation as PS2000A_EXTRA_OPERATIONS,
363-
shots.to_u32().unwrap(),
364-
sweeps.to_u32().unwrap(),
360+
sweeps_shots.to_shots(),
361+
sweeps_shots.to_sweeps(),
365362
trigger_type as PS2000A_SIGGEN_TRIG_TYPE,
366363
trigger_source as PS2000A_SIGGEN_TRIG_SOURCE,
367364
ext_in_threshold,
@@ -382,8 +379,7 @@ impl PicoDriver for PS2000ADriver {
382379
sweep_type: PicoSweepType,
383380
operation: PicoExtraOperations,
384381
index_mode: PicoIndexMode,
385-
shots: SweepShotCount,
386-
sweeps: SweepShotCount,
382+
sweeps_shots: SweepShotCount,
387383
trigger_type: PicoSigGenTrigType,
388384
trigger_source: PicoSigGenTrigSource,
389385
ext_in_threshold: i16,
@@ -407,8 +403,8 @@ impl PicoDriver for PS2000ADriver {
407403
sweep_type as PS2000A_SWEEP_TYPE,
408404
operation as PS2000A_EXTRA_OPERATIONS,
409405
index_mode as PS2000A_INDEX_MODE,
410-
shots.to_u32().unwrap(),
411-
sweeps.to_u32().unwrap(),
406+
sweeps_shots.to_shots(),
407+
sweeps_shots.to_sweeps(),
412408
trigger_type as PS2000A_SIGGEN_TRIG_TYPE,
413409
trigger_source as PS2000A_SIGGEN_TRIG_SOURCE,
414410
ext_in_threshold)

streaming/src/lib.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,7 @@ impl PicoStreamingDevice {
517517
increment: f64,
518518
dwell_time: f64,
519519
sweep_type: PicoSweepType,
520-
shots: SweepShotCount,
521-
sweeps: SweepShotCount,
520+
sweeps_shots: SweepShotCount,
522521
trigger_type: PicoSigGenTrigType,
523522
trigger_source: PicoSigGenTrigSource,
524523
ext_in_threshold: i16
@@ -547,8 +546,7 @@ impl PicoStreamingDevice {
547546
increment,
548547
dwell_time,
549548
sweep_type,
550-
shots,
551-
sweeps,
549+
sweeps_shots,
552550
trigger_type,
553551
trigger_source,
554552
ext_in_threshold).unwrap();
@@ -591,8 +589,7 @@ impl PicoStreamingDevice {
591589
dwell_time: f64, /* amount to stay at each frequency in seconds */
592590
sweep_type: PicoSweepType,
593591
extra_operations: PicoExtraOperations,
594-
shots: SweepShotCount,
595-
sweeps: SweepShotCount,
592+
sweeps_shots: SweepShotCount,
596593
trig_type: PicoSigGenTrigType,
597594
trig_source: PicoSigGenTrigSource,
598595
ext_in_threshold: i16
@@ -626,8 +623,7 @@ impl PicoStreamingDevice {
626623
dwell_time,
627624
sweep_type,
628625
extra_operations,
629-
shots,
630-
sweeps,
626+
sweeps_shots,
631627
trig_type,
632628
trig_source,
633629
ext_in_threshold,
@@ -644,8 +640,7 @@ impl PicoStreamingDevice {
644640
let delta_phase_increment: u32 = 0;
645641
let dwell_count: u32 = 1;
646642
let arbitrary_waveform = vec![1, 1, 1, 1, 0, 0, 0, 0];
647-
let shots: SweepShotCount = SweepShotCount::Regular(0);
648-
let sweeps: SweepShotCount = SweepShotCount::Regular(0);
643+
let sweeps_shots: SweepShotCount = SweepShotCount::None;
649644
let ext_in_threshold: i16 = 0;
650645

651646
let current_state = self.current_state.write();
@@ -678,8 +673,7 @@ impl PicoStreamingDevice {
678673
PicoSweepType::Up,
679674
PicoExtraOperations::Off,
680675
PicoIndexMode::Single,
681-
shots,
682-
sweeps,
676+
sweeps_shots,
683677
PicoSigGenTrigType::Rising,
684678
PicoSigGenTrigSource::None,
685679
ext_in_threshold,

0 commit comments

Comments
 (0)