1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum DeviceType {
8 AD7124_4,
10 AD7124_8,
12 Unknown,
14}
15
16impl DeviceType {
17 pub fn from_device_id(device_id: u8) -> Self {
19 match device_id {
20 0x04 => DeviceType::AD7124_4,
21 0x12 => DeviceType::AD7124_8,
22 _ => DeviceType::Unknown,
23 }
24 }
25
26 pub fn device_id(self) -> Option<u8> {
28 match self {
29 DeviceType::AD7124_4 => Some(0x04),
30 DeviceType::AD7124_8 => Some(0x12),
31 DeviceType::Unknown => None,
32 }
33 }
34
35 pub fn supports_feature(self, feature: DeviceFeature) -> bool {
37 use DeviceFeature::*;
38 match (self, feature) {
39 (_, BasicOperation) => true, (DeviceType::AD7124_4, MultiChannel) => true, (DeviceType::AD7124_8, MultiChannel) => true, (_, InternalReference) => true, (_, ProgrammableGain) => true, (_, SelfTest) => true, (_, LowPowerMode) => true, (_, HighPrecision) => true, (_, DiagnosticMode) => true, (_, BurnoutCurrent) => true, _ => false,
50 }
51 }
52
53 pub fn max_channels(self) -> u8 {
55 match self {
56 DeviceType::AD7124_4 => 4,
57 DeviceType::AD7124_8 => 8,
58 DeviceType::Unknown => 0,
59 }
60 }
61
62 pub fn max_spi_frequency_hz(self) -> u32 {
64 match self {
65 DeviceType::AD7124_4 | DeviceType::AD7124_8 => 5_000_000, DeviceType::Unknown => 1_000_000, }
68 }
69
70 pub fn description(self) -> &'static str {
72 match self {
73 DeviceType::AD7124_4 => "AD7124-4: 4-channel, 24-bit Sigma-Delta ADC",
74 DeviceType::AD7124_8 => "AD7124-8: 8-channel, 24-bit Sigma-Delta ADC",
75 DeviceType::Unknown => "Unknown or unsupported AD7124 device",
76 }
77 }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum DeviceFeature {
83 BasicOperation,
85 MultiChannel,
87 InternalReference,
89 ProgrammableGain,
91 SelfTest,
93 LowPowerMode,
95 HighPrecision,
97 DiagnosticMode,
99 BurnoutCurrent,
101 ContinuousMode,
103 SingleShotMode,
105 DigitalFiltering,
107}
108
109#[derive(Debug, Clone, Copy)]
111pub struct DeviceCapabilities {
112 pub device_type: DeviceType,
114 pub max_channels: u8,
116 pub max_spi_frequency_hz: u32,
118 pub internal_reference: bool,
120 pub programmable_gain: bool,
122 pub self_test: bool,
124 pub low_power_mode: bool,
126 pub high_precision: bool,
128 pub diagnostic_mode: bool,
130 pub burnout_current: bool,
132 pub continuous_mode: bool,
134 pub single_shot_mode: bool,
136 pub digital_filtering: bool,
138}
139
140impl DeviceCapabilities {
141 pub fn for_device(device_type: DeviceType) -> Self {
143 Self {
144 device_type,
145 max_channels: device_type.max_channels(),
146 max_spi_frequency_hz: device_type.max_spi_frequency_hz(),
147 internal_reference: device_type.supports_feature(DeviceFeature::InternalReference),
148 programmable_gain: device_type.supports_feature(DeviceFeature::ProgrammableGain),
149 self_test: device_type.supports_feature(DeviceFeature::SelfTest),
150 low_power_mode: device_type.supports_feature(DeviceFeature::LowPowerMode),
151 high_precision: device_type.supports_feature(DeviceFeature::HighPrecision),
152 diagnostic_mode: device_type.supports_feature(DeviceFeature::DiagnosticMode),
153 burnout_current: device_type.supports_feature(DeviceFeature::BurnoutCurrent),
154 continuous_mode: device_type.supports_feature(DeviceFeature::ContinuousMode),
155 single_shot_mode: device_type.supports_feature(DeviceFeature::SingleShotMode),
156 digital_filtering: device_type.supports_feature(DeviceFeature::DigitalFiltering),
157 }
158 }
159
160 pub fn supports(&self, feature: DeviceFeature) -> bool {
162 self.device_type.supports_feature(feature)
163 }
164
165 pub fn validate_channel(&self, channel: u8) -> bool {
167 channel < self.max_channels
168 }
169}
170
171impl Default for DeviceCapabilities {
172 fn default() -> Self {
173 Self::for_device(DeviceType::Unknown)
174 }
175}