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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#![allow(non_camel_case_types)]

#[derive(Copy, Clone, Debug)]
pub enum Range {
    _0d5G = 0b00,
    _1d0G = 0b01,
    _2d0G = 0b10,
    _4d0G = 0b11,
}

impl Range {
    pub fn val(self) -> u8 {
        self as u8
    }
}

impl From<Range> for f32 {
    fn from(range: Range) -> f32 {
        match range {
            Range::_0d5G => 0.512,
            Range::_1d0G => 1.024,
            Range::_2d0G => 2.048,
            Range::_4d0G => 4.096,
        }
    }
}

impl Default for Range {
    fn default() -> Self {
        Range::_0d5G
    }
}

#[derive(Copy, Clone, Debug)]
/// Output data rate (odr) and Low pass filter corner frequency (lpf)
pub enum OutputDataRate {
    /// odr = 3200 Hz and lpf = 1600 Hz
    ODR_3200_HZ = 0b1111,
    /// odr = 1600 Hz and lpf = 800 Hz
    ODR_1600_HZ = 0b1110,
    /// odr = 800 Hz and lpf = 400 Hz
    ODR_800_HZ = 0b1101,
    /// odr = 400 Hz and lpf = 200 Hz
    ODR_400_HZ = 0b1100,
    /// odr = 200 Hz and lpf = 100 Hz
    ODR_200_HZ = 0b1011,
    /// odr = 100 Hz and lpf = 50 Hz
    ODR_100_HZ = 0b1010,
    /// odr = 50 Hz and lpf = 25 Hz
    ODR_50_HZ = 0b1001,
    /// odr = 25 Hz and lpf = 12.5 Hz
    ODR_25_HZ = 0b1000,
    /// odr = 12.5 Hz and lpf = 6.25 Hz
    ODR_12d5_HZ = 0b0111,
    /// odr = 6.25 Hz and lpf = 3.125 Hz
    ODR_6d25_HZ = 0b0110,
}

impl OutputDataRate {
    pub fn val(self) -> u8 {
        self as u8
    }
}

impl From<OutputDataRate> for f32 {
    fn from(rate: OutputDataRate) -> f32 {
        match rate {
            OutputDataRate::ODR_3200_HZ => 3200.00,
            OutputDataRate::ODR_1600_HZ => 1600.00,
            OutputDataRate::ODR_800_HZ => 800.00,
            OutputDataRate::ODR_400_HZ => 400.00,
            OutputDataRate::ODR_200_HZ => 200.00,
            OutputDataRate::ODR_100_HZ => 100.00,
            OutputDataRate::ODR_50_HZ => 50.00,
            OutputDataRate::ODR_25_HZ => 25.00,
            OutputDataRate::ODR_12d5_HZ => 12.50,
            OutputDataRate::ODR_6d25_HZ => 6.25,
        }
    }
}

impl Default for OutputDataRate {
    fn default() -> Self {
        OutputDataRate::ODR_100_HZ
    }
}

#[derive(Copy, Clone, Debug)]
pub enum SleepModeFrequencyReadings {
    _8_HZ = 0b00,
    _4_HZ = 0b01,
    _2_HZ = 0b10,
    _1_HZ = 0b11
}

impl SleepModeFrequencyReadings {
    pub fn val(self) -> u8 {
        self as u8
    }
}

pub struct InterruptSource {
    pub value: u8
}

impl InterruptSource {
    pub fn new(
        data_ready: bool,
        activity: bool,
        inactivity: bool,
        watermark: bool,
        overrun: bool
    ) -> InterruptSource {
        let value =
            ((data_ready as u8) << 7) +
            ((activity as u8) << 4) +
            ((inactivity as u8) << 3) +
            ((watermark as u8) << 1) +
            (overrun as u8);
        InterruptSource { value }
    }

    pub fn data_ready(&self) -> bool {
        self.value & 0b1000_0000 != 0
    }

    pub fn activity(&self) -> bool {
        self.value & 0b0001_0000 != 0
    }

    pub fn inactivity(&self) -> bool {
        self.value & 0b0000_1000 != 0
    }

    pub fn watermark(&self) -> bool {
        self.value & 0b0000_0010 != 0
    }

    pub fn overrun(&self) -> bool {
        self.value & 0b0000_0001 != 0
    }
}

#[derive(Copy, Clone, Debug)]
pub enum FifoMode {
    Bypass = 0b00,
    Fifo = 0b01,
    Stream = 0b10,
    Trigger = 0b11
}

impl FifoMode {
    pub fn val(self) -> u8 {
        self as u8
    }
}

pub struct FifoStatus {
    pub triggered: bool,
    pub samples: u8
}

impl FifoStatus {
    pub fn new(val: u8) -> FifoStatus {
        FifoStatus {
            triggered: val & 0b1000_0000 != 0,
            samples: (val & 0b0001_1111)
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub enum SpiMode {
    _4_WIRE = 0,
    _3_WIRE = 1
}

impl SpiMode {
    pub fn val(self) -> u8 {
        self as u8
    }
}

impl Default for SpiMode {
    fn default() -> Self {
        SpiMode::_4_WIRE
    }
}

#[derive(Copy, Clone, Debug)]
pub enum IrqMode {
    ACTIVE_HIGH = 0,
    ACTIVE_LOW = 1
}

impl IrqMode {
    pub fn val(self) -> u8 {
        self as u8
    }
}

impl Default for IrqMode {
    fn default() -> Self {
        IrqMode::ACTIVE_HIGH
    }
}


#[derive(Copy, Clone, Debug)]
pub enum AcDcCoupling {
    DcCoupling = 0,
    AcCoupling = 1
}

impl AcDcCoupling {
    pub fn val(self) -> u8 {
        self as u8
    }
}