iis2mdc-rs 2.0.0

Platform-agnostic driver for the IIS2MDC ultra-low-power, high-accuracy 3-axis digital magnetic sensor with I2C and SPI interfaces.
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
use super::{
    BusOperation, DelayNs, I2c, RegisterOperation, SensorOperation, SevenBitAddress, SpiDevice,
    bisync, i2c, prelude::*, register::SensorState, spi,
};

use core::fmt::Debug;
use core::marker::PhantomData;

/// The Iis2mdc generic driver struct.
#[bisync]
pub struct Iis2mdc<B, T, S>
where
    B: BusOperation,
    T: DelayNs,
    S: SensorState,
{
    pub bus: B,
    pub tim: T,
    _state: PhantomData<S>,
}

/// Driver errors.
#[derive(Debug)]
#[bisync]
pub enum Error<B> {
    /// An error occurred at the bus level. Any methods that access the I2C/SPI bus to interact
    /// with the sensor may return this error if the bus operation fails.
    ///
    /// The generic type B represents the specific error generated by the HAL of the microcontroller in use.
    Bus(B),
}

#[bisync]
impl<P, T> Iis2mdc<i2c::I2cBus<P>, T, OnState>
where
    P: I2c,
    T: DelayNs,
{
    /// Constructor method for using the I2C bus.
    ///
    /// # Arguments
    ///
    /// * `i2c`: The I2C peripheral.
    /// * `address`: The I2C address of the COMPONENT sensor.
    /// * `tim`: The timer of the COMPONENT sensor.
    ///
    /// # Returns
    ///
    /// * `Self`: Returns an instance of `Iis2mdc`.
    pub fn new_i2c(i2c: P, address: I2CAddress, tim: T) -> Self {
        // Initialize the I2C bus with the COMPONENT address
        let bus = i2c::I2cBus::new(i2c, address as SevenBitAddress);
        Self {
            bus,
            tim,
            _state: PhantomData,
        }
    }
}
impl<B, T, S> Iis2mdc<B, T, S>
where
    B: BusOperation,
    T: DelayNs,
    S: SensorState,
{
    /// Create a safe fake buffer to use the sensor as master of the
    /// sensor hub.
    ///
    /// # Arguments
    ///
    /// * `bus`: The bus that implements BusOperation.
    /// * `tim`: The timer of the COMPONENT sensor.
    /// * `slave_address`: The I2C address of the slave sensor
    ///
    /// # Returns
    ///
    /// * `Self`: Returns an instance of `Iis2mdc`.
    pub fn from_bus(bus: B, tim: T) -> Self {
        Self {
            bus,
            tim,
            _state: PhantomData,
        }
    }
}

impl<P, T> Iis2mdc<spi::SpiBus<P>, T, OnState>
where
    P: SpiDevice,
    T: DelayNs,
{
    /// Constructor method for using the SPI bus.
    ///
    /// # Arguments
    ///
    /// * `spi`: The SPI peripheral.
    /// * `tim`: The timer of the COMPONENT sensor.
    ///
    /// # Returns
    ///
    /// * `Result`
    ///     * `Self`: Returns an instance of `Iis2mdc`.
    ///     * `Err`: Returns an error if the initialization fails.
    pub fn new_spi(spi: P, tim: T) -> Self {
        // Initialize the SPI bus
        let bus = spi::SpiBus::new(spi);
        Self {
            bus,
            tim,
            _state: PhantomData,
        }
    }
}

#[bisync]
impl<B: BusOperation, T: DelayNs, S: SensorState> SensorOperation for Iis2mdc<B, T, S> {
    type Error = Error<B::Error>;

