pico_common/
enums.rs

1use num_derive::*;
2use std::{convert::From, fmt, str::FromStr};
3
4/// Error when attempting to parse enums from strings
5#[derive(Debug, PartialEq)]
6pub struct ParseError;
7
8#[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, Ord, PartialOrd, Hash, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10/// Pico channel options
11pub enum PicoChannel {
12    A = 0,
13    B = 1,
14    C = 2,
15    D = 3,
16    E = 4,
17    F = 5,
18    G = 6,
19    H = 7,
20}
21
22impl FromStr for PicoChannel {
23    type Err = ParseError;
24
25    fn from_str(input: &str) -> Result<Self, Self::Err> {
26        let input = input.replace(" ", "").to_uppercase();
27
28        match &input[..] {
29            "A" => Ok(PicoChannel::A),
30            "B" => Ok(PicoChannel::B),
31            "C" => Ok(PicoChannel::C),
32            "D" => Ok(PicoChannel::D),
33            "E" => Ok(PicoChannel::E),
34            "F" => Ok(PicoChannel::F),
35            "G" => Ok(PicoChannel::G),
36            "H" => Ok(PicoChannel::H),
37            _ => Err(ParseError),
38        }
39    }
40}
41
42impl fmt::Display for PicoChannel {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{:?}", self)
45    }
46}
47
48impl From<PicoChannel> for u32 {
49    fn from(value: PicoChannel) -> Self {
50        num_traits::ToPrimitive::to_u32(&value).expect("Non-valid channel")
51    }
52}
53
54impl From<PicoChannel> for i32 {
55    fn from(value: PicoChannel) -> Self {
56        num_traits::ToPrimitive::to_i32(&value).expect("Non-valid channel")
57    }
58}
59
60impl From<PicoChannel> for i16 {
61    fn from(value: PicoChannel) -> Self {
62        num_traits::ToPrimitive::to_i16(&value).expect("Non-valid channel")
63    }
64}
65
66impl From<i32> for PicoChannel {
67    fn from(value: i32) -> Self {
68        num_traits::FromPrimitive::from_i32(value).expect("Non-valid channel")
69    }
70}
71
72impl From<u32> for PicoChannel {
73    fn from(value: u32) -> Self {
74        num_traits::FromPrimitive::from_u32(value).expect("Non-valid channel")
75    }
76}
77
78#[cfg(test)]
79mod channel_tests {
80    use super::*;
81
82    #[test]
83    fn channel_parse() {
84        assert_eq!(PicoChannel::from_str("a"), Ok(PicoChannel::A));
85        assert_eq!(PicoChannel::from_str("B "), Ok(PicoChannel::B));
86        assert_eq!(PicoChannel::from_str("x"), Err(ParseError));
87    }
88}
89
90/// Pico coupling options
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92#[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, PartialEq, Eq)]
93pub enum PicoCoupling {
94    AC = 0,
95    DC = 1,
96}
97
98impl FromStr for PicoCoupling {
99    type Err = ParseError;
100
101    fn from_str(input: &str) -> Result<Self, Self::Err> {
102        let input = input.replace(" ", "").to_uppercase();
103
104        match &input[..] {
105            "AC" => Ok(PicoCoupling::AC),
106            "DC" => Ok(PicoCoupling::DC),
107            _ => Err(ParseError),
108        }
109    }
110}
111
112impl fmt::Display for PicoCoupling {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        write!(f, "{:?}", self)
115    }
116}
117
118impl Default for PicoCoupling {
119    fn default() -> Self {
120        PicoCoupling::DC
121    }
122}
123
124impl From<PicoCoupling> for u32 {
125    fn from(value: PicoCoupling) -> Self {
126        num_traits::ToPrimitive::to_u32(&value).expect("Non-valid coupling")
127    }
128}
129
130impl From<PicoCoupling> for i32 {
131    fn from(value: PicoCoupling) -> Self {
132        num_traits::ToPrimitive::to_i32(&value).expect("Non-valid coupling")
133    }
134}
135
136impl From<PicoCoupling> for i16 {
137    fn from(value: PicoCoupling) -> Self {
138        num_traits::ToPrimitive::to_i16(&value).expect("Non-valid coupling")
139    }
140}
141
142impl From<i32> for PicoCoupling {
143    fn from(value: i32) -> Self {
144        num_traits::FromPrimitive::from_i32(value).expect("Non-valid channel")
145    }
146}
147
148#[cfg(test)]
149mod coupling_tests {
150    use super::*;
151
152    #[test]
153    fn coupling_parse() {
154        assert_eq!(PicoCoupling::from_str("ac"), Ok(PicoCoupling::AC));
155        assert_eq!(PicoCoupling::from_str("DC"), Ok(PicoCoupling::DC));
156        assert_eq!(PicoCoupling::from_str("ad"), Err(ParseError));
157    }
158}
159
160/// Driver downsampling mode
161#[allow(non_camel_case_types)]
162#[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive)]
163pub enum DownsampleMode {
164    NONE = 0,
165    AGGREGATE = 1,
166    DECIMATE = 2,
167    AVERAGE = 4,
168    DISTRIBUTION = 8,
169}
170
171impl From<DownsampleMode> for u32 {
172    fn from(value: DownsampleMode) -> Self {
173        num_traits::ToPrimitive::to_u32(&value).expect("Non-valid downsample mode")
174    }
175}
176
177impl From<DownsampleMode> for i32 {
178    fn from(value: DownsampleMode) -> Self {
179        num_traits::ToPrimitive::to_i32(&value).expect("Non-valid downsample mode")
180    }
181}
182
183/// Pico info options
184#[allow(non_camel_case_types)]
185#[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive)]
186pub enum PicoInfo {
187    DRIVER_VERSION = 0x0000_0000,
188    USB_VERSION = 0x0000_0001,
189    HARDWARE_VERSION = 0x0000_0002,
190    VARIANT_INFO = 0x0000_0003,
191    BATCH_AND_SERIAL = 0x0000_0004,
192    CAL_DATE = 0x0000_0005,
193    KERNEL_VERSION = 0x0000_0006,
194    DIGITAL_HARDWARE_VERSION = 0x0000_0007,
195    ANALOGUE_HARDWARE_VERSION = 0x0000_0008,
196    FIRMWARE_VERSION_1 = 0x0000_0009,
197    FIRMWARE_VERSION_2 = 0x0000_000A,
198    MAC_ADDRESS = 0x0000_000B,
199    SHADOW_CAL = 0x0000_000C,
200    IPP_VERSION = 0x0000_000D,
201    DRIVER_PATH = 0x0000_000E,
202    FIRMWARE_VERSION_3 = 0x0000_000F,
203    FRONT_PANEL_FIRMWARE_VERSION = 0x0000_0010,
204}
205
206impl From<PicoInfo> for u32 {
207    fn from(value: PicoInfo) -> Self {
208        num_traits::ToPrimitive::to_u32(&value).expect("Non-valid info type")
209    }
210}
211
212impl From<PicoInfo> for i16 {
213    fn from(value: PicoInfo) -> Self {
214        num_traits::ToPrimitive::to_i16(&value).expect("Non-valid info type")
215    }
216}