1use std::fmt;
7use std::str::FromStr;
8
9use crate::error::{Error, Result};
10
11pub const NUM_CHANNELS: usize = 12;
13pub const NUM_SLOTS: usize = 3;
15
16#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
27pub struct Channel(u8);
28
29#[allow(missing_docs)]
30impl Channel {
31 pub const S0A: Channel = Channel(0);
32 pub const S0B: Channel = Channel(1);
33 pub const S0C: Channel = Channel(2);
34 pub const S0D: Channel = Channel(3);
35 pub const S1A: Channel = Channel(4);
36 pub const S1B: Channel = Channel(5);
37 pub const S1C: Channel = Channel(6);
38 pub const S1D: Channel = Channel(7);
39 pub const S2A: Channel = Channel(8);
40 pub const S2B: Channel = Channel(9);
41 pub const S2C: Channel = Channel(10);
42 pub const S2D: Channel = Channel(11);
43}
44
45impl Channel {
46 pub const ALL: [Channel; NUM_CHANNELS] = [
48 Channel(0), Channel(1), Channel(2), Channel(3), Channel(4), Channel(5),
49 Channel(6), Channel(7), Channel(8), Channel(9), Channel(10), Channel(11),
50 ];
51
52 pub fn new(index: usize) -> Result<Channel> {
54 if index < NUM_CHANNELS {
55 Ok(Channel(index as u8))
56 } else {
57 Err(Error::InvalidArgument(format!("channel index {index} (0..=11)")))
58 }
59 }
60
61 pub fn from_slot(slot: usize, input: usize) -> Result<Channel> {
63 if slot < NUM_SLOTS && input < 4 {
64 Ok(Channel((slot * 4 + input) as u8))
65 } else {
66 Err(Error::InvalidArgument(format!("slot {slot} input {input}")))
67 }
68 }
69
70 pub fn index(self) -> usize {
72 self.0 as usize
73 }
74
75 pub fn slot(self) -> usize {
77 self.0 as usize / 4
78 }
79
80 pub fn input(self) -> usize {
82 self.0 as usize % 4
83 }
84}
85
86impl fmt::Display for Channel {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 write!(f, "S{}{}", self.slot(), (b'A' + self.input() as u8) as char)
89 }
90}
91
92impl FromStr for Channel {
93 type Err = Error;
94 fn from_str(s: &str) -> Result<Channel> {
96 let t = s.trim();
97 let b = t.as_bytes();
98 if b.len() == 3 && (b[0] == b'S' || b[0] == b's') {
99 let slot = b[1].wrapping_sub(b'0') as usize;
100 let input = b[2].to_ascii_uppercase().wrapping_sub(b'A') as usize;
101 return Channel::from_slot(slot, input).map_err(|_| Error::InvalidArgument(format!("input name '{t}'")));
102 }
103 match t.parse::<usize>() {
104 Ok(i) => Channel::new(i),
105 Err(_) => Err(Error::InvalidArgument(format!("input name '{t}'"))),
106 }
107 }
108}
109
110#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
121pub struct ChannelMask(u16);
122
123impl ChannelMask {
124 pub const NONE: ChannelMask = ChannelMask(0);
126 pub const ALL: ChannelMask = ChannelMask(0xFFF);
128
129 pub fn from_bits(bits: u16) -> ChannelMask {
131 ChannelMask(bits & 0xFFF)
132 }
133
134 pub fn slot(slot: usize) -> Result<ChannelMask> {
136 if slot < NUM_SLOTS {
137 Ok(ChannelMask(0xF << (4 * slot)))
138 } else {
139 Err(Error::InvalidArgument(format!("slot {slot} (0..=2)")))
140 }
141 }
142
143 pub fn bits(self) -> u16 {
145 self.0
146 }
147
148 pub fn contains(self, ch: Channel) -> bool {
150 self.0 >> ch.index() & 1 != 0
151 }
152
153 pub fn with(self, ch: Channel) -> ChannelMask {
155 ChannelMask(self.0 | 1 << ch.index())
156 }
157
158 pub fn len(self) -> usize {
160 self.0.count_ones() as usize
161 }
162
163 pub fn is_empty(self) -> bool {
165 self.0 == 0
166 }
167
168 pub fn iter(self) -> impl Iterator<Item = Channel> {
170 Channel::ALL.into_iter().filter(move |c| self.contains(*c))
171 }
172}
173
174impl FromIterator<Channel> for ChannelMask {
175 fn from_iter<I: IntoIterator<Item = Channel>>(iter: I) -> Self {
176 ChannelMask(iter.into_iter().fold(0, |m, c| m | 1 << c.index()))
177 }
178}
179
180impl From<Channel> for ChannelMask {
181 fn from(c: Channel) -> Self {
182 ChannelMask(1 << c.index())
183 }
184}
185
186#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
188pub enum Range {
189 V10,
191 V5,
193 V2,
195}
196
197impl Range {
198 pub fn volts(self) -> u32 {
200 match self {
201 Range::V10 => 10,
202 Range::V5 => 5,
203 Range::V2 => 2,
204 }
205 }
206
207 pub fn from_volts(v: u32) -> Result<Range> {
209 match v {
210 10 => Ok(Range::V10),
211 5 => Ok(Range::V5),
212 2 => Ok(Range::V2),
213 _ => Err(Error::InvalidArgument(format!("range {v} V (10, 5 or 2)"))),
214 }
215 }
216
217 pub fn typical_lsb_per_volt(self) -> f64 {
219 match self {
220 Range::V10 => 637.0,
221 Range::V5 => 1280.0,
222 Range::V2 => 3180.0,
223 }
224 }
225
226 pub fn code_to_volts(self, code: f64) -> f64 {
228 code / self.typical_lsb_per_volt()
229 }
230}
231
232impl fmt::Display for Range {
233 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234 write!(f, "+-{} V", self.volts())
235 }
236}
237
238#[derive(Clone, Copy, Debug, PartialEq, Eq)]
240pub enum Target {
241 Channel(Channel),
243 Slot(u8),
245 All,
247}
248
249impl fmt::Display for Target {
250 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251 match self {
252 Target::Channel(c) => write!(f, "{c}"),
253 Target::Slot(s) => write!(f, "S{s}"),
254 Target::All => write!(f, "ALL"),
255 }
256 }
257}
258
259impl From<Channel> for Target {
260 fn from(c: Channel) -> Self {
261 Target::Channel(c)
262 }
263}