Skip to main content

pico_driver/
resolution.rs

1use crate::{
2    ps2000, ps2000a, ps3000a, ps4000, ps4000a, ps5000a, ps6000, ps6000a, psospa, ArcDriver, DriverLoadError
3};
4use pico_common::Driver;
5use std::{env::current_exe, path::PathBuf, sync::Arc};
6
7/// Instructs the loader where to load drivers from
8#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
9pub enum LibraryResolution {
10    /// Searches for drivers using the OS default path resolution
11    Default,
12    /// Searches for drivers in the application root directory
13    AppRoot,
14    /// Searches for drivers at a specific path
15    Custom(PathBuf),
16}
17
18impl LibraryResolution {
19    /// Get the expected path for a driver for this resolution
20    pub fn get_path(&self, driver: Driver) -> PathBuf {
21        let binary_name = driver.get_binary_name();
22
23        match self {
24            LibraryResolution::Default => PathBuf::from(binary_name),
25            LibraryResolution::AppRoot => current_exe()
26                .expect("current_exe path could not be found")
27                .parent()
28                .expect("current_exe path does not have parent")
29                .join(binary_name),
30            LibraryResolution::Custom(path) => path.join(binary_name),
31        }
32    }
33
34    pub fn try_load(&self, driver: Driver) -> Result<ArcDriver, DriverLoadError> {
35        let path = self.get_path(driver);
36        Ok(match driver {
37            Driver::PS2000 => Arc::new(ps2000::PS2000Driver::new(path)?),
38            Driver::PS2000A => Arc::new(ps2000a::PS2000ADriver::new(path)?),
39            Driver::PS3000A => Arc::new(ps3000a::PS3000ADriver::new(path)?),
40            Driver::PS4000 => Arc::new(ps4000::PS4000Driver::new(path)?),
41            Driver::PS4000A => Arc::new(ps4000a::PS4000ADriver::new(path)?),
42            Driver::PS5000A => Arc::new(ps5000a::PS5000ADriver::new(path)?),
43            Driver::PS6000 => Arc::new(ps6000::PS6000Driver::new(path)?),
44            Driver::PS6000A => Arc::new(ps6000a::PS6000ADriver::new(path)?),
45            Driver::PSOSPA => Arc::new(psospa::PSOSPADriver::new(path)?),
46            Driver::PicoIPP | Driver::IOMP5 => {
47                panic!("{driver} is a library used by Pico drivers and cannot be loaded directly",)
48            }
49        })
50    }
51}
52
53impl Default for LibraryResolution {
54    fn default() -> Self {
55        LibraryResolution::Default
56    }
57}