minidsp_protocol/
source.rs

1//! Utilities to get a mapping from the source name to the source id
2//! Most of this logic was translated from the cordova app
3
4use super::DeviceInfo;
5
6#[derive(Copy, Clone, Eq, PartialEq)]
7#[cfg_attr(feature = "debug", derive(Debug))]
8#[cfg_attr(
9    feature = "use_serde",
10    derive(
11        strum::EnumString,
12        strum::Display,
13        serde::Serialize,
14        serde::Deserialize,
15        schemars::JsonSchema,
16    )
17)]
18#[cfg_attr(feature = "use_serde", strum(serialize_all = "lowercase"))]
19pub enum Source {
20    NotInstalled,
21    Analog,
22    Toslink,
23    Spdif,
24    Usb,
25    Aesebu,
26    Rca,
27    Xlr,
28    Lan,
29    I2S,
30}
31
32impl Source {
33    /// Gets the available input sources with their numerical mapping for a given device
34    pub fn mapping(device_info: &DeviceInfo) -> &'static [(Source, u8)] {
35        use Source::*;
36        match device_info.hw_id {
37            2 | 11 => &[(Toslink, 0), (Spdif, 1)],
38            1 | 4 | 5 => &[(Spdif, 0), (Toslink, 1), (Aesebu, 2)],
39            10 if device_info.dsp_version == 100 || device_info.dsp_version == 101 => {
40                &[(Analog, 0), (Toslink, 1), (Usb, 2)]
41            }
42            10 => &[(I2S, 0), (Toslink, 1), (Usb, 2)],
43            14 => &[
44                (Toslink, 0),
45                (Spdif, 1),
46                (Aesebu, 2),
47                (Rca, 3),
48                (Xlr, 4),
49                (Usb, 5),
50                (Lan, 6),
51            ],
52            17 | 18 => &[(Toslink, 0), (Spdif, 1), (Aesebu, 2), (Usb, 3), (Lan, 4)],
53            _ => &[(NotInstalled, 0)],
54        }
55    }
56
57    pub fn from_id(id: u8, device_info: &DeviceInfo) -> Self {
58        for (src, src_id) in Self::mapping(device_info) {
59            if *src_id == id {
60                return *src;
61            }
62        }
63        Source::NotInstalled
64    }
65
66    pub fn to_id(self, device_info: &DeviceInfo) -> u8 {
67        for &(src, src_id) in Self::mapping(device_info) {
68            if src == self {
69                return src_id;
70            }
71        }
72        0
73    }
74}