    async fn read_from_register(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Error<B::Error>> {
        self.bus
            .read_from_register(reg, buf)
            .await
            .map_err(Error::Bus)
    }

    async fn write_to_register(&mut self, reg: u8, buf: &[u8]) -> Result<(), Error<B::Error>> {
        self.bus
            .write_to_register(reg, buf)
            .await
            .map_err(Error::Bus)
    }
}

#[bisync]
impl<B: BusOperation, T: DelayNs> Iis2mdc<B, T, OnState> {
    /// Sets the magnetic sensor's hard-iron offset to compensate for environmental effects.
    ///
    /// This function writes a set of three 16-bit values to the sensor's registers, which represent
    /// the hard-iron offset. These offsets are used to adjust the magnetic output data, effectively
    /// removing environmental biases. The data format for these values is two's complement, with
    /// a resolution of 1LSb = 1.5mG.
    ///
    /// # Arguments
    ///
    /// * `val`: A reference to an array containing three 16-bit integers. Each integer represents
    ///   the offset for one axis (X, Y, Z) of the magnetic sensor. The values are expected to be
    ///   in two's complement format.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the offsets are successfully written
    ///   to the sensor's registers. If the operation fails, it returns an `Err` containing the
    ///   specific error type generated by the microcontroller's HAL.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation
    pub async fn mag_user_offset_set(&mut self, val: &[i16; 3]) -> Result<(), Error<B::Error>> {
        OffsetXYZ {
            x: val[0],
            y: val[1],
            z: val[2],
        }
        .write(self)
        .await
    }

    /// Retrieves the magnetic sensor's hard-iron offset values.
    ///
    /// This function reads a set of three 16-bit values from the sensor's registers, which represent
    /// the hard-iron offset. These offsets are used to adjust the magnetic output data, effectively
    /// removing environmental biases. The data format for these values is two's complement, with
    /// a resolution of 1LSb = 1.5mG.
    ///
    /// # Returns
    ///
    /// * `Result<[i16; 3], Error<B::Error>>`: Returns an array of three 16-bit integers if the
    ///   offsets are successfully read from the sensor's registers. Each integer represents the
    ///   offset for one axis (X, Y, Z) of the magnetic sensor.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation
    pub async fn mag_user_offset_get(&mut self) -> Result<[i16; 3], Error<B::Error>> {
        let val = OffsetXYZ::read(self).await?;
        Ok([val.x, val.y, val.z])
    }

    /// Sets the operating mode of the sensor.
    ///
    /// This function modifies the operating mode by updating the `md` field in the `CFG_REG_A` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired operating mode, represented by the `Md` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the mode is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn operating_mode_set(&mut self, val: Md) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegA::read(self).await?;
        reg.set_md(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current operating mode of the sensor.
    ///
    /// This function reads the `md` field from the `CFG_REG_A` register to determine the current operating mode.
    ///
    /// # Returns
    ///
    /// * `Result<Md, Error<B::Error>>`: Returns the current operating mode as an `Md` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn operating_mode_get(&mut self) -> Result<Md, Error<B::Error>> {
        let val = CfgRegA::read(self).await?.md();
        Ok(Md::try_from(val).unwrap_or_default())
    }

    /// Sets the output data rate of the sensor.
    ///
    /// This function modifies the output data rate by updating the `odr` field in the `CFG_REG_A` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired data rate, represented by the `Odr` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the data rate is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn data_rate_set(&mut self, val: Odr) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegA::read(self).await?;
        reg.set_odr(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current output data rate of the sensor.
    ///
    /// This function reads the `odr` field from the `CFG_REG_A` register to determine the current data rate.
    ///
    /// # Returns
    ///
    /// * `Result<Odr, Error<B::Error>>`: Returns the current data rate as an `Odr` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn data_rate_get(&mut self) -> Result<Odr, Error<B::Error>> {
        let val = CfgRegA::read(self).await?.odr();
        Ok(Odr::try_from(val).unwrap_or_default())
    }

    /// Sets the power mode of the sensor to high-resolution or low-power.
    ///
    /// This function modifies the power mode by updating the `lp` field in the `CFG_REG_A` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired power mode, represented by the `Lp` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the power mode is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn power_mode_set(&mut self, val: Lp) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegA::read(self).await?;
        reg.set_lp(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current power mode of the sensor.
    ///
    /// This function reads the `lp` field from the `CFG_REG_A` register to determine the current power mode.
    ///
    /// # Returns
    ///
    /// * `Result<Lp, Error<B::Error>>`: Returns the current power mode as an `Lp` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn power_mode_get(&mut self) -> Result<Lp, Error<B::Error>> {
        let val = CfgRegA::read(self).await?.lp();
        Ok(Lp::try_from(val).unwrap_or_default())
    }

