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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
pub(crate) trait RegisterBits {
    fn mask() -> u8;
    fn value(&self) -> u8;
}

/// Accelerometer Output Data Rate
#[derive(Copy, Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum AccelDataRate {
    /// 10 Hz
    _10_Hz = 0x10,
    /// 50 Hz
    _50_Hz = 0x20,
    /// 100 Hz
    _100_Hz = 0x30,
    /// 200 Hz
    _200_Hz = 0x40,
    /// 400 Hz
    _400_Hz = 0x50,
    /// 800 Hz
    _800_Hz = 0x60,
}

impl Default for AccelDataRate {
    fn default() -> Self {
        AccelDataRate::_100_Hz
    }
}

impl RegisterBits for AccelDataRate {
    fn mask() -> u8 {
        AccelDataRate::_800_Hz.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

/// Acceleration scale/sensitivity
#[derive(Clone, Copy, Debug)]
#[allow(non_camel_case_types)]
pub enum AccelScale {
    /// +/- 2g
    _2G,
    /// +/- 4g
    _4G,
    /// +/- 8g
    _8G,
}

impl Default for AccelScale {
    fn default() -> Self {
        AccelScale::_2G
    }
}

impl RegisterBits for AccelScale {
    fn mask() -> u8 {
        AccelScale::_8G.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

impl AccelScale {
    pub(crate) fn resolution(&self) -> f32 {
        match self {
            AccelScale::_2G => 2.0 / 32768.0,
            AccelScale::_4G => 4.0 / 32768.0,
            AccelScale::_8G => 8.0 / 32768.0,
        }
    }
}

/// Magnetometer Output Data Rate
#[derive(Clone, Copy, Debug)]
#[allow(non_camel_case_types)]
pub enum MagDataRate {
    /// 0.625Hz
    _0_625_Hz = 0x00,
    /// 1.25Hz
    _1_25_Hz = 0x04,
    /// 2.5Hz
    _2_5_Hz = 0x08,
    /// 5Hz
    _5_Hz = 0x0C,
    /// 10Hz
    _10_Hz = 0x10,
    /// 20Hz
    _20_Hz = 0x14,
    /// 40Hz
    _40_Hz = 0x18,
    /// 80Hz
    _80_Hz = 0x1C,
}

impl Default for MagDataRate {
    fn default() -> Self {
        MagDataRate::_40_Hz
    }
}

impl RegisterBits for MagDataRate {
    fn mask() -> u8 {
        MagDataRate::_80_Hz.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

/// Magnetometer full scale.
/// LSM303C only supports 16 ga.
#[derive(Debug, Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum MagScale {
    // /// +/- 4 gauss
    // _4_Ga = 0x00,
    // /// +/- 8 gauss
    // _8_Ga = 0x20,
    // /// +/- 12 gauss
    // _12_Ga = 0x40,
    /// +/- 16 gauss
    _16_Ga = 0x60,
}
// mgauss/LSB

impl Default for MagScale {
    fn default() -> Self {
        MagScale::_16_Ga
    }
}

impl RegisterBits for MagScale {
    fn mask() -> u8 {
        MagScale::_16_Ga.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

impl MagScale {
    pub(crate) fn resolution(&self) -> f32 {
        match self {
            // MagScale::_4_Ga => 4.0 / 32768.0,
            // MagScale::_8_Ga => 8.0 / 32768.0,
            // MagScale::_12_Ga => 12.0 / 32768.0,
            MagScale::_16_Ga => 0.58,
        }
    }
}

/// Magnetrometer mode
#[derive(Debug, Clone, Copy)]
pub enum MagMode {
    /// Continous mode
    Continous = 0x00,
    /// Single
    Single = 0x01,
    /// Pwr Down 1
    PowerDown1 = 0x02,
    /// Pwr Down 2
    PowerDown2 = 0x03,
}

impl RegisterBits for MagMode {
    fn mask() -> u8 {
        MagMode::PowerDown2.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

impl Default for MagMode {
    fn default() -> Self {
        MagMode::Continous
    }
}

/// Magnetrometer X and Y axes operative mode selection.
/// Default: High performance.
#[derive(Debug, Clone, Copy)]
pub enum MagXYOperativeMode {
    /// Low power
    LowPower = 0x00,
    /// Medium performance
    MediumPerformance = 0x20,
    /// High performance
    HighPerformance = 0x40,
    /// Ultra high performance
    UltraHighPerformance = 0x60,
}

impl RegisterBits for MagXYOperativeMode {
    fn mask() -> u8 {
        MagXYOperativeMode::UltraHighPerformance.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

impl Default for MagXYOperativeMode {
    fn default() -> Self {
        MagXYOperativeMode::HighPerformance
    }
}

/// Magnetrometer Z axis operative mode selection
/// Default value: HighPerformance.
#[derive(Debug, Clone, Copy)]
pub enum MagZOperativeMode {
    /// Low power
    LowPower = 0x00,
    /// Medium performance
    MediumPerformance = 0x04,
    /// High performance
    HighPerformance = 0x08,
    /// Ultra high performance
    UltraHighPerformance = 0x0C,
}

impl RegisterBits for MagZOperativeMode {
    fn mask() -> u8 {
        MagZOperativeMode::UltraHighPerformance.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

impl Default for MagZOperativeMode {
    fn default() -> Self {
        MagZOperativeMode::HighPerformance
    }
}

#[derive(Debug, Clone, Copy)]
/// Controls (disable or enable) temperature sensor.
pub enum TempControl {
    /// Enable temperature sensor
    Enable = 0x80,
    /// Disable temperature sensor
    Disable = 0x00,
}

impl Default for TempControl {
    fn default() -> Self {
        TempControl::Enable
    }
}

impl RegisterBits for TempControl {
    fn mask() -> u8 {
        TempControl::Enable.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

#[derive(Debug, Clone, Copy)]
/// Block data update for accel data.
pub enum MagBlockDataUpdate {
    /// output registers not updated until MSB and LSB have been read
    Enable = 0x40,
    /// continuous update
    Disable = 0x00,
}

impl Default for MagBlockDataUpdate {
    fn default() -> Self {
        MagBlockDataUpdate::Enable
    }
}

impl RegisterBits for MagBlockDataUpdate {
    fn mask() -> u8 {
        MagBlockDataUpdate::Enable.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

#[derive(Debug, Clone, Copy)]
/// Block data update for accel data.
pub enum AccelBlockDataUpdate {
    /// output registers not updated until MSB and LSB have been read
    Enable = 0x08,
    /// continuous update
    Disable = 0x00,
}

impl Default for AccelBlockDataUpdate {
    fn default() -> Self {
        AccelBlockDataUpdate::Enable
    }
}

impl RegisterBits for AccelBlockDataUpdate {
    fn mask() -> u8 {
        AccelBlockDataUpdate::Enable.value()
    }

    fn value(&self) -> u8 {
        *self as u8
    }
}

#[derive(Debug, Clone, Copy)]
/// Accel axes control
pub struct AccelAxesControl {
    x_axis: bool,
    y_axis: bool,
    z_axis: bool,
}

impl AccelAxesControl {
    fn all_enabled() -> Self {
        AccelAxesControl { x_axis: true,
                           y_axis: true,
                           z_axis: true, }
    }

    fn all_disabled() -> Self {
        AccelAxesControl { x_axis: true,
                           y_axis: true,
                           z_axis: true, }
    }

    fn custom(x_axis: bool, y_axis: bool, z_axis: bool) -> Self {
        AccelAxesControl { x_axis,
                           y_axis,
                           z_axis, }
    }
}

impl Default for AccelAxesControl {
    fn default() -> Self {
        AccelAxesControl::all_enabled()
    }
}

impl RegisterBits for AccelAxesControl {
    fn mask() -> u8 {
        AccelAxesControl::all_enabled().value()
    }

    fn value(&self) -> u8 {
        let mut res: u8 = 0;
        if (self.x_axis) {
            res |= 0x01;
        }
        if (self.y_axis) {
            res |= 0x02;
        }
        if (self.z_axis) {
            res |= 0x04;
        }

        res
    }
}

/// Configuration of Lsm303c
#[derive(Copy, Clone, Debug)]
pub struct LsmConfig {
    pub(crate) accel_scale: Option<AccelScale>,
    pub(crate) mag_scale: Option<MagScale>,
    pub(crate) accel_data_rate: Option<AccelDataRate>,
    pub(crate) mag_data_rate: Option<MagDataRate>,
    pub(crate) accel_block_data_update: Option<AccelBlockDataUpdate>,
    pub(crate) mag_block_data_update: Option<MagBlockDataUpdate>,
    pub(crate) mag_mode: Option<MagMode>,
    pub(crate) mag_xy_operative_mode: Option<MagXYOperativeMode>,
    pub(crate) mag_z_operative_mode: Option<MagZOperativeMode>,
    pub(crate) accel_axes_control: Option<AccelAxesControl>,
    pub(crate) temp_control: Option<TempControl>,
}

impl LsmConfig {
    /// Creates Lsm303c configuration with default
    /// [`AccelScale`], [`MagScale`], [`AccelDataRate`],
    /// [`MagDataRate`], [`MagMode`], [`AccelBlockDataUpdate`],
    /// [`MagBlockDataUpdate`], [`MagXYOperativeMode`], [`MagZOperativeMode`],
    /// [`AccelAxesControl`], and [`TempControl`].
    ///
    /// [`AccelScale`]: ./enum.AccelScale.html
    /// [`MagScale`]: ./enum.MagScale.html
    /// [`AccelDataRate`]: ./enum.AccelDataRate.html
    /// [`MagDataRate`]: ./enum.MagDataRate.html
    /// [`MagMode`]: ./enum.MagMode.html
    /// [`MagBlockDataUpdate`]: ./enum.MagBlockDataUpdate.html
    /// [`AccelBlockDataUpdate`]: ./enum.AccelBlockDataUpdate.html
    /// [`MagXYOperativeMode`]: ./enum.MagXYOperativeMode.html
    /// [`MagZOperativeMode`]: ./enum.MagZOperativeMode.html
    /// [`AccelAxesControl`]: ./enum.AccelAxesControl.html
    /// [`TempControl`]: ./enum.TempControl.html
    pub fn new() -> Self {
        LsmConfig { accel_scale: None,
                    mag_scale: None,
                    accel_data_rate: None,
                    mag_data_rate: None,
                    accel_block_data_update: None,
                    mag_block_data_update: None,
                    mag_mode: None,
                    mag_xy_operative_mode: None,
                    mag_z_operative_mode: None,
                    accel_axes_control: None,
                    temp_control: None, }
    }

    /// Sets accelerometer full reading scale ([`AccelScale`])
    ///
    /// [`AccelScale`]: ./enum.AccelScale.html
    pub fn accel_scale(&mut self, accel_scale: AccelScale) -> &mut Self {
        self.accel_scale = Some(accel_scale);
        self
    }

    /// Sets magnetrometer full reading scale ([`MagScale`])
    ///
    /// [`MagScale`]: ./enum.MagScale.html
    pub fn mag_scale(&mut self, mag_scale: MagScale) -> &mut Self {
        self.mag_scale = Some(mag_scale);
        self
    }

    /// Sets accelerometer output data rate ([`AccelDataRate`])
    ///
    /// [`AccelDataRate`]: ./enum.AccelDataRate.html
    pub fn accel_data_rate(&mut self,
                           accel_data_rate: AccelDataRate)
                           -> &mut Self {
        self.accel_data_rate = Some(accel_data_rate);
        self
    }

    /// Sets magnetrometer output data rate ([`MagDataRate`])
    ///
    /// [`MagDataRate`]: ./enum.MagDataRate.html
    pub fn mag_data_rate(&mut self, mag_data_rate: MagDataRate) -> &mut Self {
        self.mag_data_rate = Some(mag_data_rate);
        self
    }

    /// Sets accelerometer block data update ([`AccelBlockDataUpdate`])
    ///
    /// [`AccelBlockDataUpdate`]: ./enum.AccelBlockDataUpdate.html
    pub fn accel_block_data_update(&mut self,
                                   accel_block_data_update: AccelBlockDataUpdate)
                                   -> &mut Self {
        self.accel_block_data_update = Some(accel_block_data_update);
        self
    }

    /// Sets magnetrometer block data update ([`MagBlockDataUpdate`])
    ///
    /// [`MagBlockDataUpdate`]: ./enum.MagBlockDataUpdate.html
    pub fn mag_block_data_update(&mut self,
                                 mag_block_data_update: MagBlockDataUpdate)
                                 -> &mut Self {
        self.mag_block_data_update = Some(mag_block_data_update);
        self
    }

    /// Sets magnetrometer mode ([`MagMode`])
    ///
    /// [`MagMode`]: ./enum.MagMode.html
    pub fn mag_mode(&mut self, mag_mode: MagMode) -> &mut Self {
        self.mag_mode = Some(mag_mode);
        self
    }

    /// Sets magnetrometer x and y axes operative mode ([`MagXYOperativeMode`])
    ///
    /// [`MagXYOperativeMode`]: ./enum.MagXYOperativeMode.html
    pub fn mag_xy_operative_mode(&mut self,
                                 operative_mode: MagXYOperativeMode)
                                 -> &mut Self {
        self.mag_xy_operative_mode = Some(operative_mode);
        self
    }

    /// Sets magnetrometer z axis operative mode ([`MagZOperativeMode`])
    ///
    /// [`MagZOperativeMode`]: ./enum.MagZOperativeMode.html
    pub fn mag_z_operative_mode(&mut self,
                                operative_mode: MagZOperativeMode)
                                -> &mut Self {
        self.mag_z_operative_mode = Some(operative_mode);
        self
    }

    /// Sets accel axes control ([`AccelAxesControl`])
    ///
    /// [`AccelAxesControl`]: ./enum.AccelAxesControl.html
    pub fn accel_axes_control(&mut self,
                              axes_control: AccelAxesControl)
                              -> &mut Self {
        self.accel_axes_control = Some(axes_control);
        self
    }

    /// Sets temperature control ([`TempControl`])
    ///
    /// [`TempControl`]: ./enum.TempControl.html
    pub fn temp_control(&mut self, temp_control: TempControl) -> &mut Self {
        self.temp_control = Some(temp_control);
        self
    }
}