lis2dtw12/
register_data.rs

1use crate::registers::*;
2
3/// Struct representation of the Status register
4#[derive(Debug, Copy, Clone)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub struct Status {
7    /// FIFO threshold status
8    ///
9    /// false: FIFO filling is lower than the threshold level
10    ///
11    /// true: FIFO filling is equal or higher than the threshold level
12    pub fifo_threshold: bool,
13    /// Wake up event detection
14    ///
15    /// false: no wake-up event detected
16    ///
17    /// true: wake-up event detected
18    pub wake_up_event: bool,
19    /// Sleep event status
20    ///
21    /// false: no sleep event detected
22    ///
23    /// true: sleep event detected
24    pub sleep_event: bool,
25    /// Double-tap event status
26    ///
27    /// false: no tap event detected
28    ///
29    /// true: tap event detected
30    pub double_tap_event: bool,
31    /// Single-tap event status
32    ///
33    /// false: no tap event detected
34    ///
35    /// true: tap event detected
36    pub single_tap_event: bool,
37    /// Source of change in position (portrait/landscape/face-up/face-down)
38    ///
39    /// false: no change in position detected
40    ///
41    /// true: change in position detected
42    pub position_change_event: bool,
43    /// Free-fall event detection status
44    ///
45    /// false: no free-fall event detected
46    ///
47    /// true: free-fall event detected
48    pub free_fall_event: bool,
49    /// Data ready status
50    ///
51    /// false: no data is available
52    ///
53    /// true: X-, Y- and Z-axis new data available
54    pub data_ready: bool,
55}
56
57impl From<u8> for Status {
58    fn from(value: u8) -> Self {
59        Self {
60            fifo_threshold: value & FIFO_THS != 0,
61            wake_up_event: value & WU_IA != 0,
62            sleep_event: value & SLEEP_STATE != 0,
63            double_tap_event: value & DOUBLE_TAP != 0,
64            single_tap_event: value & SINGLE_TAP != 0,
65            position_change_event: value & D6D_IA != 0,
66            free_fall_event: value & FF_IA != 0,
67            data_ready: value & DRDY != 0,
68        }
69    }
70}
71
72/// Struct representation of the Status DUP (Event status) register
73#[derive(Debug, Copy, Clone)]
74#[cfg_attr(feature = "defmt", derive(defmt::Format))]
75pub struct EventStatus {
76    /// FIFO threshold status
77    ///
78    /// false: FIFO is not completely filled
79    ///
80    /// true: FIFO is overrun
81    pub fifo_overrun: bool,
82    /// Temperature data ready status
83    ///
84    /// false: data not available
85    ///
86    /// true: new set of data is available
87    pub temperature_data_ready: bool,
88    /// Sleep event status
89    ///
90    /// false: no sleep event detected
91    ///
92    /// true: sleep event detected
93    pub sleep_event: bool,
94    /// Double-tap event status
95    ///
96    /// false: no tap event detected
97    ///
98    /// true: tap event detected
99    pub double_tap_event: bool,
100    /// Single-tap event status
101    ///
102    /// false: no tap event detected
103    ///
104    /// true: tap event detected
105    pub single_tap_event: bool,
106    /// Source of change in position (portrait/landscape/face-up/face-down)
107    ///
108    /// false: no change in position detected
109    ///
110    /// true: change in position detected
111    pub position_change_event: bool,
112    /// Free-fall event detection status
113    ///
114    /// false: no free-fall event detected
115    ///
116    /// true: free-fall event detected
117    pub free_fall_event: bool,
118    /// Data ready status
119    ///
120    /// false: no data is available
121    ///
122    /// true: X-, Y- and Z-axis new data available
123    pub data_ready: bool,
124}
125
126impl From<u8> for EventStatus {
127    fn from(value: u8) -> Self {
128        Self {
129            fifo_overrun: value & OVR != 0,
130            temperature_data_ready: value & DRDY_T != 0,
131            sleep_event: value & SLEEP_STATE_IA != 0,
132            double_tap_event: value & DOUBLE_TAP != 0,
133            single_tap_event: value & SINGLE_TAP != 0,
134            position_change_event: value & D6D_IA != 0,
135            free_fall_event: value & FF_IA != 0,
136            data_ready: value & DRDY != 0,
137        }
138    }
139}
140
141/// Acceleration data
142#[derive(Clone, Copy, Debug)]
143#[cfg_attr(feature = "defmt", derive(defmt::Format))]
144pub struct AccelerationData {
145    /// X-axis acceleration
146    pub x: f32,
147    /// Y-axis acceleration
148    pub y: f32,
149    /// Z-axis acceleration
150    pub z: f32,
151}
152
153/// RAW acceleration data
154#[derive(Clone, Copy, Debug)]
155#[cfg_attr(feature = "defmt", derive(defmt::Format))]
156pub struct RawAccelerationData {
157    /// X-axis acceleration
158    pub x: i16,
159    /// Y-axis acceleration
160    pub y: i16,
161    /// Z-axis acceleration
162    pub z: i16,
163}
164
165/// FIFO Samples Status
166#[derive(Debug, Copy, Clone)]
167#[cfg_attr(feature = "defmt", derive(defmt::Format))]
168pub struct FifoSamplesStatus {
169    /// FIFO threshold status
170    ///
171    /// false: FIFO filling is lower than the threshold level
172    ///
173    /// true: FIFO filling is equal or higher than the threshold level
174    pub threshold: bool,
175    /// FIFO overrun status
176    ///
177    /// false: FIFO is not overrun
178    ///
179    /// true: FIFO is overrun
180    pub overrun: bool,
181    /// Number of unread samples in FIFO
182    pub samples: u8,
183}
184
185impl From<u8> for FifoSamplesStatus {
186    fn from(value: u8) -> Self {
187        Self {
188            threshold: value & FIFO_FTH != 0,
189            overrun: value & FIFO_OVR != 0,
190            samples: value & FIFO_DIFF,
191        }
192    }
193}
194
195/// Wake-up source
196#[derive(Debug, Copy, Clone)]
197#[cfg_attr(feature = "defmt", derive(defmt::Format))]
198pub struct WakeUpSource {
199    /// Free-fall event detection status
200    ///
201    /// false: no free-fall event detected
202    ///
203    /// true: free-fall event detected
204    pub free_fall_event: bool,
205    /// Sleep event status
206    ///
207    /// false: no sleep event detected
208    ///
209    /// true: sleep event detected
210    pub sleep_event: bool,
211    /// Wake up event detection
212    ///
213    /// false: no wake-up event detected
214    ///
215    /// true: wake-up event detected
216    pub wake_up_event: bool,
217    /// X-axis wake-up event detection
218    ///
219    /// false: no wake-up event detected
220    ///
221    /// true: wake-up event on X-axis detected
222    pub x_wake_up_event: bool,
223    /// Y-axis wake-up event detection
224    ///
225    /// false: no wake-up event detected
226    ///
227    /// true: wake-up event on Y-axis detected
228    pub y_wake_up_event: bool,
229    /// Z-axis wake-up event detection
230    ///
231    /// false: no wake-up event detected
232    ///
233    /// true: wake-up event on Z-axis detected
234    pub z_wake_up_event: bool,
235}
236
237impl From<u8> for WakeUpSource {
238    fn from(value: u8) -> Self {
239        Self {
240            free_fall_event: value & WAKE_UP_FF_IA != 0,
241            sleep_event: value & WAKE_UP_SLEEP_STATE_IA != 0,
242            wake_up_event: value & WAKE_UP_WU_IA != 0,
243            x_wake_up_event: value & X_WU != 0,
244            y_wake_up_event: value & Y_WU != 0,
245            z_wake_up_event: value & Z_WU != 0,
246        }
247    }
248}
249
250/// Sign of the tap event
251#[derive(Debug, Copy, Clone, PartialEq, Eq)]
252#[cfg_attr(feature = "defmt", derive(defmt::Format))]
253pub enum Sign {
254    /// Positive sign
255    Positive,
256    /// Negative sign
257    Negative,
258}
259
260/// Tap source
261#[derive(Debug, Copy, Clone)]
262#[cfg_attr(feature = "defmt", derive(defmt::Format))]
263pub struct TapSource {
264    /// Tap event status
265    ///
266    /// false: no tap event detected
267    ///
268    /// true: tap event detected
269    pub tap_event: bool,
270    /// Single-tap event status
271    ///
272    /// false: no tap event detected
273    ///
274    /// true: tap event detected
275    pub single_tap_event: bool,
276    /// Double-tap event status
277    ///
278    /// false: no tap event detected
279    ///
280    /// true: tap event detected
281    pub double_tap_event: bool,
282    /// Tap sign
283    pub tap_sign: Sign,
284    /// X-axis tap event detection
285    ///
286    /// false: no tap event detected
287    ///
288    /// true: tap event on X-axis detected
289    pub x_tap_event: bool,
290    /// Y-axis tap event detection
291    ///
292    /// false: no tap event detected
293    ///
294    /// true: tap event on Y-axis detected
295    pub y_tap_event: bool,
296    /// Z-axis tap event detection
297    ///
298    /// false: no tap event detected
299    ///
300    /// true: tap event on Z-axis detected
301    pub z_tap_event: bool,
302}
303
304impl From<u8> for TapSource {
305    fn from(value: u8) -> Self {
306        Self {
307            tap_event: value & TAP_IA != 0,
308            single_tap_event: value & TAP_SRC_SINGLE_TAP != 0,
309            double_tap_event: value & TAP_SRC_DOUBLE_TAP != 0,
310            tap_sign: if value & TAP_SIGN == 0 {
311                Sign::Positive
312            } else {
313                Sign::Negative
314            },
315            x_tap_event: value & X_TAP != 0,
316            y_tap_event: value & Y_TAP != 0,
317            z_tap_event: value & Z_TAP != 0,
318        }
319    }
320}
321
322/// 6D source
323#[derive(Clone, Copy, Debug)]
324#[cfg_attr(feature = "defmt", derive(defmt::Format))]
325pub struct SixDSource {
326    /// Source of change in position (portrait/landscape/face-up/face-down)
327    ///
328    /// false: no change in position detected
329    ///
330    /// true: change in position detected
331    pub position_change_event: bool,
332    /// ZH over threshold
333    ///
334    /// false: ZH is not over threshold
335    ///
336    /// true: ZH is over threshold
337    pub zh_over_threshold: bool,
338    /// ZL over threshold
339    ///
340    /// false: ZL is not over threshold
341    ///
342    /// true: ZL is over threshold
343    pub zl_over_threshold: bool,
344    /// YH over threshold
345    ///
346    /// false: YH is not over threshold
347    ///
348    /// true: YH is over threshold
349    pub yh_over_threshold: bool,
350    /// YL over threshold
351    ///
352    /// false: YL is not over threshold
353    ///
354    /// true: YL is over threshold
355    pub yl_over_threshold: bool,
356    /// XH over threshold
357    ///
358    /// false: XH is not over threshold
359    ///
360    /// true: XH is over threshold
361    pub xh_over_threshold: bool,
362    /// XL over threshold
363    ///
364    /// false: XL is not over threshold
365    ///
366    /// true: XL is over threshold
367    pub xl_over_threshold: bool,
368}
369
370impl From<u8> for SixDSource {
371    fn from(value: u8) -> Self {
372        Self {
373            position_change_event: value & IA_6D != 0,
374            zh_over_threshold: value & ZH != 0,
375            zl_over_threshold: value & ZL != 0,
376            yh_over_threshold: value & YH != 0,
377            yl_over_threshold: value & YL != 0,
378            xh_over_threshold: value & XH != 0,
379            xl_over_threshold: value & XL != 0,
380        }
381    }
382}
383
384/// Struct representation of the All Interrupt Sources register
385///
386/// This register is a combination of all interrupt sources
387#[derive(Debug, Copy, Clone)]
388#[cfg_attr(feature = "defmt", derive(defmt::Format))]
389pub struct AllInterruptSources {
390    /// Sleep change interrupt
391    ///
392    /// false: no sleep change interrupt
393    ///
394    /// true: sleep change interrupt
395    pub sleep_change_interrupt: bool,
396    /// 6D interrupt
397    ///
398    /// false: no 6D interrupt
399    ///
400    /// true: 6D interrupt
401    pub six_d_interrupt: bool,
402    /// Double-tap interrupt
403    ///
404    /// false: no double-tap interrupt
405    ///
406    /// true: double-tap interrupt
407    pub double_tap_interrupt: bool,
408    /// Single-tap interrupt
409    ///
410    /// false: no single-tap interrupt
411    ///
412    /// true: single-tap interrupt
413    pub single_tap_interrupt: bool,
414    /// Wake-up interrupt
415    ///
416    /// false: no wake-up interrupt
417    ///
418    /// true: wake-up interrupt
419    pub wake_up_interrupt: bool,
420    /// Free-fall interrupt
421    ///
422    /// false: no free-fall interrupt
423    ///
424    /// true: free-fall interrupt
425    pub free_fall_interrupt: bool,
426}
427
428impl From<u8> for AllInterruptSources {
429    fn from(value: u8) -> Self {
430        Self {
431            sleep_change_interrupt: value & ALL_INT_SLEEP_CHANGE_IA != 0,
432            six_d_interrupt: value & ALL_INT_6D_IA != 0,
433            double_tap_interrupt: value & ALL_INT_DOUBLE_TAP != 0,
434            single_tap_interrupt: value & ALL_INT_SINGLE_TAP != 0,
435            wake_up_interrupt: value & ALL_INT_WU_IA != 0,
436            free_fall_interrupt: value & ALL_INT_FF_IA != 0,
437        }
438    }
439}
440
441/// Struct representation of the 5 SRC registers combined
442///
443/// can be read with [crate::Lis2dtw12::get_all_sources]
444#[derive(Debug, Copy, Clone)]
445#[cfg_attr(feature = "defmt", derive(defmt::Format))]
446pub struct AllSources {
447    /// Event status register (see [`EventStatus`](crate::register_data::EventStatus))
448    pub event_status: EventStatus,
449    /// Wake up source register (see [`WakeUpSource`](crate::register_data::WakeUpSource))
450    pub wake_up_source: WakeUpSource,
451    /// Tap source register (see [`TapSource`](crate::register_data::TapSource))
452    pub tap_source: TapSource,
453    /// 6D source register (see [`SixDSource`](crate::register_data::SixDSource))
454    pub six_d_source: SixDSource,
455    /// All interrupt sources register (see [`AllInterruptSources`](crate::register_data::AllInterruptSources))
456    pub all_interrupt_sources: AllInterruptSources,
457}
458
459impl From<[u8; 5]> for AllSources {
460    fn from(value: [u8; 5]) -> Self {
461        Self {
462            event_status: EventStatus::from(value[0]),
463            wake_up_source: WakeUpSource::from(value[1]),
464            tap_source: TapSource::from(value[2]),
465            six_d_source: SixDSource::from(value[3]),
466            all_interrupt_sources: AllInterruptSources::from(value[4]),
467        }
468    }
469}