Skip to main content

pico_common/
driver.rs

1use crate::ParseError;
2use enum_iterator::IntoEnumIterator;
3use std::{fmt, str::FromStr};
4
5/// Supported Pico drivers
6#[cfg_attr(
7    feature = "serde",
8    derive(serde::Serialize, serde::Deserialize),
9    serde(rename_all = "lowercase")
10)]
11#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, IntoEnumIterator)]
12pub enum Driver {
13    PS2000,
14    PS2000A,
15    PS3000A,
16    PS4000,
17    PS4000A,
18    PS5000A,
19    PS6000,
20    PS6000A,
21    PSOSPA,
22    /// Only used to get the full dependency name on each platform
23    PicoIPP,
24    /// Only used to get the full dependency name on each platform
25    IOMP5,
26}
27
28impl FromStr for Driver {
29    type Err = ParseError;
30
31    fn from_str(input: &str) -> Result<Self, Self::Err> {
32        let input = input.to_uppercase().replace("PS", "").replace(' ', "");
33
34        match &input[..] {
35            "2000" => Ok(Driver::PS2000),
36            "2000A" => Ok(Driver::PS2000A),
37            "3000A" => Ok(Driver::PS3000A),
38            "4000" => Ok(Driver::PS4000),
39            "4000A" => Ok(Driver::PS4000A),
40            "5000A" => Ok(Driver::PS5000A),
41            "6000" => Ok(Driver::PS6000),
42            "6000A" => Ok(Driver::PS6000A),
43            _ => Err(ParseError),
44        }
45    }
46}
47
48impl fmt::Display for Driver {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        write!(f, "{}", format!("{:?}", self).to_lowercase())
51    }
52}
53
54impl Driver {
55    /// Returns the relevant `Driver` for the supplied USB PID
56    pub fn from_pid(pid: u16) -> Option<Driver> {
57        match pid {
58            0x1007 => Some(Driver::PS2000),
59            0x1016 | 0x1200 => Some(Driver::PS2000A),
60            0x1012 | 0x1201 | 0x1211 | 0x1213 => Some(Driver::PS3000A),
61            0x1009 | 0x100F => Some(Driver::PS4000),
62            0x1202 | 0x1212 | 0x1214 | 0x1219 | 0x1220 | 0x121A | 0x121B => Some(Driver::PS4000A),
63            0x1019 | 0x1203 | 0x1217 | 0x1218 => Some(Driver::PS5000A),
64            0x100E | 0x1204 => Some(Driver::PS6000),
65            0x1215 | 0x1216 | 0x12A0 | 0x12A1 => Some(Driver::PS6000A),
66            0x1020 => Some(Driver::PSOSPA),
67            u => {
68                tracing::warn!("Unsupported Pico Product ID found: {:#X}", u);
69                None
70            }
71        }
72    }
73
74    /// Returns the platform dependent name of the driver binary with file
75    /// extension
76    /// ```
77    /// let driver = pico_common::Driver::PS2000A;
78    /// let binary_name = driver.get_binary_name();
79    ///
80    /// if cfg!(target_os = "windows") {
81    ///     assert_eq!(binary_name, "ps2000a.dll");
82    /// } else if cfg!(target_os = "macos") {
83    ///     assert_eq!(binary_name, "libps2000a.dylib");
84    /// } else {
85    ///     assert_eq!(binary_name, "libps2000a.so");
86    /// }
87    /// ```
88    pub fn get_binary_name(self) -> String {
89        if cfg!(target_os = "windows") {
90            format!("{}.dll", self)
91        } else if cfg!(target_os = "macos") {
92            format!("lib{}.dylib", self)
93        } else {
94            format!("lib{}.so", self)
95        }
96    }
97
98    /// Gets the required driver dependencies for this platform
99    pub fn get_dependencies_for_platform() -> Vec<Driver> {
100        if cfg!(target_os = "windows") {
101            vec![Driver::PicoIPP]
102        } else if cfg!(target_os = "macos") {
103            vec![Driver::IOMP5, Driver::PicoIPP]
104        } else {
105            // There is no libiomp5 requirement for Pico ARM drivers
106            if cfg!(all(target_arch = "arm", target_os = "linux")) {
107                vec![Driver::PicoIPP]
108            } else {
109                vec![Driver::IOMP5, Driver::PicoIPP]
110            }
111        }
112    }
113}