bmx055 0.1.0

This is a platform agnostic Rust driver for the BMX055 small, versatile 9-axis sensor module: 3D accelerometer, 3D gyroscope and 3D magnetometer.
Documentation
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use bitflags::bitflags;

use crate::register_address::{RegRead, WhoAmIA, WhoAmIG, WhoAmIM};

/// All possible errors in this crate
#[derive(Debug)]
pub enum Error<CommE, PinE> {
    /// I²C / SPI communication error
    Comm(CommE),
    /// Chip-select pin error (SPI)
    Pin(PinE),
    /// Invalid input data provided
    InvalidInputData,
}

/// All possible errors in this crate
#[derive(Debug)]
pub struct ModeChangeError<CommE, PinE, DEV> {
    /// I²C / SPI communication error
    pub error: Error<CommE, PinE>,
    /// Original device without mode changed
    pub dev: DEV,
}

/// Device operation modes
pub mod mode {
    /// Marker type for magnetometer in one-shot (single) mode.
    #[derive(Debug)]
    pub enum MagOneShot {}
    /// Marker type for magnetometer in continuous mode.
    #[derive(Debug)]
    pub enum MagContinuous {}
}

/// An Accelerometer ID.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AccelerometerId {
    raw: u8,
}

impl AccelerometerId {
    pub(crate) fn from_bits_truncate(raw: u8) -> Self {
        Self { raw }
    }

    /// Raw accelerometer ID.
    pub const fn raw(&self) -> u8 {
        self.raw
    }

    /// Check if the ID corresponds to the expected value.
    pub const fn is_correct(&self) -> bool {
        self.raw == WhoAmIA::ID
    }
}

/// An acceleration measurement.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Acceleration {
    pub(crate) x: u16,
    pub(crate) y: u16,
    pub(crate) z: u16,
    pub(crate) mode: AccelMode,
    pub(crate) range: AccelRange,
}

impl RegRead<(u16, u16, u16)> for Acceleration {
    type Output = (u16, u16, u16);

    /// ACC_X_Y_Z
    const ADDR: u8 = 0x02;

    #[inline(always)]
    fn from_data(data: (u16, u16, u16)) -> Self::Output {
        data
    }
}

impl Acceleration {
    /// Raw acceleration in X-direction.
    #[inline]
    pub const fn x_raw(&self) -> u16 {
        self.x
    }

    /// Raw acceleration in Y-direction.
    #[inline]
    pub const fn y_raw(&self) -> u16 {
        self.y
    }

    /// Raw acceleration in Z-direction.
    #[inline]
    pub const fn z_raw(&self) -> u16 {
        self.z
    }

    /// Raw acceleration in X-, Y- and Z-directions.
    #[inline]
    pub const fn xyz_raw(&self) -> (u16, u16, u16) {
        (self.x, self.y, self.z)
    }

    /// Unscaled acceleration in X-direction.
    #[inline]
    pub const fn x_unscaled(&self) -> i16 {
        (self.x as i16) / 0b1_0000
    }

    /// Unscaled acceleration in Y-direction.
    #[inline]
    pub const fn y_unscaled(&self) -> i16 {
        (self.y as i16) / 0b1_0000
    }

    /// Unscaled acceleration in Z-direction.
    #[inline]
    pub const fn z_unscaled(&self) -> i16 {
        (self.z as i16) / 0b1_0000
    }

    /// Unscaled acceleration in X-, Y- and Z-directions.
    #[inline]
    pub const fn xyz_unscaled(&self) -> (i16, i16, i16) {
        let factor = 0b1_0000;

        (
            (self.x as i16) / factor,
            (self.y as i16) / factor,
            (self.z as i16) / factor,
        )
    }

    /// Acceleration in X-direction in m*g* (milli-*g*).
    #[inline]
    pub const fn x_mg(&self) -> i32 {
        self.x_unscaled() as i32
    }

    /// Acceleration in Y-direction in m*g* (milli-*g*).
    #[inline]
    pub const fn y_mg(&self) -> i32 {
        self.y_unscaled() as i32
    }

    /// Acceleration in Z-direction in m*g* (milli-*g*).
    #[inline]
    pub const fn z_mg(&self) -> i32 {
        self.z_unscaled() as i32
    }

    /// Acceleration in X-, Y- and Z-directions in m*g* (milli-*g*).
    #[inline]
    pub const fn xyz_mg(&self) -> (i32, i32, i32) {
        let (x_unscaled, y_unscaled, z_unscaled) = self.xyz_unscaled();
        let scaling_factor = 1;

        (
            (x_unscaled as i32) * scaling_factor,
            (y_unscaled as i32) * scaling_factor,
            (z_unscaled as i32) * scaling_factor,
        )
    }
}

