1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Accelerometer readings - axis-specific acceleration measurements

use core::fmt::Debug;
use generic_array::{
    typenum::{U2, U3},
    ArrayLength, GenericArray,
};

/// Accelerometer readings
pub trait Reading: Copy + Debug + Default + PartialEq + Sized + Send + Sync {
    /// Type representing measured acceleration for a particular axis
    type Component: Copy + Sized + Into<f32>;

    /// Number of axes
    type Axes: ArrayLength<Self::Component>;

    /// Obtain an array of the acceleration components for each of the axes
    fn to_array(&self) -> GenericArray<Self::Component, Self::Axes>;
}

macro_rules! impl_2axis_reading {
    ($reading:ident, $component:ty, $doc:expr) => {
        #[doc=$doc]
        #[derive(Copy, Clone, Debug, Default, PartialEq)]
        pub struct $reading {
            /// X component
            pub x: $component,

            /// Y component
            pub y: $component,
        }

        impl $reading {
            /// Instantiate from X and Y components
            pub fn new(x: $component, y: $component) -> Self {
                $reading { x, y }
            }
        }

        impl Reading for $reading {
            type Component = $component;
            type Axes = U2;

            fn to_array(&self) -> GenericArray<$component, U2> {
                arr![$component; self.x, self.y]
            }
        }

        impl From<($component, $component)> for $reading {
            fn from(reading: ($component, $component)) -> Self {
                $reading::new(reading.0, reading.1)
            }
        }
    }
}

impl_2axis_reading!(I8x2, i8, "2-axis XY pair of `i8` values");
impl_2axis_reading!(I16x2, i16, "2-axis XY pair of `i16` values");
impl_2axis_reading!(U8x2, u8, "2-axis XY pair of `u8` values");
impl_2axis_reading!(U16x2, u16, "2-axis XY pair of `u16` values");
impl_2axis_reading!(F32x2, f32, "2-axis XY pair of `f32` values");

impl From<I8x2> for F32x2 {
    fn from(reading: I8x2) -> F32x2 {
        F32x2::new(reading.x.into(), reading.y.into())
    }
}

impl From<I16x2> for F32x2 {
    fn from(reading: I16x2) -> F32x2 {
        F32x2::new(reading.x.into(), reading.y.into())
    }
}

impl From<U8x2> for F32x2 {
    fn from(reading: U8x2) -> F32x2 {
        F32x2::new(reading.x.into(), reading.y.into())
    }
}

impl From<U16x2> for F32x2 {
    fn from(reading: U16x2) -> F32x2 {
        F32x2::new(reading.x.into(), reading.y.into())
    }
}

macro_rules! impl_3axis_reading {
    ($reading:ident, $component:ty, $doc:expr) => {
        #[doc=$doc]
        #[derive(Copy, Clone, Debug, Default, PartialEq)]
        pub struct $reading {
            /// X component
            pub x: $component,

            /// Y component
            pub y: $component,

            /// Z component
            pub z: $component,
        }

        impl $reading {
            /// Instantiate from X, Y, and Z components
            pub fn new(x: $component, y: $component, z: $component) -> Self {
                $reading { x, y, z }
            }
        }

        impl Reading for $reading {
            type Component = $component;
            type Axes = U3;

            fn to_array(&self) -> GenericArray<$component, U3> {
                arr![$component; self.x, self.y, self.z]
            }
        }

        impl From<($component, $component, $component)> for $reading {
            fn from(reading: ($component, $component, $component)) -> Self {
                $reading::new(reading.0, reading.1, reading.2)
            }
        }
    }
}

impl_3axis_reading!(I8x3, i8, "3-axis XYZ triple of `i8` values");
impl_3axis_reading!(I16x3, i16, "3-axis XYZ triple of `i16` values");
impl_3axis_reading!(U8x3, u8, "3-axis XYZ triple of `u8` values");
impl_3axis_reading!(U16x3, u16, "3-axis XYZ triple of `u16` values");
impl_3axis_reading!(F32x3, f32, "3-axis XYZ triple of `f32` values");

impl From<I8x3> for F32x3 {
    fn from(reading: I8x3) -> F32x3 {
        F32x3::new(reading.x.into(), reading.y.into(), reading.z.into())
    }
}

impl From<I16x3> for F32x3 {
    fn from(reading: I16x3) -> F32x3 {
        F32x3::new(reading.x.into(), reading.y.into(), reading.z.into())
    }
}

impl From<U8x3> for F32x3 {
    fn from(reading: U8x3) -> F32x3 {
        F32x3::new(reading.x.into(), reading.y.into(), reading.z.into())
    }
}

impl From<U16x3> for F32x3 {
    fn from(reading: U16x3) -> F32x3 {
        F32x3::new(reading.x.into(), reading.y.into(), reading.z.into())
    }
}