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
use crate::DriverLoadError;

use libffi::high::ClosureMut8;
use libloading::{Error, Library};
use parking_lot::Mutex;
use pico_common::Driver;
use std::{fmt, path::Path, sync::Arc};

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct UserProbeInteractions {
    pub connected: u16,
    pub channel: i32,
    pub enabled: u16,
    pub probe_name: i32,
    pub requires_power: u8,
    pub is_powered: u8,
    pub status: u32,
    pub probe_off: i32,
    pub range_first: i32,
    pub range_last: i32,
    pub range_current: i32,
    pub coupling_first: i32,
    pub coupling_last: i32,
    pub coupling_current: i32,
    pub filter_flags: i32,
    pub filter_current: i32,
    pub default_filter: i32,
}

pub type StreamingReady = unsafe extern "C" fn(
    handle: i16,
    no_df_samples: i32,
    start_index: u32,
    overflow: i16,
    trigger_at: u32,
    triggered: i16,
    auto_stop: i16,
    context: *mut ::std::os::raw::c_void,
);

pub type ProbeInteractions = unsafe extern "system" fn(
    handle: i16,
    status: u32,
    probes: *mut UserProbeInteractions,
    n_probes: u32,
);

#[derive(Copy, Clone)]
pub enum OpenUnitFn {
    Common(unsafe extern "system" fn(*mut i16, *mut i8) -> u32),
    PS5000A(unsafe extern "system" fn(*mut i16, *mut i8, i32) -> u32),
}

impl OpenUnitFn {
    pub fn detect(driver: Driver, lib: &Library) -> Result<Self, Error> {
        let sym_name = driver.get_symbol_name(b"OpenUnit");

        unsafe {
            match driver {
                Driver::PS5000A => Ok(OpenUnitFn::PS5000A(*lib.get(&sym_name)?)),
                _ => Ok(OpenUnitFn::Common(*lib.get(&sym_name)?)),
            }
        }
    }
}

#[derive(Copy, Clone)]
pub enum RunStreamingFn {
    Common(unsafe extern "system" fn(i16, *mut u32, i32, u32, u32, i16, u32, i32, u32) -> u32),
    PS4000(unsafe extern "system" fn(i16, *mut u32, i32, u32, u32, i16, u32, u32) -> u32),
}

impl RunStreamingFn {
    pub fn detect(driver: Driver, lib: &Library) -> Result<Self, Error> {
        let sym_name = driver.get_symbol_name(b"RunStreaming");

        unsafe {
            match driver {
                Driver::PS4000 => Ok(RunStreamingFn::PS4000(*lib.get(&sym_name)?)),
                _ => Ok(RunStreamingFn::Common(*lib.get(&sym_name)?)),
            }
        }
    }
}

#[derive(Copy, Clone)]
pub enum SetBufferFn {
    Common(unsafe extern "system" fn(i16, i32, *const i16, i32, u32, i32) -> u32),
    PS6000(unsafe extern "system" fn(i16, i32, *const i16, u32, i32) -> u32),
    PS4000(unsafe extern "system" fn(i16, i32, *const i16, i32) -> u32),
}

impl SetBufferFn {
    pub fn detect(driver: Driver, lib: &Library) -> Result<Self, Error> {
        let sym_name = driver.get_symbol_name(b"SetDataBuffer");

        unsafe {
            match driver {
                Driver::PS4000 => Ok(SetBufferFn::PS4000(*lib.get(&sym_name)?)),
                Driver::PS6000 => Ok(SetBufferFn::PS6000(*lib.get(&sym_name)?)),
                _ => Ok(SetBufferFn::Common(*lib.get(&sym_name)?)),
            }
        }
    }
}

#[derive(Copy, Clone)]
pub enum SetChannelFn {
    Common(unsafe extern "system" fn(i16, i32, i16, i32, i32, f32) -> u32),
    PS6000(unsafe extern "system" fn(i16, i32, i16, i32, i32, f32, i32) -> u32),
}

impl SetChannelFn {
    pub fn detect(driver: Driver, lib: &Library) -> Result<Self, Error> {
        let sym_name = driver.get_symbol_name(b"SetChannel");

        unsafe {
            match driver {
                Driver::PS6000 => Ok(SetChannelFn::PS6000(*lib.get(&sym_name)?)),
                _ => Ok(SetChannelFn::Common(*lib.get(&sym_name)?)),
            }
        }
    }
}