/// Accelerometer bandwidth
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AccelBandwidth {
    /// 7.81 Hz
    Hz7_81,
    /// 15.63 Hz
    Hz15_63,
    /// 31.25 Hz
    Hz31_25,
    /// 62.5 Hz
    Hz62_5,
    /// 125 Hz
    Hz125,
    /// 250 Hz
    Hz250,
    /// 500 Hz
    Hz500,
    /// 1.000 Hz
    Hz1000,
}

/// Accelerometer mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AccelMode {
    /// Normal mode
    Normal,
}

/// Accelerometer g-range
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AccelRange {
    /// Plus or minus 2g
    G2 = 2,
    /// Plus or minus 4g
    G4 = 4,
    /// Plus or minus 8g
    G8 = 8,
    /// Plus or minus 16g
    G16 = 16,
}

bitflags! {
    #[derive(Debug, Default, Copy, Clone, PartialEq)]
    pub struct StatusFlags: u8 {
        const DATA_INT      = 0b10000000;
        const FIFO_WM_INT   = 0b01000000;
        const FIFO_FULL_INT = 0b00100000;
    }
}

/// Data status
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Status {
    flags: StatusFlags,
}

impl Status {
    pub(crate) const fn new(flags: StatusFlags) -> Self {
        Self { flags }
    }

    /// X-, Y- and Z-axis new data available.
    #[inline]
    pub const fn xyz_new_data(&self) -> bool {
        self.flags.contains(StatusFlags::DATA_INT)
    }
}

bitflags! {
    #[derive(Debug, Default, Copy, Clone, PartialEq)]
    pub struct GyroStatusFlags: u8 {
        const DATA_INT        = 0b10000000;
        const AUTO_OFFSET_INT = 0b01000000;
        const FAST_OFFSET_INT = 0b00100000;
        const FIFO_INT        = 0b00010000;
    }
}

/// Gyroscope data status
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct GyroStatus {
    flags: GyroStatusFlags,
}

impl GyroStatus {
    pub(crate) const fn new(flags: GyroStatusFlags) -> Self {
        Self { flags }
    }

    /// X-, Y- and Z-axis new data available.
    #[inline]
    pub const fn xyz_new_data(&self) -> bool {
        self.flags.contains(GyroStatusFlags::DATA_INT)
    }
}

/// A temperature measurement.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Temperature {
    pub(crate) raw: u8,
}

impl RegRead<u8> for Temperature {
    type Output = Self;

    /// OUT_TEMP_L_A
    const ADDR: u8 = 0x08;

    #[inline]
    fn from_data(data: u8) -> Self::Output {
        Temperature { raw: data }
    }
}

impl Temperature {
    const CENTER: f32 = 23.0;

    /// Raw temperature.
    #[inline]
    pub const fn raw(&self) -> u8 {
        self.raw
    }

    /// Unscaled temperature.
    #[inline]
    pub const fn unscaled(&self) -> i8 {
        self.raw as i8
    }

    /// Temperature in °C.
    #[inline]
    pub fn degrees_celsius(&self) -> f32 {
        f32::from(self.unscaled()) / 2.0 + Self::CENTER
    }
}

/// Gyroscope filter bandwidth
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroFilterBandwidth {
    /// 32 Hz, ODR 100 Hz
    Hz32,
    /// 64 Hz, ODR 200 Hz
    Hz64,
    /// 12 Hz, ODR 100 Hz
    Hz12,
    /// 23 Hz, ODR 200 Hz
    Hz23,
    /// 47 Hz, ODR 400 Hz
    Hz47,
    /// 116 Hz, ODR 1000 Hz
    Hz116,
    /// 230 Hz, ODR 2000 Hz
    Hz230,
    /// 523 Hz, ODR 2000 Hz
    Hz523,
}

/// Gyroscope mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroMode {
    /// Normal mode
    Normal,
}

/// Gyroscope range
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroRange {
    /// Plus or minus 2000 °/s
    R2000 = 2000,
    /// Plus or minus 1000 °/s
    R1000 = 1000,
    /// Plus or minus 500 °/s
    R500 = 500,
    /// Plus or minus 250 °/s
    R250 = 250,
    /// Plus or minus 125 °/s
    R125 = 125,
}

/// An angular rate measurement.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AngularRate {
    pub(crate) x: u16,
    pub(crate) y: u16,
    pub(crate) z: u16,
    pub(crate) mode: GyroMode,
    pub(crate) range: GyroRange,
}

impl RegRead<(u16, u16, u16)> for AngularRate {
    type Output = (u16, u16, u16);

    /// GYR_X_Y_Z
    const ADDR: u8 = 0x02;

    #[inline(always)]
    fn from_data(data: (u16, u16, u16)) -> Self::Output {
        data
    }
}

