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
pub use common::{DriverCommon, LoaderCommon};
pub use kernel_driver::is_kernel_driver_installed;
use parking_lot::RwLock;
use pico_common::{
ChannelConfig, Driver, PicoChannel, PicoError, PicoInfo, PicoRange, PicoResult, SampleConfig,
};
pub use ps2000::{DriverPS2000, LoaderPS2000};
pub use resolution::{DependencyLoader, Resolution};
use std::{pin::Pin, sync::Arc};
use thiserror::Error;
use version_compare::Version;
mod common;
mod kernel_driver;
mod ps2000;
mod resolution;
#[derive(Error, Debug)]
pub enum DriverLoadError {
#[error("Pico driver error: {0}")]
DriverError(#[from] PicoError),
#[error("Library load error: {0}")]
LibloadingError(#[from] libloading::Error),
#[error("Invalid Driver Version: Requires >= {required}, Found: {found}")]
VersionError { found: String, required: String },
}
pub trait PicoDriver: Send + Sync {
fn get_driver(&self) -> Driver;
fn get_version(&self) -> PicoResult<String>;
fn get_path(&self) -> PicoResult<Option<String>>;
fn enumerate_units(&self) -> PicoResult<Vec<String>>;
fn open_unit<'a>(&self, serial: Option<&'a str>) -> PicoResult<i16>;
fn ping_unit(&self, handle: i16) -> PicoResult<()>;
fn maximum_value(&self, handle: i16) -> PicoResult<i16>;
fn close_unit(&self, handle: i16) -> PicoResult<()>;
fn get_unit_info(&self, handle: i16, info_type: PicoInfo) -> PicoResult<String>;
fn get_channel_ranges(&self, handle: i16, channel: PicoChannel) -> PicoResult<Vec<PicoRange>>;
fn set_channel(
&self,
handle: i16,
channel: PicoChannel,
config: &ChannelConfig,
) -> PicoResult<()>;
fn set_data_buffer(
&self,
handle: i16,
channel: PicoChannel,
buffer: Arc<RwLock<Pin<Vec<i16>>>>,
buffer_len: usize,
) -> PicoResult<()>;
fn start_streaming(
&self,
handle: i16,
sample_config: &SampleConfig,
) -> PicoResult<SampleConfig>;
fn get_latest_streaming_values<'a>(
&self,
handle: i16,
callback: Box<dyn FnMut(usize, usize) + 'a>,
) -> PicoResult<()>;
fn stop_streaming(&self, handle: i16) -> PicoResult<()>;
fn check_version(&self) -> Result<(), DriverLoadError> {
let loaded_str = &self.get_version()?;
let loaded_version = Version::from(loaded_str);
#[allow(clippy::expect_fun_call)]
let required_str = get_min_required_version(self.get_driver());
let required_version = Version::from(required_str);
if loaded_version < required_version {
Err(DriverLoadError::VersionError {
found: loaded_str.to_string(),
required: required_str.to_string(),
})
} else {
Ok(())
}
}
}
pub type ArcDriver = Arc<Box<dyn PicoDriver>>;
fn get_min_required_version(driver: Driver) -> &'static str {
match driver {
Driver::PS2000 => "3.0.30.1878",
Driver::PS2000A
| Driver::PS3000A
| Driver::PS4000
| Driver::PS4000A
| Driver::PS5000A
| Driver::PS6000 => "2.1.30.1878",
_ => panic!(format!(
"We don't know the minimum required version for the {:?} driver!",
driver,
)),
}
}
pub trait LoadDriverExt {
fn try_load(&self) -> Result<ArcDriver, DriverLoadError>;
fn try_load_with_resolution(
&self,
resolution: &Resolution,
) -> Result<ArcDriver, DriverLoadError>;
}
impl LoadDriverExt for Driver {
fn try_load(&self) -> Result<ArcDriver, DriverLoadError> {
self.try_load_with_resolution(&Default::default())
}
fn try_load_with_resolution(
&self,
resolution: &Resolution,
) -> Result<ArcDriver, DriverLoadError> {
Ok(Arc::new(match self {
Driver::PS2000 => Box::new(DriverPS2000::new(&resolution)?),
Driver::PicoIPP | Driver::IOMP5 => panic!("This type of driver cannot be loaded"),
_ => Box::new(DriverCommon::new(*self, &resolution)?),
}))
}
}