    /// Enables or disables the magnetometer temperature compensation.
    ///
    /// This function modifies the temperature compensation setting by updating the `comp_temp_en` field in the `CFG_REG_A` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired state of temperature compensation (enabled or disabled).
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the setting is successfully updated. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn offset_temp_comp_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegA::read(self).await?;
        reg.set_comp_temp_en(val);
        reg.write(self).await
    }

    /// Retrieves the current state of the magnetometer temperature compensation.
    ///
    /// This function reads the `comp_temp_en` field from the `CFG_REG_A` register to determine if temperature compensation is enabled or disabled.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current state of temperature compensation. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn offset_temp_comp_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegA::read(self).await?.comp_temp_en())
    }

    /// Sets the low-pass filter bandwidth of the sensor.
    ///
    /// This function modifies the low-pass filter bandwidth by updating the `lpf` field in the `CFG_REG_B` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired low-pass filter bandwidth, represented by the `Lpf` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the bandwidth is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn low_pass_bandwidth_set(&mut self, val: Lpf) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegB::read(self).await?;
        reg.set_lpf(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current low-pass filter bandwidth of the sensor.
    ///
    /// This function reads the `lpf` field from the `CFG_REG_B` register to determine the current bandwidth setting.
    ///
    /// # Returns
    ///
    /// * `Result<Lpf, Error<B::Error>>`: Returns the current low-pass filter bandwidth as an `Lpf` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn low_pass_bandwidth_get(&mut self) -> Result<Lpf, Error<B::Error>> {
        let val = CfgRegB::read(self).await?.lpf();
        Ok(Lpf::try_from(val).unwrap_or_default())
    }

    /// Sets the reset pulse mode.
    ///
    /// This function modifies the reset pulse mode by updating the `set_rst` field in the `CFG_REG_B` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired reset pulse mode, represented by the `SetRst` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the mode is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn set_rst_mode_set(&mut self, val: SetRst) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegB::read(self).await?;
        reg.set_set_rst(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current reset pulse mode.
    ///
    /// This function reads the `set_rst` field from the `CFG_REG_B` register to determine the current reset pulse mode.
    ///
    /// # Returns
    ///
    /// * `Result<SetRst, Error<B::Error>>`: Returns the current reset pulse mode as a `SetRst` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn set_rst_mode_get(&mut self) -> Result<SetRst, Error<B::Error>> {
        let val = CfgRegB::read(self).await?.set_rst();
        Ok(SetRst::try_from(val).unwrap_or_default())
    }

    /// Enables offset cancellation in single measurement mode.
    ///
    /// This function modifies the offset cancellation setting by updating the `off_canc_one_shot` field in the `CFG_REG_B` register.
    /// The `OFF_CANC` bit must be set to 1, which requires calling the function `set_rst_mode(SensOffCancEveryOdr)`.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired state of offset cancellation (enabled or disabled).
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the setting is successfully updated. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn off_canc_en_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegB::read(self).await?;
        reg.set_off_canc_one_shot(val);
        reg.write(self).await
    }

    /// Retrieves the current state of offset cancellation in single measurement mode.
    ///
    /// This function reads the `off_canc_one_shot` field from the `CFG_REG_B` register to determine if offset cancellation is enabled or disabled.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current state of offset cancellation. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn off_canc_en_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegB::read(self).await?.off_canc_one_shot())
    }

    /// Sets the block data update mode.
    ///
    /// This function modifies the block data update setting by updating the `bdu` field in the `CFG_REG_C` register.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired state of block data update (enabled or disabled).
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the setting is successfully updated. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn block_data_update_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegC::read(self).await?;
        reg.set_bdu(val);
        reg.write(self).await
    }

    /// Retrieves the current block data update mode.
    ///
    /// This function reads the `bdu` field from the `CFG_REG_C` register to determine if block data update is enabled or disabled.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current state of block data update. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn block_data_update_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegC::read(self).await?.bdu())
    }

    /// Checks if magnetic data is ready.
    ///
    /// This function reads the `zyxda` field from the `STATUS_REG` register to determine if a new set of magnetic data is available.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the state indicating if magnetic data is ready. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn mag_data_ready_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(StatusReg::read(self).await?.zyxda())
    }

    /// Checks if magnetic data has overrun.
    ///
    /// This function reads the `zyxor` field from the `STATUS_REG` register to determine if magnetic data has overrun.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the state indicating if magnetic data has overrun. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn mag_data_ovr_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(StatusReg::read(self).await?.zyxor())
    }

    // Retrieves the raw magnetic output values.
    ///
    /// This function reads the magnetic output values from the sensor's registers, returning them as a three-element array of 16-bit integers.
    ///
    /// # Returns
    ///
    /// * `Result<[i16; 3], Error<B::Error>>`: Returns an array containing the raw magnetic output values. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn magnetic_raw_get(&mut self) -> Result<[i16; 3], Error<B::Error>> {
        let val = OutXYZ::read(self).await?;
        Ok([val.x, val.y, val.z])
    }

    //// Retrieves the raw temperature output value.
    ///
    /// This function reads the temperature value from the sensor's registers, returning it as a 16-bit integer.
    ///
    /// # Returns
    ///
    /// * `Result<i16, Error<B::Error>>`: Returns the raw temperature value. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn temperature_raw_get(&mut self) -> Result<i16, Error<B::Error>> {
        Ok(TempOutReg::read(self).await?.temp_out())
    }

    /// Retrieves the device ID.
    ///
    /// This function reads the device ID from the sensor's registers, returning it as an 8-bit integer.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the device ID. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn device_id_get(&mut self) -> Result<u8, Error<B::Error>> {
        let mut buff: [u8; 1] = [0];
        self.read_from_register(Reg::WhoAmI as u8, &mut buff)
            .await?;

        Ok(buff[0])
    }

    /// Performs a software reset, restoring default values in user registers.
    ///
    /// This function modifies the `soft_rst` field in the `CFG_REG_A` register to initiate a software reset.
    ///
    /// # Arguments
    ///
    /// * `val`: The value to set for the software reset.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the reset is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn reset_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegA::read(self).await?;
        reg.set_soft_rst(val);
        reg.write(self).await
    }

    /// Retrieves the current software reset state.
    ///
    /// This function reads the `soft_rst` field from the `CFG_REG_A` register to determine the current reset state.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current software reset state. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn reset_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegA::read(self).await?.soft_rst())
    }

    /// Reboots memory content, reloading calibration parameters.
    ///
    /// This function modifies the `reboot` field in the `CFG_REG_A` register to initiate a memory reboot.
    ///
    /// # Arguments
    ///
    /// * `val`: The value to set for the memory reboot.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the reboot is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn boot_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegA::read(self).await?;
        reg.set_reboot(val);
        reg.write(self).await
    }

    /// Retrieves the current memory reboot state.
    ///
    /// This function reads the `reboot` field from the `CFG_REG_A` register to determine the current reboot state.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current memory reboot state. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn boot_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegA::read(self).await?.reboot())
    }

    /// Sets the self-test mode.
    ///
    /// This function modifies the `self_test` field in the `CFG_REG_C` register to initiate a self-test.
    ///
    /// # Arguments
    ///
    /// * `val`: The value to set for the self-test mode.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the self-test mode is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn self_test_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegC::read(self).await?;
        reg.set_self_test(val);
        reg.write(self).await
    }

    /// Retrieves the current self-test mode.
    ///
    /// This function reads the `self_test` field from the `CFG_REG_C` register to determine the current self-test mode.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current self-test mode. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn self_test_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegC::read(self).await?.self_test())
    }

    /// Sets the data format to Big or Little Endian.
    ///
    /// This function modifies the `ble` field in the `CFG_REG_C` register to select the data format.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired data format, represented by the `Ble` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the data format is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn data_format_set(&mut self, val: Ble) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegC::read(self).await?;
        reg.set_ble(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current data format (Big or Little Endian).
    ///
    /// This function reads the `ble` field from the `CFG_REG_C` register to determine the current data format.
    ///
    /// # Returns
    ///
    /// * `Result<Ble, Error<B::Error>>`: Returns the current data format as a `Ble` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn data_format_get(&mut self) -> Result<Ble, Error<B::Error>> {
        let val = CfgRegC::read(self).await?.ble();
        Ok(Ble::try_from(val).unwrap_or_default())
    }

    /// Retrieves the device status information.
    ///
    /// This function reads the `STATUS_REG` register to obtain the current status of the device.
    ///
    /// # Returns
    ///
    /// * `Result<StatusReg, Error<B::Error>>`: Returns the status register value. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn status_get(&mut self) -> Result<StatusReg, Error<B::Error>> {
        StatusReg::read(self).await
    }

    /// Configures the interrupt block recognition for data checks after/before hard-iron correction.
    ///
    /// This function modifies the `int_on_dataoff` field in the `CFG_REG_B` register to set the interrupt configuration.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired interrupt configuration, represented by the `IntOnDataOff` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the configuration is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn offset_int_conf_set(&mut self, val: IntOnDataOff) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegB::read(self).await?;
        reg.set_int_on_dataoff(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current interrupt configuration for data checks after/before hard-iron correction.
    ///
    /// This function reads the `int_on_dataoff` field from the `CFG_REG_B` register to determine the current configuration.
    ///
    /// # Returns
    ///
    /// * `Result<IntOnDataOff, Error<B::Error>>`: Returns the current interrupt configuration as an `IntOnDataOff` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn offset_int_conf_get(&mut self) -> Result<IntOnDataOff, Error<B::Error>> {
        let val = CfgRegB::read(self).await?.int_on_dataoff();
        Ok(IntOnDataOff::try_from(val).unwrap_or_default())
    }

    /// Sets the data-ready signal on the INT_DRDY pin.
    ///
    /// This function modifies the `drdy_on_pin` field in the `CFG_REG_C` register to enable or disable the data-ready signal.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired state of the data-ready signal (enabled or disabled).
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the signal is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn drdy_on_pin_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegC::read(self).await?;
        reg.set_drdy_on_pin(val);
        reg.write(self).await
    }

    /// Retrieves the current state of the data-ready signal on the INT_DRDY pin.
    ///
    /// This function reads the `drdy_on_pin` field from the `CFG_REG_C` register to determine if the data-ready signal is enabled or disabled.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current state of the data-ready signal. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn drdy_on_pin_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegC::read(self).await?.drdy_on_pin())
    }

    /// Sets the interrupt signal on the INT_DRDY pin.
    ///
    /// This function modifies the `int_on_pin` field in the `CFG_REG_C` register to enable or disable the interrupt signal.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired state of the interrupt signal (enabled or disabled).
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the signal is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn int_on_pin_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegC::read(self).await?;
        reg.set_int_on_pin(val);
        reg.write(self).await
    }

    /// Retrieves the current state of the interrupt signal on the INT_DRDY pin.
    ///
    /// This function reads the `int_on_pin` field from the `CFG_REG_C` register to determine if the interrupt signal is enabled or disabled.
    ///
    /// # Returns
    ///
    /// * `Result<u8, Error<B::Error>>`: Returns the current state of the interrupt signal. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn int_on_pin_get(&mut self) -> Result<u8, Error<B::Error>> {
        Ok(CfgRegC::read(self).await?.int_on_pin())
    }

    /// Sets the interrupt generator configuration.
    ///
    /// This function writes to the `INT_CTRL_REG` register to configure the interrupt generator settings.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired configuration for the interrupt generator, represented by the `IntCtrlReg` struct.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the configuration is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn int_gen_conf_set(&mut self, val: IntCtrlReg) -> Result<(), Error<B::Error>> {
        val.write(self).await
    }

    /// Retrieves the current interrupt generator configuration.
    ///
    /// This function reads the `INT_CTRL_REG` register to obtain the current configuration of the interrupt generator.
    ///
    /// # Returns
    ///
    /// * `Result<IntCtrlReg, Error<B::Error>>`: Returns the current configuration as an `IntCtrlReg` struct. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn int_gen_conf_get(&mut self) -> Result<IntCtrlReg, Error<B::Error>> {
        IntCtrlReg::read(self).await
    }

    /// Retrieves the interrupt generator source register value.
    ///
    /// This function reads the `INT_SOURCE_REG` register to obtain the source of the interrupt generator.
    ///
    /// # Returns
    ///
    /// * `Result<IntSourceReg, Error<B::Error>>`: Returns the source register value as an `IntSourceReg` struct. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn int_gen_source_get(&mut self) -> Result<IntSourceReg, Error<B::Error>> {
        IntSourceReg::read(self).await
    }

    /// Sets the user-defined threshold value for the XL interrupt event on the generator.
    ///
    /// This function writes the threshold value to the `INT_THS_L_REG` register. The data format is two's complement with 1LSb = 1.5mG.
    ///
    /// # Arguments
    ///
    /// * `val`: The threshold value to set, represented as a 16-bit integer.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the threshold is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation..
    pub async fn int_gen_threshold_set(&mut self, val: i16) -> Result<(), Error<B::Error>> {
        IntThsReg::from_bits(val.cast_unsigned()).write(self).await
    }

    /// Retrieves the user-defined threshold value for the XL interrupt event on the generator.
    ///
    /// This function reads the threshold value from the `INT_THS_L_REG` register. The data format is two's complement with 1LSb = 1.5mG.
    ///
    /// # Returns
    ///
    /// * `Result<i16, Error<B::Error>>`: Returns the threshold value as a 16-bit integer. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn int_gen_threshold_get(&mut self) -> Result<i16, Error<B::Error>> {
        Ok(IntThsReg::read(self).await?.int_ths())
    }

    /// Enables or disables the I2C interface.
    ///
    /// This function modifies the `i2c_dis` field in the `CFG_REG_C` register to enable or disable the I2C interface.
    ///
    /// # Arguments
    ///
    /// * `val`: The desired state of the I2C interface, represented by the `I2cDis` enum.
    ///
    /// # Returns
    ///
    /// * `Result<(), Error<B::Error>>`: Returns `Ok(())` if the interface is successfully set. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn i2c_interface_set(&mut self, val: I2cDis) -> Result<(), Error<B::Error>> {
        let mut reg = CfgRegC::read(self).await?;
        reg.set_i2c_dis(val as u8);
        reg.write(self).await
    }

    /// Retrieves the current state of the I2C interface.
    ///
    /// This function reads the `i2c_dis` field from the `CFG_REG_C` register to determine if the I2C interface is enabled or disabled.
    ///
    /// # Returns
    ///
    /// * `Result<I2cDis, Error<B::Error>>`: Returns the current state of the I2C interface as an `I2cDis` enum. Returns an `Err` if the operation fails.
    ///
    /// # Errors
    ///
    /// - `Error::Bus(B::Error)`: This error is returned if there is a failure in the bus operation.
    pub async fn i2c_interface_get(&mut self) -> Result<I2cDis, Error<B::Error>> {
        let val = CfgRegC::read(self).await?.i2c_dis();
        Ok(I2cDis::try_from(val).unwrap_or_default())
    }
}

/// Converts LSB value to milligauss.
pub fn from_lsb_to_mgauss(lsb: i16) -> f32 {
    lsb as f32 * 1.5
}

/// Converts LSB to Celsius.
pub fn from_lsb_to_celsius(lsb: i16) -> f32 {
    (lsb as f32 / 8.0) + 25.0
}

/// Represents the I2C address for the device.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq)]
pub enum I2CAddress {
    /// The I2C address for the device, set to `0x1e`.
    I2cAdd = 0x1e,
}

/// Device Who am I
pub const IIS2MDC_ID: u8 = 0x40;