impl AngularRate {
    /// Raw angular rate in X-direction.
    #[inline]
    pub const fn x_raw(&self) -> u16 {
        self.x
    }

    /// Raw angular rate in Y-direction.
    #[inline]
    pub const fn y_raw(&self) -> u16 {
        self.y
    }

    /// Raw angular rate in Z-direction.
    #[inline]
    pub const fn z_raw(&self) -> u16 {
        self.z
    }

    /// Raw angular rate in X-, Y- and Z-directions.
    #[inline]
    pub const fn xyz_raw(&self) -> (u16, u16, u16) {
        (self.x, self.y, self.z)
    }

    /// Unscaled angular rate in X-direction.
    #[inline]
    pub const fn x_unscaled(&self) -> i16 {
        self.x as i16
    }

    /// Unscaled angular rate in Y-direction.
    #[inline]
    pub const fn y_unscaled(&self) -> i16 {
        self.y as i16
    }

    /// Unscaled angular rate in Z-direction.
    #[inline]
    pub const fn z_unscaled(&self) -> i16 {
        self.z as i16
    }

    /// Unscaled angular rate in X-, Y- and Z-directions.
    #[inline]
    pub const fn xyz_unscaled(&self) -> (i16, i16, i16) {
        (self.x as i16, self.y as i16, self.z as i16)
    }

    /// Angular rate in X-direction in °/s (degree per second)
    #[inline]
    pub fn x_dps(&self) -> f64 {
        let factor: f64 = (self.range as i32 as f64) / 32767_f64;
        (self.x_unscaled() as f64) * factor
    }

    /// Angular rate in Y-direction in °/s (degree per second)
    #[inline]
    pub fn y_dps(&self) -> f64 {
        let factor: f64 = (self.range as i32 as f64) / 32767_f64;
        (self.y_unscaled() as f64) * factor
    }

    /// Angular rate in Z-direction in °/s (degree per second)
    #[inline]
    pub fn z_dps(&self) -> f64 {
        let factor: f64 = (self.range as i32 as f64) / 32767_f64;
        (self.z_unscaled() as f64) * factor
    }

    /// Angular rate in X-, Y- and Z-directions °/s (degree per second)
    #[inline]
    pub fn xyz_dps(&self) -> (f64, f64, f64) {
        let (x_unscaled, y_unscaled, z_unscaled) = self.xyz_unscaled();
        let scaling_factor: f64 = (self.range as i32 as f64) / 32767_f64;

        (
            (x_unscaled as f64) * scaling_factor,
            (y_unscaled as f64) * scaling_factor,
            (z_unscaled as f64) * scaling_factor,
        )
    }
}

/// A Gyroscope ID.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GyroscopeId {
    raw: u8,
}

impl GyroscopeId {
    pub(crate) fn from_bits_truncate(raw: u8) -> Self {
        Self { raw }
    }

    /// Raw accelerometer ID.
    pub const fn raw(&self) -> u8 {
        self.raw
    }

    /// Check if the ID corresponds to the expected value.
    pub const fn is_correct(&self) -> bool {
        self.raw == WhoAmIG::ID
    }
}

/// A Magnetometer ID.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MagnetometerId {
    raw: u8,
}

impl MagnetometerId {
    pub(crate) fn from_bits_truncate(raw: u8) -> Self {
        Self { raw }
    }

    /// Raw magnetometer ID.
    pub const fn raw(&self) -> u8 {
        self.raw
    }

    /// Check if the ID corresponds to the expected value.
    pub const fn is_correct(&self) -> bool {
        self.raw == WhoAmIM::ID
    }
}

/// Magnetometer op mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MagOpMode {
    Normal,
    Forced,
    Sleep,
}

/// A magnetic field data measurement.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MagneticFieldData {
    pub(crate) x: u16,
    pub(crate) y: u16,
    pub(crate) z: u16,
    pub(crate) rhall: u16,
    pub trim_data: TrimData,
}

impl RegRead<(u16, u16, u16, u16)> for MagneticFieldData {
    type Output = (u16, u16, u16, u16);

    /// MAG_X_Y_Z_RHALL
    const ADDR: u8 = 0x42;

    #[inline(always)]
    fn from_data(data: (u16, u16, u16, u16)) -> Self::Output {
        data
    }
}

impl MagneticFieldData {
    /// Raw value X
    #[inline]
    pub const fn x_raw(&self) -> u16 {
        self.x
    }

    /// Raw value Y
    #[inline]
    pub const fn y_raw(&self) -> u16 {
        self.y
    }

    /// Raw value Z
    #[inline]
    pub const fn z_raw(&self) -> u16 {
        self.z
    }

    /// Raw value RHALL
    #[inline]
    pub const fn rhall_raw(&self) -> u16 {
        self.rhall
    }