/// Dynamically loads various Pico drivers
#[derive(Clone)]
pub struct LoaderCommon {
    _library: Arc<Library>,
    pub driver: Driver,
    pub apply_fix: unsafe extern "system" fn(u32, i16) -> u32,
    pub enumerate_units: unsafe extern "system" fn(*mut i16, *mut i8, *mut i16) -> u32,
    pub open_unit: Arc<Mutex<OpenUnitFn>>,
    pub close_unit: unsafe extern "system" fn(i16) -> u32,
    pub ping_unit: unsafe extern "system" fn(i16) -> u32,
    pub change_power_source: Option<unsafe extern "system" fn(i16, u32) -> u32>,
    pub get_unit_info: unsafe extern "system" fn(i16, *mut i8, i16, *mut i16, u32) -> u32,
    pub get_channel_info:
        Option<unsafe extern "system" fn(i16, i32, i32, *mut i32, *mut i32, i32) -> u32>,
    pub get_maximum_value: Option<unsafe extern "system" fn(i16, *mut i16) -> u32>,
    pub set_channel: SetChannelFn,
    pub run_streaming: RunStreamingFn,
    pub stop_streaming: unsafe extern "system" fn(i16) -> u32,
    pub set_data_buffer: SetBufferFn,
    pub get_latest_streaming_values:
        unsafe extern "system" fn(i16, StreamingReady, *mut ::std::os::raw::c_void) -> u32,
    pub set_probe_callback: Option<unsafe extern "system" fn(i16, ProbeInteractions) -> u32>,
}

impl fmt::Debug for LoaderCommon {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.driver)
    }
}

impl LoaderCommon {
    /// Attempts to load a driver
    pub fn load<P: AsRef<Path>>(driver: Driver, path: P) -> Result<LoaderCommon, DriverLoadError> {
        let path = path.as_ref().to_path_buf();

        let library = Library::new(path)?;

        unsafe {
            let lib = LoaderCommon {
                driver,
                apply_fix: *library.get(&driver.get_symbol_name(b"ApplyFix"))?,
                enumerate_units: *library.get(&driver.get_symbol_name(b"EnumerateUnits"))?,
                ping_unit: *library.get(&driver.get_symbol_name(b"PingUnit"))?,
                open_unit: Arc::new(Mutex::new(OpenUnitFn::detect(driver, &library)?)),
                close_unit: *library.get(&driver.get_symbol_name(b"CloseUnit"))?,
                change_power_source: library
                    .get(&driver.get_symbol_name(b"ChangePowerSource"))
                    .map(|m| *m)
                    .ok(),
                get_unit_info: *library.get(&driver.get_symbol_name(b"GetUnitInfo"))?,
                get_channel_info: library
                    .get(&driver.get_symbol_name(b"GetChannelInformation"))
                    .map(|m| *m)
                    .ok(),
                get_maximum_value: library
                    .get(&driver.get_symbol_name(b"MaximumValue"))
                    .map(|m| *m)
                    .ok(),
                set_channel: SetChannelFn::detect(driver, &library)?,
                run_streaming: RunStreamingFn::detect(driver, &library)?,
                stop_streaming: *library.get(&driver.get_symbol_name(b"Stop"))?,
                set_data_buffer: SetBufferFn::detect(driver, &library)?,
                get_latest_streaming_values: *library
                    .get(&driver.get_symbol_name(b"GetStreamingLatestValues"))?,
                set_probe_callback: library
                    .get(&driver.get_symbol_name(b"SetProbeInteractionCallback"))
                    .map(|m| *m)
                    .ok(),
                _library: Arc::new(library),
            };

            // Apply Fix to disable splash dialog
            (&lib.apply_fix)(0x1ced_9168, 0x11e6);

            Ok(lib)
        }
    }

    /// Wraps the c callback with libffi so we can use closures
    ///
    /// libffi isn't the only way to do this because we have c_void context.
    /// However, we already need libffi for the ps2000 driver and this is easy.
    pub fn get_latest_streaming_values_wrap<
        F: FnMut(i16, i32, u32, i16, u32, i16, i16, *mut ::std::os::raw::c_void),
    >(
        &self,
        handle: i16,
        mut callback: F,
    ) -> u32 {
        let closure = ClosureMut8::new(&mut callback);
        let get_latest_streaming_values = self.get_latest_streaming_values;
        unsafe { get_latest_streaming_values(handle, *closure.code_ptr(), std::ptr::null_mut()) }
    }
}