Skip to main content

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    pub fn iter(
135        &self,
136    ) -> impl ExactSizeIterator<Item = ReadingChannel> + core::iter::FusedIterator + '_ {
137        match self.channels {
138            InnerReadingChannels::V1(ref channels) => channels.iter().copied(),
139            #[cfg(feature = "max-sample-min-count-2")]
140            InnerReadingChannels::V2(ref channels) => channels.iter().copied(),
141            #[cfg(feature = "max-sample-min-count-3")]
142            InnerReadingChannels::V3(ref channels) => channels.iter().copied(),
143            #[cfg(feature = "max-sample-min-count-4")]
144            InnerReadingChannels::V4(ref channels) => channels.iter().copied(),
145            #[cfg(feature = "max-sample-min-count-5")]
146            InnerReadingChannels::V5(ref channels) => channels.iter().copied(),
147            #[cfg(feature = "max-sample-min-count-6")]
148            InnerReadingChannels::V6(ref channels) => channels.iter().copied(),
149            #[cfg(feature = "max-sample-min-count-7")]
150            InnerReadingChannels::V7(ref channels) => channels.iter().copied(),
151            #[cfg(feature = "max-sample-min-count-8")]
152            InnerReadingChannels::V8(ref channels) => channels.iter().copied(),
153            #[cfg(feature = "max-sample-min-count-9")]
154            InnerReadingChannels::V9(ref channels) => channels.iter().copied(),
155            #[cfg(feature = "max-sample-min-count-10")]
156            InnerReadingChannels::V10(ref channels) => channels.iter().copied(),
157            #[cfg(feature = "max-sample-min-count-11")]
158            InnerReadingChannels::V11(ref channels) => channels.iter().copied(),
159            #[cfg(feature = "max-sample-min-count-12")]
160            InnerReadingChannels::V12(ref channels) => channels.iter().copied(),
161        }
162    }
163
164    /// Returns the first [`ReadingChannel`].
165    #[must_use]
166    pub fn first(&self) -> ReadingChannel {
167        if let Some(sample) = self.iter().next() {
168            sample
169        } else {
170            // NOTE(no-panic): there is always at least one sample.
171            unreachable!();
172        }
173    }
174}
175
176#[derive(Debug, Copy, Clone)]
177enum InnerReadingChannels {
178    V1([ReadingChannel; 1]),
179    #[cfg(feature = "max-sample-min-count-2")]
180    V2([ReadingChannel; 2]),
181    #[cfg(feature = "max-sample-min-count-3")]
182    V3([ReadingChannel; 3]),
183    #[cfg(feature = "max-sample-min-count-4")]
184    V4([ReadingChannel; 4]),
185    #[cfg(feature = "max-sample-min-count-5")]
186    V5([ReadingChannel; 5]),
187    #[cfg(feature = "max-sample-min-count-6")]
188    V6([ReadingChannel; 6]),
189    #[cfg(feature = "max-sample-min-count-7")]
190    V7([ReadingChannel; 7]),
191    #[cfg(feature = "max-sample-min-count-8")]
192    V8([ReadingChannel; 8]),
193    #[cfg(feature = "max-sample-min-count-9")]
194    V9([ReadingChannel; 9]),
195    #[cfg(feature = "max-sample-min-count-10")]
196    V10([ReadingChannel; 10]),
197    #[cfg(feature = "max-sample-min-count-11")]
198    V11([ReadingChannel; 11]),
199    #[cfg(feature = "max-sample-min-count-12")]
200    V12([ReadingChannel; 12]),
201}