    /// Raw values X, Y, Z and RHALL
    #[inline]
    pub const fn xyzr_raw(&self) -> (u16, u16, u16, u16) {
        (self.x, self.y, self.z, self.rhall)
    }

    /// Unscaled X
    #[inline]
    pub const fn x_unscaled(&self) -> i16 {
        (self.x as i16) >> 3
    }

    /// Unscaled Y
    #[inline]
    pub const fn y_unscaled(&self) -> i16 {
        (self.y as i16) >> 3
    }

    /// Unscaled Z
    #[inline]
    pub const fn z_unscaled(&self) -> i16 {
        (self.z as i16) >> 1
    }

    /// Unscaled RHALL
    #[inline]
    pub const fn rhall_unscaled(&self) -> u16 {
        self.rhall >> 2
    }

    /// Data ready
    #[inline]
    pub const fn data_ready(&self) -> bool {
        self.rhall & 0b0000_0000_0000_0001 == 1
    }

    /// Unscaled X, Y, Z and RHALL
    #[inline]
    pub const fn xyzr_unscaled(&self) -> (i16, i16, i16, u16) {
        (
            self.x_unscaled(),
            self.y_unscaled(),
            self.z_unscaled(),
            self.rhall_unscaled(),
        )
    }
}

/// Trim data X1Y1
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrimX1Y1 {
    dig_x1: i8,
    dig_y1: i8,
}

impl RegRead<[u8; 2]> for TrimX1Y1 {
    type Output = TrimX1Y1;

    /// MAG_DIG_X1
    const ADDR: u8 = 0x5d;

    #[inline(always)]
    fn from_data(data: [u8; 2]) -> Self::Output {
        TrimX1Y1 {
            dig_x1: i8::from_le_bytes([data[0]]),
            dig_y1: i8::from_le_bytes([data[1]]),
        }
    }
}

/// Trim data XYZ
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrimXYZ {
    dig_z4: i16,
    dig_x2: i8,
    dig_y2: i8,
}

impl RegRead<[u8; 4]> for TrimXYZ {
    type Output = TrimXYZ;

    /// MAG_DIG_Z4_LSB
    const ADDR: u8 = 0x62;

    #[inline(always)]
    fn from_data(data: [u8; 4]) -> Self::Output {
        TrimXYZ {
            dig_z4: i16::from_le_bytes([data[0], data[1]]),
            dig_x2: i8::from_le_bytes([data[2]]),
            dig_y2: i8::from_le_bytes([data[3]]),
        }
    }
}

/// Trim data XY1XY2
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrimXY1XY2 {
    dig_z2: i16,
    dig_z1: i16,
    dig_xyz1: u16,
    dig_z3: i16,
    dig_xy1: u8,
    dig_xy2: i8,
}

impl RegRead<[u8; 10]> for TrimXY1XY2 {
    type Output = TrimXY1XY2;

    /// MAG_DIG_Z2_LSB
    const ADDR: u8 = 0x68;

    #[inline(always)]
    fn from_data(data: [u8; 10]) -> Self::Output {
        TrimXY1XY2 {
            dig_z2: i16::from_le_bytes([data[0], data[1]]),
            dig_z1: i16::from_le_bytes([data[2], data[3]]),
            dig_xyz1: u16::from_le_bytes([data[4], data[5]]) & 0x7fff,
            dig_z3: i16::from_le_bytes([data[6], data[7]]),
            dig_xy1: data[8],
            dig_xy2: i8::from_le_bytes([data[9]]),
        }
    }
}

/// Trim data
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrimData {
    pub(crate) dig_x1: i8,
    pub(crate) dig_y1: i8,
    pub(crate) dig_z4: i16,
    pub(crate) dig_x2: i8,
    pub(crate) dig_y2: i8,
    pub(crate) dig_z2: i16,
    pub(crate) dig_z1: i16,
    pub(crate) dig_xyz1: u16,
    pub(crate) dig_z3: i16,
    pub(crate) dig_xy1: u8,
    pub(crate) dig_xy2: i8,
}

impl From<(TrimX1Y1, TrimXYZ, TrimXY1XY2)> for TrimData {
    #[inline(always)]
    fn from(
        (
            TrimX1Y1 { dig_x1, dig_y1 },
            TrimXYZ {
                dig_z4,
                dig_x2,
                dig_y2,
            },
            TrimXY1XY2 {
                dig_z2,
                dig_z1,
                dig_xyz1,
                dig_z3,
                dig_xy1,
                dig_xy2,
            },
        ): (TrimX1Y1, TrimXYZ, TrimXY1XY2),
    ) -> Self {
        TrimData {
            dig_x1,
            dig_y1,
            dig_z4,
            dig_x2,
            dig_y2,
            dig_z2,
            dig_z1,
            dig_xyz1,
            dig_z3,
            dig_xy1,
            dig_xy2,
        }
    }
}