ariel_os_sensors/sensor/
reading_channels.rs

1use super::ReadingChannel;
2
3/// Metadata required to interpret samples returned by [`Sensor::wait_for_reading()`].
4///
5/// # For implementors
6///
7/// [`ReadingChannels`] can be instantiated using [`From`] implementations.
8///
9/// Sensor driver crates must enable the appropriate `max-sample-min-count-*` Cargo feature
10/// on this crate.
11/// For instance, a 3-axis accelerometer driver crate must enable `max-sample-min-count-3`
12/// to be able to expose 3 [`ReadingChannel`]s.
13///
14/// [`Sensor::wait_for_reading()`]: super::Sensor::wait_for_reading()
15#[derive(Debug, Copy, Clone)]
16pub struct ReadingChannels {
17    channels: InnerReadingChannels,
18}
19
20impl From<[ReadingChannel; 1]> for ReadingChannels {
21    fn from(value: [ReadingChannel; 1]) -> Self {
22        Self {
23            channels: InnerReadingChannels::V1(value),
24        }
25    }
26}
27
28#[cfg(feature = "max-sample-min-count-2")]
29impl From<[ReadingChannel; 2]> for ReadingChannels {
30    fn from(value: [ReadingChannel; 2]) -> Self {
31        Self {
32            channels: InnerReadingChannels::V2(value),
33        }
34    }
35}
36
37#[cfg(feature = "max-sample-min-count-3")]
38impl From<[ReadingChannel; 3]> for ReadingChannels {
39    fn from(value: [ReadingChannel; 3]) -> Self {
40        Self {
41            channels: InnerReadingChannels::V3(value),
42        }
43    }
44}
45
46#[cfg(feature = "max-sample-min-count-4")]
47impl From<[ReadingChannel; 4]> for ReadingChannels {
48    fn from(value: [ReadingChannel; 4]) -> Self {
49        Self {
50            channels: InnerReadingChannels::V4(value),
51        }
52    }
53}
54
55#[cfg(feature = "max-sample-min-count-5")]
56impl From<[ReadingChannel; 5]> for ReadingChannels {
57    fn from(value: [ReadingChannel; 5]) -> Self {
58        Self {
59            channels: InnerReadingChannels::V5(value),
60        }
61    }
62}
63
64#[cfg(feature = "max-sample-min-count-6")]
65impl From<[ReadingChannel; 6]> for ReadingChannels {
66    fn from(value: [ReadingChannel; 6]) -> Self {
67        Self {
68            channels: InnerReadingChannels::V6(value),
69        }
70    }
71}
72
73#[cfg(feature = "max-sample-min-count-7")]
74impl From<[ReadingChannel; 7]> for ReadingChannels {
75    fn from(value: [ReadingChannel; 7]) -> Self {
76        Self {
77            channels: InnerReadingChannels::V7(value),
78        }
79    }
80}
81
82#[cfg(feature = "max-sample-min-count-8")]
83impl From<[ReadingChannel; 8]> for ReadingChannels {
84    fn from(value: [ReadingChannel; 8]) -> Self {
85        Self {
86            channels: InnerReadingChannels::V8(value),
87        }
88    }
89}
90
91#[cfg(feature = "max-sample-min-count-9")]
92impl From<[ReadingChannel; 9]> for ReadingChannels {
93    fn from(value: [ReadingChannel; 9]) -> Self {
94        Self {
95            channels: InnerReadingChannels::V9(value),
96        }
97    }
98}
99
100#[cfg(feature = "max-sample-min-count-10")]
101impl From<[ReadingChannel; 10]> for ReadingChannels {
102    fn from(value: [ReadingChannel; 10]) -> Self {
103        Self {
104            channels: InnerReadingChannels::V10(value),
105        }
106    }
107}
108
109#[cfg(feature = "max-sample-min-count-11")]
110impl From<[ReadingChannel; 11]> for ReadingChannels {
111    fn from(value: [ReadingChannel; 11]) -> Self {
112        Self {
113            channels: InnerReadingChannels::V11(value),
114        }
115    }
116}
117
118#[cfg(feature = "max-sample-min-count-12")]
119impl From<[ReadingChannel; 12]> for ReadingChannels {
120    fn from(value: [ReadingChannel; 12]) -> Self {
121        Self {
122            channels: InnerReadingChannels::V12(value),
123        }
124    }
125}
126
127impl ReadingChannels {
128    /// Returns an iterator over the underlying [`ReadingChannel`] items.
129    ///
130    /// For a given sensor driver, the number and order of items match the one of
131    /// [`Samples`].
132    ///
133    /// [`Samples`]: super::Samples
134    #[must_use]
135    pub fn iter(
136        &self,
137    ) -> impl ExactSizeIterator<Item = ReadingChannel> + core::iter::FusedIterator + '_ {
138        match self.channels {
139            InnerReadingChannels::V1(ref channels) => channels.iter().copied(),
140            #[cfg(feature = "max-sample-min-count-2")]
141            InnerReadingChannels::V2(ref channels) => channels.iter().copied(),
142            #[cfg(feature = "max-sample-min-count-3")]
143            InnerReadingChannels::V3(ref channels) => channels.iter().copied(),
144            #[cfg(feature = "max-sample-min-count-4")]
145            InnerReadingChannels::V4(ref channels) => channels.iter().copied(),
146            #[cfg(feature = "max-sample-min-count-5")]
147            InnerReadingChannels::V5(ref channels) => channels.iter().copied(),
148            #[cfg(feature = "max-sample-min-count-6")]
149            InnerReadingChannels::V6(ref channels) => channels.iter().copied(),
150            #[cfg(feature = "max-sample-min-count-7")]
151            InnerReadingChannels::V7(ref channels) => channels.iter().copied(),
152            #[cfg(feature = "max-sample-min-count-8")]
153            InnerReadingChannels::V8(ref channels) => channels.iter().copied(),
154            #[cfg(feature = "max-sample-min-count-9")]
155            InnerReadingChannels::V9(ref channels) => channels.iter().copied(),
156            #[cfg(feature = "max-sample-min-count-10")]
157            InnerReadingChannels::V10(ref channels) => channels.iter().copied(),
158            #[cfg(feature = "max-sample-min-count-11")]
159            InnerReadingChannels::V11(ref channels) => channels.iter().copied(),
160            #[cfg(feature = "max-sample-min-count-12")]
161            InnerReadingChannels::V12(ref channels) => channels.iter().copied(),
162        }
163    }
164
165    /// Returns the first [`ReadingChannel`].
166    #[must_use]
167    pub fn first(&self) -> ReadingChannel {
168        if let Some(sample) = self.iter().next() {
169            sample
170        } else {
171            // NOTE(no-panic): there is always at least one sample.
172            unreachable!();
173        }
174    }
175}
176
177#[derive(Debug, Copy, Clone)]
178enum InnerReadingChannels {
179    V1([ReadingChannel; 1]),
180    #[cfg(feature = "max-sample-min-count-2")]
181    V2([ReadingChannel; 2]),
182    #[cfg(feature = "max-sample-min-count-3")]
183    V3([ReadingChannel; 3]),
184    #[cfg(feature = "max-sample-min-count-4")]
185    V4([ReadingChannel; 4]),
186    #[cfg(feature = "max-sample-min-count-5")]
187    V5([ReadingChannel; 5]),
188    #[cfg(feature = "max-sample-min-count-6")]
189    V6([ReadingChannel; 6]),
190    #[cfg(feature = "max-sample-min-count-7")]
191    V7([ReadingChannel; 7]),
192    #[cfg(feature = "max-sample-min-count-8")]
193    V8([ReadingChannel; 8]),
194    #[cfg(feature = "max-sample-min-count-9")]
195    V9([ReadingChannel; 9]),
196    #[cfg(feature = "max-sample-min-count-10")]
197    V10([ReadingChannel; 10]),
198    #[cfg(feature = "max-sample-min-count-11")]
199    V11([ReadingChannel; 11]),
200    #[cfg(feature = "max-sample-min-count-12")]
201    V12([ReadingChannel; 12]),
202}