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