lsm9ds0 0.3.0

Platform-agnostic async driver for the LSM9DS0 IMU
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
//! Configuration for the LSM9DS0 Accelerometer/Magnetometer/Gyroscope sensors
//!
//! This module provides a shadow register-based configuration for the LSM9DS0 sensor. The config
//! maintains local copies of hardware register values, eliminating the need for read-modify-write
//! operations when changing settings.
//!
//! Use [`Lsm9ds0Config::new()`] with the builder pattern to configure sensor settings before
//! initialization. See the [crate-level documentation](crate) for complete usage examples.

use crate::registers::*;

/// Temperature sensor scale factor: 8 LSB/°C from datasheet Table 4
pub(crate) const TEMP_SCALE: f32 = 8.0;

/// Default temperature bias/offset in °C
///
/// **Important**: The LSM9DS0 datasheet does NOT specify an absolute temperature reference point.
/// The temperature sensor outputs relative temperature changes from an unspecified baseline.
/// This default value of 25°C is a reasonable assumption for typical room temperature conditions,
/// but for accurate absolute temperature readings, you should:
///
/// 1. Calibrate against a known reference thermometer
/// 2. Use [`Lsm9ds0Config::with_temperature_offset`] to set a custom offset
///
/// The temperature reading is calculated as: `raw_value / 8.0 + offset`
pub const DEFAULT_TEMP_BIAS: f32 = 25.0;

/// LSM9DS0 configuration with shadow registers
///
/// This struct maintains local copies of hardware register values, allowing configuration changes
/// without reading from the device first.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Lsm9ds0Config {
    // === Gyro shadow registers ===
    pub(crate) ctrl_reg1_g: CtrlReg1G,
    pub(crate) ctrl_reg2_g: CtrlReg2G,
    pub(crate) ctrl_reg3_g: CtrlReg3G,
    pub(crate) ctrl_reg4_g: CtrlReg4G,
    pub(crate) ctrl_reg5_g: CtrlReg5G,
    pub(crate) fifo_ctrl_reg_g: FifoCtrlRegG,
    pub(crate) int1_cfg_g: Int1CfgG,
    pub(crate) int1_duration_g: Int1DurationG,

    // === Gyro multi-byte values (per-axis) ===
    pub(crate) gyro_int_ths_x: u16, // 15-bit
    pub(crate) gyro_int_ths_y: u16,
    pub(crate) gyro_int_ths_z: u16,

    // === XM shadow registers ===
    pub(crate) ctrl_reg0_xm: CtrlReg0Xm,
    pub(crate) ctrl_reg1_xm: CtrlReg1Xm,
    pub(crate) ctrl_reg2_xm: CtrlReg2Xm,
    pub(crate) ctrl_reg3_xm: CtrlReg3Xm,
    pub(crate) ctrl_reg4_xm: CtrlReg4Xm,
    pub(crate) ctrl_reg5_xm: CtrlReg5Xm,
    pub(crate) ctrl_reg6_xm: CtrlReg6Xm,
    pub(crate) ctrl_reg7_xm: CtrlReg7Xm,
    pub(crate) fifo_ctrl_reg: FifoCtrlReg,
    pub(crate) int_gen_1_reg: IntGenReg,
    pub(crate) int_gen_1_ths: u8,      // 7-bit
    pub(crate) int_gen_1_duration: u8, // 7-bit
    pub(crate) int_gen_2_reg: IntGenReg,
    pub(crate) int_gen_2_ths: u8,
    pub(crate) int_gen_2_duration: u8,
    pub(crate) int_ctrl_reg_m: IntCtrlRegM,
    pub(crate) click_cfg: ClickCfg,
    pub(crate) click_ths: u8, // 7-bit
    pub(crate) time_limit_ms: u8,
    pub(crate) time_latency_ms: u8,
    pub(crate) time_window_ms: u8,
    pub(crate) act_ths: u8, // 7-bit
    pub(crate) act_dur: u8,

    // === XM multi-byte values ===
    pub(crate) mag_int_ths: u16, // 15-bit
    pub(crate) mag_offset_x: i16,
    pub(crate) mag_offset_y: i16,
    pub(crate) mag_offset_z: i16,

    // Useful for gyro sleep mode where all axes are disabled.
    pub(crate) gyro_axes_enabled: (bool, bool, bool),

    // === Calibration ===
    pub(crate) temp_offset: f32,
    pub(crate) gyro_bias: (f32, f32, f32),
    pub(crate) accel_bias: (f32, f32, f32),
    pub(crate) auto_calibration: Option<crate::types::Orientation>,
}

impl Default for Lsm9ds0Config {
    /// Create a configuration with datasheet power-on-reset default values
    fn default() -> Self {
        Self {
            // Gyro defaults per datasheet
            // CTRL_REG1_G default: 0x07 (PD=0, all axes enabled)
            ctrl_reg1_g: CtrlReg1G::new()
                .with_dr(GyroDataRate::Hz95)
                .with_bw(GyroBandwidth::Bw0)
                .with_pd(PowerMode::PowerDown) // Power-down by default per datasheet
                .with_zen(Enable::Enabled)
                .with_yen(Enable::Enabled)
                .with_xen(Enable::Enabled),

            // CTRL_REG2_G default: 0x00
            ctrl_reg2_g: CtrlReg2G::new()
                .with_hpm(GyroHpfMode::NormalReset)
                .with_hpcf(GyroHpfCutoff::Cutoff0),

            // CTRL_REG3_G default: 0x00
            ctrl_reg3_g: CtrlReg3G::new(),

            // CTRL_REG4_G default: 0x00
            ctrl_reg4_g: CtrlReg4G::new()
                .with_fs(GyroScale::Dps245)
                .with_bdu(BlockDataUpdate::Continuous)
                .with_ble(Endianness::Little)
                .with_st(GyroSelfTest::Disabled)
                .with_sim(SpiMode::FourWire),

            // CTRL_REG5_G default: 0x00
            ctrl_reg5_g: CtrlReg5G::new(),

            // FIFO_CTRL_REG_G default: 0x00
            fifo_ctrl_reg_g: FifoCtrlRegG::new().with_fm(FifoMode::Bypass).with_wtm(0),

            // INT1_CFG_G default: 0x00
            int1_cfg_g: Int1CfgG::new(),

            // INT1_DURATION_G default: 0x00
            int1_duration_g: Int1DurationG::new(),

            // Gyro interrupt thresholds default: 0x00
            gyro_int_ths_x: 0,
            gyro_int_ths_y: 0,
            gyro_int_ths_z: 0,

            // XM defaults per datasheet
            // CTRL_REG0_XM default: 0x00
            ctrl_reg0_xm: CtrlReg0Xm::new(),

            // CTRL_REG1_XM default: 0x07 (all axes enabled, power-down)
            ctrl_reg1_xm: CtrlReg1Xm::new()
                .with_aodr(AccelDataRate::PowerDown)
                .with_bdu(BlockDataUpdate::Continuous)
                .with_azen(Enable::Enabled)
                .with_ayen(Enable::Enabled)
                .with_axen(Enable::Enabled),

            // CTRL_REG2_XM default: 0x00
            ctrl_reg2_xm: CtrlReg2Xm::new()
                .with_abw(AccelBandwidth::Hz773)
                .with_afs(AccelScale::G2)
                .with_ast(AccelSelfTest::Normal)
                .with_sim(SpiMode::FourWire),

            // CTRL_REG3_XM default: 0x00
            ctrl_reg3_xm: CtrlReg3Xm::new(),

            // CTRL_REG4_XM default: 0x00
            ctrl_reg4_xm: CtrlReg4Xm::new(),

            // CTRL_REG5_XM default: 0x18 (M_RES=0, M_ODR=0b110 but that's reserved, TEMP_EN=0)
            // Using reasonable defaults instead of reserved values
            ctrl_reg5_xm: CtrlReg5Xm::new()
                .with_temp_en(Enable::Disabled)
                .with_m_res(MagResolution::Low)
                .with_m_odr(MagDataRate::Hz50)
                .with_lir1(LatchInterrupt::NotLatched)
                .with_lir2(LatchInterrupt::NotLatched),

            // CTRL_REG6_XM default: 0x20 (MFS=01 = ±4 gauss)
            ctrl_reg6_xm: CtrlReg6Xm::new().with_mfs(MagScale::Gauss4),

            // CTRL_REG7_XM default: 0x03
            //
            // NOTE: Datasheet (pg 60/74 DocID024763 Rev 2) says the default value should be 0x02,
            // but directly reading this register from a fresh power-on shows a default value of
            // 0x03, so this initial default configuration reflects 0x03.
            ctrl_reg7_xm: CtrlReg7Xm::new()
                .with_md(MagMode::PowerDown)
                .with_mlp(MagLowPower::Normal)
                .with_afds(Enable::Disabled)
                .with_ahpm(AccelHpfMode::NormalReset),

            // FIFO_CTRL_REG default: 0x00
            fifo_ctrl_reg: FifoCtrlReg::new().with_fm(FifoMode::Bypass).with_fth(0),

            // Interrupt generator defaults: 0x00
            int_gen_1_reg: IntGenReg::new(),
            int_gen_1_ths: 0,
            int_gen_1_duration: 0,
            int_gen_2_reg: IntGenReg::new(),
            int_gen_2_ths: 0,
            int_gen_2_duration: 0,

            // INT_CTRL_REG_M default: 0xE8
            //
            // NOTE: Datasheet (pg 53/74 DocID024763 Rev 2) says the default value should be 0x00,
            // but directly reading this register from a fresh power-on shows a default value of
            // 0xE8, so this initial default configuration reflects 0xE8.
            int_ctrl_reg_m: IntCtrlRegM::new()
                .with_iea(ActiveLevelInverted::ActiveHigh)
                .with_zmien(Enable::Enabled)
                .with_ymien(Enable::Enabled)
                .with_xmien(Enable::Enabled),

            // Click defaults: 0x00
            click_cfg: ClickCfg::new(),
            click_ths: 0,
            time_limit_ms: 0,
            time_latency_ms: 0,
            time_window_ms: 0,

            // Activity defaults: 0x00
            act_ths: 0,
            act_dur: 0,

            // Multi-byte value defaults
            mag_int_ths: 0,
            mag_offset_x: 0,
            mag_offset_y: 0,
            mag_offset_z: 0,

            // Temperature calibration
            temp_offset: DEFAULT_TEMP_BIAS,

            // Gyro sleep mode - default to all axes enabled
            gyro_axes_enabled: (true, true, true),

            // Bias calibration - default to no bias correction
            gyro_bias: (0.0, 0.0, 0.0),
            accel_bias: (0.0, 0.0, 0.0),
            auto_calibration: None,
        }
    }
}

impl Lsm9ds0Config {
    /// Create a new configuration with datasheet default values
    pub fn new() -> Self {
        Self::default()
    }
}

/// # Derived Values
///
/// Methods for retrieving calculated values based on current configuration settings.
impl Lsm9ds0Config {
    /// Get gyro sensitivity in mdps/LSB based on current scale setting
    pub fn gyro_sensitivity(&self) -> f32 {
        self.ctrl_reg4_g.fs().sensitivity()
    }

    /// Get accel sensitivity in mg/LSB based on current scale setting
    pub fn accel_sensitivity(&self) -> f32 {
        self.ctrl_reg2_xm.afs().sensitivity()
    }

    /// Get mag sensitivity in mgauss/LSB based on current scale setting
    pub fn mag_sensitivity(&self) -> f32 {
        self.ctrl_reg6_xm.mfs().sensitivity()
    }

    /// Get gyro HPF cutoff frequency in Hz (depends on both ODR and HPCF)
    pub fn gyro_hpf_cutoff_hz(&self) -> f32 {
        const CUTOFF_TABLE: [[f32; 10]; 4] = [
            // ODR = 95 Hz
            [7.2, 3.5, 1.8, 0.9, 0.45, 0.18, 0.09, 0.045, 0.018, 0.009],
            // ODR = 190 Hz
            [13.5, 7.2, 3.5, 1.8, 0.9, 0.45, 0.18, 0.09, 0.045, 0.018],
            // ODR = 380 Hz
            [27.0, 13.5, 7.2, 3.5, 1.8, 0.9, 0.45, 0.18, 0.09, 0.045],
            // ODR = 760 Hz
            [51.4, 27.0, 13.5, 7.2, 3.5, 1.8, 0.9, 0.45, 0.18, 0.09],
        ];

        let odr_idx = self.ctrl_reg1_g.dr() as usize;
        let cutoff_idx = self.ctrl_reg2_g.hpcf() as usize;
        CUTOFF_TABLE[odr_idx][cutoff_idx.min(9)]
    }

    /// Get gyro LPF (low-pass filter) cutoff frequency in Hz
    ///
    /// The cutoff depends on both ODR and bandwidth setting (see datasheet Table 21).
    pub fn gyro_lpf_cutoff_hz(&self) -> f32 {
        const CUTOFF_TABLE: [[f32; 4]; 4] = [
            // ODR = 95 Hz:  BW0=12.5, BW1=25, BW2=25, BW3=25
            [12.5, 25.0, 25.0, 25.0],
            // ODR = 190 Hz: BW0=12.5, BW1=25, BW2=50, BW3=70
            [12.5, 25.0, 50.0, 70.0],
            // ODR = 380 Hz: BW0=20, BW1=25, BW2=50, BW3=100
            [20.0, 25.0, 50.0, 100.0],
            // ODR = 760 Hz: BW0=30, BW1=35, BW2=50, BW3=100
            [30.0, 35.0, 50.0, 100.0],
        ];

        let odr_idx = self.ctrl_reg1_g.dr() as usize;
        let bw_idx = self.ctrl_reg1_g.bw() as usize;
        CUTOFF_TABLE[odr_idx][bw_idx]
    }

    /// Get the configured temperature offset in °C
    ///
    /// This offset is added to the raw temperature reading to produce absolute temperature.
    /// See [`DEFAULT_TEMP_BIAS`] for calibration details.
    pub fn temperature_offset(&self) -> f32 {
        self.temp_offset
    }

    /// Get current gyro scale setting
    pub fn gyro_scale(&self) -> GyroScale {
        self.ctrl_reg4_g.fs()
    }

    /// Get current gyro data rate setting
    pub fn gyro_data_rate(&self) -> GyroDataRate {
        self.ctrl_reg1_g.dr()
    }

    /// Get current accel scale setting
    pub fn accel_scale(&self) -> AccelScale {
        self.ctrl_reg2_xm.afs()
    }

    /// Get current accel data rate setting
    pub fn accel_data_rate(&self) -> AccelDataRate {
        self.ctrl_reg1_xm.aodr()
    }

    /// Get current mag scale setting
    pub fn mag_scale(&self) -> MagScale {
        self.ctrl_reg6_xm.mfs()
    }

    /// Get current mag data rate setting
    pub fn mag_data_rate(&self) -> MagDataRate {
        self.ctrl_reg5_xm.m_odr()
    }
}

/// # Gyroscope Configuration
///
/// Methods for configuring the gyroscope sensor including scale, data rate, filtering, FIFO, and
/// interrupt settings.
impl Lsm9ds0Config {
    /// Enable or disable the gyroscope (sets power mode)
    ///
    /// When enabled, uses Normal mode with configured axes.
    /// When disabled, uses Power-down mode.
    /// For Sleep mode (faster wake-up), use [`with_gyro_power_mode`](Self::with_gyro_power_mode).
    pub fn with_gyro_enabled(mut self, enabled: bool) -> Self {
        if enabled {
            self.ctrl_reg1_g.set_pd(PowerMode::Normal);
            // Restore saved axis states
            let (x, y, z) = self.gyro_axes_enabled;
            self.ctrl_reg1_g.set_xen(Enable::from(x));
            self.ctrl_reg1_g.set_yen(Enable::from(y));
            self.ctrl_reg1_g.set_zen(Enable::from(z));
        } else {
            self.ctrl_reg1_g.set_pd(PowerMode::PowerDown);
        }
        self
    }

    /// Set gyroscope power mode with sleep support
    ///
    /// The gyroscope supports three power modes (see datasheet Table 22):
    /// - **PowerDown**: Lowest power consumption, sensor completely off
    /// - **Sleep**: Lower power than normal, but faster wake-up than power-down.
    ///   PD=1 with all axes disabled. Useful for motion-triggered wake-up scenarios.
    /// - **Normal**: Full operation with configured axes enabled
    ///
    /// # Example
    /// ```
    /// use lsm9ds0::{Lsm9ds0Config, GyroPowerMode, GyroDataRate};
    ///
    /// // Configure for sleep mode with fast wake-up
    /// let config = Lsm9ds0Config::new()
    ///     .with_gyro_data_rate(GyroDataRate::Hz190)
    ///     .with_gyro_power_mode(GyroPowerMode::Sleep);
    /// ```
    pub fn with_gyro_power_mode(mut self, mode: GyroPowerMode) -> Self {
        match mode {
            GyroPowerMode::PowerDown => {
                self.ctrl_reg1_g.set_pd(PowerMode::PowerDown);
            }
            GyroPowerMode::Sleep => {
                // Sleep mode: PD=1, all axes disabled
                self.ctrl_reg1_g.set_pd(PowerMode::Normal);
                self.ctrl_reg1_g.set_xen(Enable::Disabled);
                self.ctrl_reg1_g.set_yen(Enable::Disabled);
                self.ctrl_reg1_g.set_zen(Enable::Disabled);
            }
            GyroPowerMode::Normal => {
                self.ctrl_reg1_g.set_pd(PowerMode::Normal);
                // Restore saved axis states
                let (x, y, z) = self.gyro_axes_enabled;
                self.ctrl_reg1_g.set_xen(Enable::from(x));
                self.ctrl_reg1_g.set_yen(Enable::from(y));
                self.ctrl_reg1_g.set_zen(Enable::from(z));
            }
        }
        self
    }

    /// Set the gyroscope full-scale range
    pub fn with_gyro_scale(mut self, scale: GyroScale) -> Self {
        self.ctrl_reg4_g.set_fs(scale);
        self
    }

    /// Set the gyroscope output data rate
    pub fn with_gyro_data_rate(mut self, rate: GyroDataRate) -> Self {
        self.ctrl_reg1_g.set_dr(rate);
        self
    }

    /// Set the gyroscope bandwidth
    pub fn with_gyro_bandwidth(mut self, bw: GyroBandwidth) -> Self {
        self.ctrl_reg1_g.set_bw(bw);
        self
    }

    /// Enable or disable individual gyroscope axes
    ///
    /// The axis enable state is saved and restored when switching between power modes.
    pub fn with_gyro_axes(mut self, x: bool, y: bool, z: bool) -> Self {
        // Save for sleep/wake restoration
        self.gyro_axes_enabled = (x, y, z);

        self.ctrl_reg1_g.set_xen(Enable::from(x));
        self.ctrl_reg1_g.set_yen(Enable::from(y));
        self.ctrl_reg1_g.set_zen(Enable::from(z));
        self
    }

    /// Configure the gyroscope high-pass filter.
    ///
    /// - `enabled`: routes HPF output to the data registers and FIFO
    /// - `mode`: filter behaviour — [`GyroHpfMode::NormalReset`] (reset on read of
    ///   `INT1_SRC_G`), [`GyroHpfMode::Reference`] (subtract a reference value set via
    ///   [`Lsm9ds0::set_gyro_reference`](crate::Lsm9ds0::set_gyro_reference)), or
    ///   [`GyroHpfMode::Normal`] (standard HPF)
    /// - `cutoff`: cutoff frequency index (0–9); actual frequency depends on ODR —
    ///   see [`Lsm9ds0Config::gyro_hpf_cutoff_hz`] or datasheet Table 25
    ///
    /// ```
    /// use lsm9ds0::{Lsm9ds0Config, GyroDataRate, GyroHpfMode, GyroHpfCutoff};
    ///
    /// // Standard HPF at ~0.9 Hz cutoff with 95 Hz ODR (cutoff index 3, see datasheet Table 25)
    /// let config = Lsm9ds0Config::new()
    ///     .with_gyro_enabled(true)
    ///     .with_gyro_data_rate(GyroDataRate::Hz95)
    ///     .with_gyro_hpf(true, GyroHpfMode::Normal, GyroHpfCutoff::Cutoff3);
    /// ```
    pub fn with_gyro_hpf(
        mut self,
        enabled: bool,
        mode: GyroHpfMode,
        cutoff: GyroHpfCutoff,
    ) -> Self {
        self.ctrl_reg5_g.set_hpen(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg2_g.set_hpm(mode);
        self.ctrl_reg2_g.set_hpcf(cutoff);
        self
    }

    /// Configure the gyroscope FIFO.
    ///
    /// - `enabled`: gates FIFO output into the gyroscope data registers
    /// - `mode`: FIFO operating mode (e.g., [`FifoMode::Bypass`], [`FifoMode::Stream`],
    ///   [`FifoMode::Fifo`]); see datasheet Table 54 for full descriptions
    /// - `watermark`: number of samples (0–31) at which the watermark flag is set
    pub fn with_gyro_fifo(mut self, enabled: bool, mode: FifoMode, watermark: u8) -> Self {
        self.ctrl_reg5_g.set_fifo_en(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.fifo_ctrl_reg_g.set_fm(mode);
        self.fifo_ctrl_reg_g.set_wtm(watermark & 0x1F);
        self
    }

    /// Set gyroscope self-test mode
    pub fn with_gyro_self_test(mut self, mode: GyroSelfTest) -> Self {
        self.ctrl_reg4_g.set_st(mode);
        self
    }

    /// Set gyroscope block data update mode
    pub fn with_gyro_bdu(mut self, enabled: bool) -> Self {
        self.ctrl_reg4_g.set_bdu(if enabled {
            BlockDataUpdate::WaitForRead
        } else {
            BlockDataUpdate::Continuous
        });
        self
    }
}

/// # Gyroscope Interrupts
///
/// Methods for configuring gyroscope interrupt generation, thresholds, and pin routing.
impl Lsm9ds0Config {
    /// Enable gyroscope interrupt on INT_G pin
    pub fn with_gyro_int_enabled(mut self, enabled: bool) -> Self {
        self.ctrl_reg3_g.set_i1_int1(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Configure gyroscope interrupt polarity and pin mode
    pub fn with_gyro_int_pin_config(mut self, active_low: bool, open_drain: bool) -> Self {
        self.ctrl_reg3_g.set_h_lactive(if active_low {
            ActiveLevel::ActiveLow
        } else {
            ActiveLevel::ActiveHigh
        });
        self.ctrl_reg3_g.set_pp_od(if open_drain {
            OutputType::OpenDrain
        } else {
            OutputType::PushPull
        });
        self
    }

    /// Configure which gyroscope axes trigger interrupts
    pub fn with_gyro_int_axes(
        mut self,
        x_high: bool,
        x_low: bool,
        y_high: bool,
        y_low: bool,
        z_high: bool,
        z_low: bool,
    ) -> Self {
        self.int1_cfg_g.set_xhie(if x_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int1_cfg_g.set_xlie(if x_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int1_cfg_g.set_yhie(if y_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int1_cfg_g.set_ylie(if y_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int1_cfg_g.set_zhie(if z_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int1_cfg_g.set_zlie(if z_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set gyroscope interrupt combination mode and latch
    pub fn with_gyro_int_mode(mut self, and_combination: bool, latch: bool) -> Self {
        self.int1_cfg_g.set_and_or(if and_combination {
            InterruptCombination::And
        } else {
            InterruptCombination::Or
        });
        self.int1_cfg_g.set_lir(LatchInterrupt::from(latch));
        self
    }

    /// Set same gyroscope interrupt threshold for all axes (15-bit value, max 0x7FFF)
    pub fn with_gyro_int_threshold(mut self, threshold: u16) -> Self {
        debug_assert!(
            threshold <= 0x7FFF,
            "gyro interrupt threshold {threshold:#06X} exceeds 15-bit max (0x7FFF)"
        );
        let masked = threshold & 0x7FFF;
        self.gyro_int_ths_x = masked;
        self.gyro_int_ths_y = masked;
        self.gyro_int_ths_z = masked;
        self
    }

    /// Set per-axis gyroscope interrupt thresholds (15-bit values, max 0x7FFF each)
    pub fn with_gyro_int_threshold_xyz(mut self, x: u16, y: u16, z: u16) -> Self {
        debug_assert!(
            x <= 0x7FFF,
            "gyro X interrupt threshold {x:#06X} exceeds 15-bit max (0x7FFF)"
        );
        debug_assert!(
            y <= 0x7FFF,
            "gyro Y interrupt threshold {y:#06X} exceeds 15-bit max (0x7FFF)"
        );
        debug_assert!(
            z <= 0x7FFF,
            "gyro Z interrupt threshold {z:#06X} exceeds 15-bit max (0x7FFF)"
        );
        self.gyro_int_ths_x = x & 0x7FFF;
        self.gyro_int_ths_y = y & 0x7FFF;
        self.gyro_int_ths_z = z & 0x7FFF;
        self
    }

    /// Set whether the interrupt signal holds for the full duration after the condition clears.
    ///
    /// When `true`, the INT_G pin stays active for `duration` ODR cycles after the measurement
    /// drops back below threshold (symmetrical de-assertion). When `false` (default), the
    /// interrupt clears as soon as the measurement falls below threshold.
    ///
    /// ```
    /// use lsm9ds0::Lsm9ds0Config;
    ///
    /// // Hold the interrupt pin active for 5 ODR cycles after motion stops
    /// let config = Lsm9ds0Config::new()
    ///     .with_gyro_motion_threshold(500)
    ///     .with_gyro_int_duration(5)
    ///     .with_gyro_int_wait(true);
    /// ```
    pub fn with_gyro_int_wait(mut self, wait: bool) -> Self {
        self.int1_duration_g.set_wait(wait);
        self
    }

    /// Set gyroscope interrupt duration (7-bit value, max 0x7F)
    pub fn with_gyro_int_duration(mut self, duration: u8) -> Self {
        debug_assert!(
            duration <= 0x7F,
            "gyro interrupt duration {duration:#04X} exceeds 7-bit max (0x7F)"
        );
        self.int1_duration_g.set_duration(duration & 0x7F);
        self
    }

    /// Simple gyroscope motion detection - triggers when any axis exceeds threshold
    ///
    /// Threshold is a 15-bit value (max 0x7FFF).
    pub fn with_gyro_motion_threshold(mut self, threshold: u16) -> Self {
        debug_assert!(
            threshold <= 0x7FFF,
            "gyro motion threshold {threshold:#06X} exceeds 15-bit max (0x7FFF)"
        );
        self.ctrl_reg3_g.set_i1_int1(Enable::Enabled);
        self.int1_cfg_g.set_and_or(InterruptCombination::Or); // OR combination
        self.int1_cfg_g.set_lir(LatchInterrupt::NotLatched);
        self.int1_cfg_g.set_xhie(Enable::Enabled);
        self.int1_cfg_g.set_xlie(Enable::Enabled);
        self.int1_cfg_g.set_yhie(Enable::Enabled);
        self.int1_cfg_g.set_ylie(Enable::Enabled);
        self.int1_cfg_g.set_zhie(Enable::Enabled);
        self.int1_cfg_g.set_zlie(Enable::Enabled);
        let masked = threshold & 0x7FFF;
        self.gyro_int_ths_x = masked;
        self.gyro_int_ths_y = masked;
        self.gyro_int_ths_z = masked;
        self.int1_duration_g = Int1DurationG::new();
        self
    }

    /// Configure gyroscope data-ready and FIFO interrupts on DRDY_G pin
    pub fn with_gyro_drdy_int(
        mut self,
        data_ready: bool,
        fifo_wtm: bool,
        fifo_overrun: bool,
        fifo_empty: bool,
    ) -> Self {
        self.ctrl_reg3_g.set_i2_drdy(if data_ready {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_g.set_i2_wtm(if fifo_wtm {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_g.set_i2_orun(if fifo_overrun {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_g.set_i2_empty(if fifo_empty {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }
}

/// # Accelerometer Configuration
///
/// Methods for configuring the accelerometer sensor including scale, data rate, filtering, and FIFO
/// settings.
impl Lsm9ds0Config {
    /// Set the accelerometer output data rate (also controls power mode)
    pub fn with_accel_data_rate(mut self, rate: AccelDataRate) -> Self {
        // If mag is at 100 Hz, accel must be > 50 Hz or power-down
        // (Datasheet Table 84, footnote 1)
        let mag_odr = self.ctrl_reg5_xm.m_odr();
        if mag_odr == MagDataRate::Hz100 {
            debug_assert!(
                matches!(
                    rate,
                    AccelDataRate::PowerDown
                        | AccelDataRate::Hz100
                        | AccelDataRate::Hz200
                        | AccelDataRate::Hz400
                        | AccelDataRate::Hz800
                        | AccelDataRate::Hz1600
                ),
                "AccelDataRate must be > 50 Hz or PowerDown when MagDataRate is Hz100, got {:?}",
                rate
            );
        }
        self.ctrl_reg1_xm.set_aodr(rate);
        self
    }

    /// Set the accelerometer full-scale range
    pub fn with_accel_scale(mut self, scale: AccelScale) -> Self {
        self.ctrl_reg2_xm.set_afs(scale);
        self
    }

    /// Set the accelerometer anti-alias filter bandwidth
    pub fn with_accel_bandwidth(mut self, bw: AccelBandwidth) -> Self {
        self.ctrl_reg2_xm.set_abw(bw);
        self
    }

    /// Enable or disable individual accelerometer axes
    pub fn with_accel_axes(mut self, x: bool, y: bool, z: bool) -> Self {
        self.ctrl_reg1_xm.set_axen(Enable::from(x));
        self.ctrl_reg1_xm.set_ayen(Enable::from(y));
        self.ctrl_reg1_xm.set_azen(Enable::from(z));
        self
    }

    /// Set accelerometer block data update mode
    pub fn with_accel_bdu(mut self, enabled: bool) -> Self {
        self.ctrl_reg1_xm.set_bdu(if enabled {
            BlockDataUpdate::WaitForRead
        } else {
            BlockDataUpdate::Continuous
        });
        self
    }

    /// Set accelerometer self-test mode
    pub fn with_accel_self_test(mut self, mode: AccelSelfTest) -> Self {
        self.ctrl_reg2_xm.set_ast(mode);
        self
    }

    /// Configure the accelerometer high-pass filter.
    ///
    /// - `enabled`: routes HPF output to the data registers (filtered data)
    /// - `mode`: filter behaviour — [`AccelHpfMode::NormalReset`] (reset on read of
    ///   `INT_GEN_x_SRC`), [`AccelHpfMode::Reference`] (subtract a reference value set via
    ///   [`Lsm9ds0::set_accel_reference`](crate::Lsm9ds0::set_accel_reference)), or
    ///   [`AccelHpfMode::Normal`] / [`AccelHpfMode::AutoReset`] (standard HPF modes)
    ///
    /// ```
    /// use lsm9ds0::{Lsm9ds0Config, AccelDataRate, AccelHpfMode};
    ///
    /// // Remove DC offset / gravity from accelerometer output
    /// let config = Lsm9ds0Config::new()
    ///     .with_accel_data_rate(AccelDataRate::Hz100)
    ///     .with_accel_hpf(true, AccelHpfMode::Normal);
    /// ```
    pub fn with_accel_hpf(mut self, enabled: bool, mode: AccelHpfMode) -> Self {
        self.ctrl_reg7_xm.set_afds(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg7_xm.set_ahpm(mode);
        self
    }

    /// Configure accelerometer HPF for click/interrupt functions
    pub fn with_accel_hpf_functions(
        mut self,
        for_click: bool,
        for_int1: bool,
        for_int2: bool,
    ) -> Self {
        self.ctrl_reg0_xm.set_hp_click(if for_click {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg0_xm.set_hpis1(if for_int1 {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg0_xm.set_hpis2(if for_int2 {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Configure the accelerometer FIFO.
    ///
    /// - `enabled`: gates FIFO output into the accelerometer data registers
    /// - `mode`: FIFO operating mode (e.g., [`FifoMode::Bypass`], [`FifoMode::Stream`],
    ///   [`FifoMode::Fifo`]); see datasheet Table 54 for full descriptions
    /// - `watermark`: number of samples (0–31) at which the watermark flag is set
    pub fn with_accel_fifo(mut self, enabled: bool, mode: FifoMode, watermark: u8) -> Self {
        self.ctrl_reg0_xm.set_fifo_en(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.fifo_ctrl_reg.set_fm(mode);
        self.fifo_ctrl_reg.set_fth(watermark & 0x1F);
        self
    }
}

/// # Accelerometer Interrupts
///
/// Methods for configuring accelerometer interrupt generation, thresholds, click/tap detection,
/// activity detection, and pin routing.
impl Lsm9ds0Config {
    /// Configure accelerometer interrupt generator 1
    pub fn with_accel_int1_axes(
        mut self,
        x_high: bool,
        x_low: bool,
        y_high: bool,
        y_low: bool,
        z_high: bool,
        z_low: bool,
    ) -> Self {
        self.int_gen_1_reg.set_xhie(if x_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_1_reg.set_xlie(if x_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_1_reg.set_yhie(if y_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_1_reg.set_ylie(if y_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_1_reg.set_zhie(if z_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_1_reg.set_zlie(if z_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set accelerometer interrupt 1 mode (AND/OR combination, 6D detection)
    pub fn with_accel_int1_mode(mut self, and_combination: bool, six_d: bool) -> Self {
        self.int_gen_1_reg.set_aoi(if and_combination {
            InterruptCombination::And
        } else {
            InterruptCombination::Or
        });
        self.int_gen_1_reg.set_six_d(if six_d {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set accelerometer interrupt 1 threshold (7-bit value, max 0x7F)
    pub fn with_accel_int1_threshold(mut self, threshold: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "accel int1 threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.int_gen_1_ths = threshold & 0x7F;
        self
    }

    /// Set accelerometer interrupt 1 duration (7-bit value, max 0x7F)
    pub fn with_accel_int1_duration(mut self, duration: u8) -> Self {
        debug_assert!(
            duration <= 0x7F,
            "accel int1 duration {duration:#04X} exceeds 7-bit max (0x7F)"
        );
        self.int_gen_1_duration = duration & 0x7F;
        self
    }

    /// Set accelerometer interrupt 1 latch mode
    pub fn with_accel_int1_latch(mut self, latch: bool) -> Self {
        self.ctrl_reg5_xm.set_lir1(LatchInterrupt::from(latch));
        self
    }

    /// Simple accelerometer motion detection using interrupt 1
    ///
    /// Threshold is a 7-bit value (max 0x7F).
    pub fn with_accel_motion_threshold(mut self, threshold: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "accel motion threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.ctrl_reg3_xm.set_p1_int1(Enable::Enabled);
        self.int_gen_1_reg.set_aoi(InterruptCombination::Or); // OR combination
        self.int_gen_1_reg.set_six_d(Enable::Disabled);
        self.int_gen_1_reg.set_xhie(Enable::Enabled);
        self.int_gen_1_reg.set_xlie(Enable::Enabled);
        self.int_gen_1_reg.set_yhie(Enable::Enabled);
        self.int_gen_1_reg.set_ylie(Enable::Enabled);
        self.int_gen_1_reg.set_zhie(Enable::Enabled);
        self.int_gen_1_reg.set_zlie(Enable::Enabled);
        self.int_gen_1_ths = threshold & 0x7F;
        self.int_gen_1_duration = 0;
        self
    }

    /// Configure accelerometer interrupt generator 2
    pub fn with_accel_int2_axes(
        mut self,
        x_high: bool,
        x_low: bool,
        y_high: bool,
        y_low: bool,
        z_high: bool,
        z_low: bool,
    ) -> Self {
        self.int_gen_2_reg.set_xhie(if x_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_2_reg.set_xlie(if x_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_2_reg.set_yhie(if y_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_2_reg.set_ylie(if y_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_2_reg.set_zhie(if z_high {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_gen_2_reg.set_zlie(if z_low {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set accelerometer interrupt 2 mode (AND/OR combination, 6D detection)
    pub fn with_accel_int2_mode(mut self, and_combination: bool, six_d: bool) -> Self {
        self.int_gen_2_reg.set_aoi(if and_combination {
            InterruptCombination::And
        } else {
            InterruptCombination::Or
        });
        self.int_gen_2_reg.set_six_d(if six_d {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set accelerometer interrupt 2 threshold (7-bit value, max 0x7F)
    pub fn with_accel_int2_threshold(mut self, threshold: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "accel int2 threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.int_gen_2_ths = threshold & 0x7F;
        self
    }

    /// Set accelerometer interrupt 2 duration (7-bit value, max 0x7F)
    pub fn with_accel_int2_duration(mut self, duration: u8) -> Self {
        debug_assert!(
            duration <= 0x7F,
            "accel int2 duration {duration:#04X} exceeds 7-bit max (0x7F)"
        );
        self.int_gen_2_duration = duration & 0x7F;
        self
    }

    /// Set accelerometer interrupt 2 latch mode
    pub fn with_accel_int2_latch(mut self, latch: bool) -> Self {
        self.ctrl_reg5_xm.set_lir2(LatchInterrupt::from(latch));
        self
    }

    /// Configure interrupt routing to INT1_XM pin
    #[allow(clippy::too_many_arguments)]
    pub fn with_int1_xm_routing(
        mut self,
        boot: bool,
        tap: bool,
        int1: bool,
        int2: bool,
        mag_int: bool,
        accel_drdy: bool,
        mag_drdy: bool,
        fifo_empty: bool,
    ) -> Self {
        self.ctrl_reg3_xm.set_p1_boot(if boot {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_tap(if tap {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_int1(if int1 {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_int2(if int2 {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_intm(if mag_int {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_drdya(if accel_drdy {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_drdym(if mag_drdy {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg3_xm.set_p1_empty(if fifo_empty {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Configure interrupt routing to INT2_XM pin
    #[allow(clippy::too_many_arguments)]
    pub fn with_int2_xm_routing(
        mut self,
        tap: bool,
        int1: bool,
        int2: bool,
        mag_int: bool,
        accel_drdy: bool,
        mag_drdy: bool,
        fifo_overrun: bool,
        fifo_wtm: bool,
    ) -> Self {
        self.ctrl_reg4_xm.set_p2_tap(if tap {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_int1(if int1 {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_int2(if int2 {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_intm(if mag_int {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_drdya(if accel_drdy {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_drdym(if mag_drdy {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_overrun(if fifo_overrun {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.ctrl_reg4_xm.set_p2_wtm(if fifo_wtm {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Configure click detection axes
    pub fn with_click_axes(
        mut self,
        x_single: bool,
        x_double: bool,
        y_single: bool,
        y_double: bool,
        z_single: bool,
        z_double: bool,
    ) -> Self {
        self.click_cfg.set_xs(if x_single {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.click_cfg.set_xd(if x_double {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.click_cfg.set_ys(if y_single {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.click_cfg.set_yd(if y_double {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.click_cfg.set_zs(if z_single {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.click_cfg.set_zd(if z_double {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set click detection threshold (7-bit value, max 0x7F)
    pub fn with_click_threshold(mut self, threshold: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "click threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.click_ths = threshold & 0x7F;
        self
    }

    /// Set click time limit (milliseconds)
    pub fn with_click_time_limit(mut self, limit: u8) -> Self {
        self.time_limit_ms = limit;
        self
    }

    /// Set click time latency (milliseconds)
    pub fn with_click_time_latency(mut self, latency: u8) -> Self {
        self.time_latency_ms = latency;
        self
    }

    /// Set click time window (milliseconds)
    pub fn with_click_time_window(mut self, window: u8) -> Self {
        self.time_window_ms = window;
        self
    }

    /// Simple single-click detection on all axes
    ///
    /// Threshold is a 7-bit value (max 0x7F).
    pub fn with_click_detection(mut self, threshold: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "click detection threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.click_cfg.set_xs(Enable::Enabled);
        self.click_cfg.set_ys(Enable::Enabled);
        self.click_cfg.set_zs(Enable::Enabled);
        self.click_ths = threshold & 0x7F;
        self.time_limit_ms = 10;
        self.time_latency_ms = 20;
        self.time_window_ms = 50;
        self
    }

    /// Set activity detection threshold (7-bit value, max 0x7F, 1 LSB = 16 mg)
    pub fn with_activity_threshold(mut self, threshold: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "activity threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.act_ths = threshold & 0x7F;
        self
    }

    /// Set activity detection duration
    pub fn with_activity_duration(mut self, duration: u8) -> Self {
        self.act_dur = duration;
        self
    }

    /// Configure activity/inactivity detection
    ///
    /// Threshold is a 7-bit value (max 0x7F).
    pub fn with_activity_detection(mut self, threshold: u8, duration: u8) -> Self {
        debug_assert!(
            threshold <= 0x7F,
            "activity threshold {threshold:#04X} exceeds 7-bit max (0x7F)"
        );
        self.act_ths = threshold & 0x7F;
        self.act_dur = duration;
        self
    }
}

/// # Magnetometer Configuration
///
/// Methods for configuring the magnetometer sensor including mode, scale, data rate, resolution,
/// and interrupt settings.
impl Lsm9ds0Config {
    /// Set the magnetometer operating mode
    pub fn with_mag_mode(mut self, mode: MagMode) -> Self {
        self.ctrl_reg7_xm.set_md(mode);
        self
    }

    /// Set the magnetometer output data rate
    pub fn with_mag_data_rate(mut self, rate: MagDataRate) -> Self {
        // MagDataRate::Hz100 requires accelerometer ODR > 50 Hz or power-down
        // (Datasheet Table 84, footnote 1)
        if rate == MagDataRate::Hz100 {
            let accel_odr = self.ctrl_reg1_xm.aodr();
            debug_assert!(
                matches!(
                    accel_odr,
                    AccelDataRate::PowerDown
                        | AccelDataRate::Hz100
                        | AccelDataRate::Hz200
                        | AccelDataRate::Hz400
                        | AccelDataRate::Hz800
                        | AccelDataRate::Hz1600
                ),
                "MagDataRate::Hz100 requires AccelDataRate > 50 Hz or PowerDown, got {:?}",
                accel_odr
            );
        }
        self.ctrl_reg5_xm.set_m_odr(rate);
        self
    }

    /// Set the magnetometer full-scale range
    pub fn with_mag_scale(mut self, scale: MagScale) -> Self {
        self.ctrl_reg6_xm.set_mfs(scale);
        self
    }

    /// Set the magnetometer resolution
    pub fn with_mag_resolution(mut self, res: MagResolution) -> Self {
        self.ctrl_reg5_xm.set_m_res(res);
        self
    }

    /// Enable magnetometer low power mode
    pub fn with_mag_low_power(mut self, enabled: bool) -> Self {
        self.ctrl_reg7_xm.set_mlp(if enabled {
            MagLowPower::LowPower
        } else {
            MagLowPower::Normal
        });
        self
    }

    /// Configure the magnetometer interrupt.
    ///
    /// - `enabled`: enables the interrupt generator (master switch)
    /// - `x`, `y`, `z`: enable interrupt generation for each axis individually
    /// - `active_high`: when `true`, the interrupt pin is active-high; when `false`, active-low
    /// - `latch`: when `true`, the interrupt pin latches until the source register is read;
    ///   when `false`, the pin pulses and clears automatically
    ///
    /// ```
    /// use lsm9ds0::{Lsm9ds0Config, MagMode, MagScale};
    ///
    /// // Trigger on any axis exceeding the threshold, active-high, auto-clearing
    /// let config = Lsm9ds0Config::new()
    ///     .with_mag_mode(MagMode::ContinuousConversion)
    ///     .with_mag_int(true, true, true, true, true, false)
    ///     .with_mag_int_threshold(2000);
    /// ```
    pub fn with_mag_int(
        mut self,
        enabled: bool,
        x: bool,
        y: bool,
        z: bool,
        active_high: bool,
        latch: bool,
    ) -> Self {
        self.int_ctrl_reg_m.set_mien(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self.int_ctrl_reg_m.set_xmien(Enable::from(x));
        self.int_ctrl_reg_m.set_ymien(Enable::from(y));
        self.int_ctrl_reg_m.set_zmien(Enable::from(z));
        self.int_ctrl_reg_m.set_iea(if active_high {
            ActiveLevelInverted::ActiveHigh
        } else {
            ActiveLevelInverted::ActiveLow
        });
        self.int_ctrl_reg_m.set_iel(LatchInterrupt::from(latch));
        self
    }

    /// Set magnetometer interrupt threshold (15-bit value, max 0x7FFF)
    pub fn with_mag_int_threshold(mut self, threshold: u16) -> Self {
        debug_assert!(
            threshold <= 0x7FFF,
            "mag interrupt threshold {threshold:#06X} exceeds 15-bit max (0x7FFF)"
        );
        self.mag_int_ths = threshold & 0x7FFF;
        self
    }

    /// Set magnetometer offset calibration for all axes
    pub fn with_mag_offset(mut self, x: i16, y: i16, z: i16) -> Self {
        self.mag_offset_x = x;
        self.mag_offset_y = y;
        self.mag_offset_z = z;
        self
    }

    /// Set magnetometer X-axis offset calibration
    pub fn with_mag_offset_x(mut self, offset: i16) -> Self {
        self.mag_offset_x = offset;
        self
    }

    /// Set magnetometer Y-axis offset calibration
    pub fn with_mag_offset_y(mut self, offset: i16) -> Self {
        self.mag_offset_y = offset;
        self
    }

    /// Set magnetometer Z-axis offset calibration
    pub fn with_mag_offset_z(mut self, offset: i16) -> Self {
        self.mag_offset_z = offset;
        self
    }
}

/// # Temperature Sensor Configuration
///
/// Methods for configuring the built-in temperature sensor.
impl Lsm9ds0Config {
    /// Enable or disable the temperature sensor
    pub fn with_temperature_enabled(mut self, enabled: bool) -> Self {
        self.ctrl_reg5_xm.set_temp_en(if enabled {
            Enable::Enabled
        } else {
            Enable::Disabled
        });
        self
    }

    /// Set the temperature offset for absolute temperature calculation
    ///
    /// The LSM9DS0 temperature sensor outputs relative temperature changes from an unspecified
    /// baseline. This offset is added to convert to absolute temperature.
    ///
    /// **Calibration procedure:**
    /// 1. Place the sensor in a known temperature environment
    /// 2. Read the raw temperature value
    /// 3. Calculate offset: `offset = known_temp - (raw_value / 8.0)`
    /// 4. Use this method to set the calculated offset
    ///
    /// # Example
    /// ```
    /// use lsm9ds0::Lsm9ds0Config;
    ///
    /// // After calibration, set custom offset
    /// let config = Lsm9ds0Config::new()
    ///     .with_temperature_enabled(true)
    ///     .with_temperature_offset(21.5); // Calibrated offset
    /// ```
    pub fn with_temperature_offset(mut self, offset: f32) -> Self {
        self.temp_offset = offset;
        self
    }
}

/// # Common Configuration
///
/// Convenience methods that configure multiple sensors at once.
impl Lsm9ds0Config {
    /// Set block data update for both gyro and accel
    pub fn with_block_data_update(mut self, enabled: bool) -> Self {
        let bdu = if enabled {
            BlockDataUpdate::WaitForRead
        } else {
            BlockDataUpdate::Continuous
        };
        self.ctrl_reg4_g.set_bdu(bdu);
        self.ctrl_reg1_xm.set_bdu(bdu);
        self
    }

    /// Configure SPI interface mode for both gyro and accel/mag
    ///
    /// - **FourWire** (default): Standard 4-wire SPI with separate SDI and SDO lines
    /// - **ThreeWire**: 3-wire SPI mode where SDI/SDO share a single bidirectional line
    ///
    /// Note: When using 3-wire mode, ensure your SPI bus and driver support bidirectional data
    /// transfer on a single line.
    pub fn with_spi_mode(mut self, mode: SpiMode) -> Self {
        self.ctrl_reg4_g.set_sim(mode);
        self.ctrl_reg2_xm.set_sim(mode);
        self
    }
}

/// # Bias Calibration Configuration
///
/// Methods for configuring gyroscope and accelerometer bias correction. Bias values are subtracted
/// from sensor readings to compensate for sensor offset errors.
impl Lsm9ds0Config {
    /// Set gyroscope bias correction values.
    ///
    /// These values (in degrees per second) are subtracted from gyroscope readings in
    /// [`Lsm9ds0::read_gyro`](crate::Lsm9ds0::read_gyro) to compensate for sensor offset.
    ///
    /// # Example
    /// ```
    /// use lsm9ds0::Lsm9ds0Config;
    ///
    /// // Apply pre-calibrated gyro bias values
    /// let config = Lsm9ds0Config::new()
    ///     .with_gyro_enabled(true)
    ///     .with_gyro_bias(0.5, -0.2, 0.1); // dps
    /// ```
    pub fn with_gyro_bias(mut self, x: f32, y: f32, z: f32) -> Self {
        self.gyro_bias = (x, y, z);
        self
    }

    /// Set accelerometer bias correction values.
    ///
    /// These values (in g-force) are subtracted from accelerometer readings in
    /// [`Lsm9ds0::read_accel`](crate::Lsm9ds0::read_accel) to compensate for sensor offset.
    ///
    /// # Example
    /// ```
    /// use lsm9ds0::Lsm9ds0Config;
    ///
    /// // Apply pre-calibrated accel bias values
    /// let config = Lsm9ds0Config::new()
    ///     .with_accel_data_rate(lsm9ds0::AccelDataRate::Hz100)
    ///     .with_accel_bias(0.01, -0.02, 0.03); // g
    /// ```
    pub fn with_accel_bias(mut self, x: f32, y: f32, z: f32) -> Self {
        self.accel_bias = (x, y, z);
        self
    }

    /// Enable automatic bias calibration during [`Lsm9ds0::init`](crate::Lsm9ds0::init).
    ///
    /// When enabled, the driver will use the FIFO to collect samples and calculate bias values
    /// automatically. The device must be stationary during initialization.
    ///
    /// The `orientation` parameter specifies which axis points "up" (against gravity) during
    /// calibration:
    /// - Use a specific orientation (e.g., [`Orientation::ZUp`](crate::Orientation::ZUp)) for
    ///   gravity-referenced readings where the accelerometer shows the gravity vector when stationary.
    /// - Use [`Orientation::Unknown`](crate::Orientation::Unknown) for pose-relative readings where
    ///   the accelerometer reads ~(0, 0, 0) in the calibration pose.
    ///
    /// **Note:** Auto-calibration requires a delay provider to be passed to
    /// [`Lsm9ds0::init`](crate::Lsm9ds0::init).
    ///
    /// # Example
    /// ```
    /// use lsm9ds0::{Lsm9ds0Config, Orientation};
    ///
    /// // Auto-calibrate with sensor Z-axis pointing up
    /// let config = Lsm9ds0Config::new()
    ///     .with_gyro_enabled(true)
    ///     .with_accel_data_rate(lsm9ds0::AccelDataRate::Hz100)
    ///     .with_auto_calibration(Orientation::ZUp);
    ///
    /// // Auto-calibrate without gravity compensation (pose-relative)
    /// let config = Lsm9ds0Config::new()
    ///     .with_gyro_enabled(true)
    ///     .with_accel_data_rate(lsm9ds0::AccelDataRate::Hz100)
    ///     .with_auto_calibration(Orientation::Unknown);
    /// ```
    pub fn with_auto_calibration(mut self, orientation: crate::types::Orientation) -> Self {
        self.auto_calibration = Some(orientation);
        self
    }

    /// Get the current gyroscope bias values (x, y, z) in degrees per second.
    pub fn gyro_bias(&self) -> (f32, f32, f32) {
        self.gyro_bias
    }

    /// Get the current accelerometer bias values (x, y, z) in g-force.
    pub fn accel_bias(&self) -> (f32, f32, f32) {
        self.accel_bias
    }

    /// Get the auto-calibration orientation, if configured.
    pub fn auto_calibration(&self) -> Option<crate::types::Orientation> {
        self.auto_calibration
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = Lsm9ds0Config::default();

        // Test gyroscope defaults match datasheet POR values
        assert_eq!(config.ctrl_reg1_g.pd(), PowerMode::PowerDown);
        assert_eq!(config.ctrl_reg1_g.dr(), GyroDataRate::Hz95);
        assert_eq!(config.ctrl_reg4_g.fs(), GyroScale::Dps245);
        assert_eq!(config.ctrl_reg4_g.bdu(), BlockDataUpdate::Continuous);

        // Test accelerometer defaults
        assert_eq!(config.ctrl_reg1_xm.aodr(), AccelDataRate::PowerDown);
        assert_eq!(config.ctrl_reg2_xm.afs(), AccelScale::G2);

        // Test magnetometer defaults
        assert_eq!(config.ctrl_reg6_xm.mfs(), MagScale::Gauss4);
        assert_eq!(config.ctrl_reg7_xm.md(), MagMode::PowerDown);

        // Test multi-byte value defaults
        assert_eq!(config.gyro_int_ths_x, 0);
        assert_eq!(config.mag_offset_x, 0);
    }

    #[test]
    fn test_builder_pattern() {
        let config = Lsm9ds0Config::new()
            .with_gyro_scale(GyroScale::Dps2000)
            .with_accel_scale(AccelScale::G8)
            .with_mag_scale(MagScale::Gauss12)
            .with_block_data_update(true)
            .with_temperature_enabled(true);

        assert_eq!(config.ctrl_reg4_g.fs(), GyroScale::Dps2000);
        assert_eq!(config.ctrl_reg2_xm.afs(), AccelScale::G8);
        assert_eq!(config.ctrl_reg6_xm.mfs(), MagScale::Gauss12);
        assert_eq!(config.ctrl_reg4_g.bdu(), BlockDataUpdate::WaitForRead);
        assert_eq!(config.ctrl_reg1_xm.bdu(), BlockDataUpdate::WaitForRead);
        assert_eq!(config.ctrl_reg5_xm.temp_en(), Enable::Enabled);
    }

    #[test]
    fn test_gyro_motion_threshold() {
        let config = Lsm9ds0Config::new().with_gyro_motion_threshold(1000);

        assert_eq!(config.ctrl_reg3_g.i1_int1(), Enable::Enabled);
        assert_eq!(config.int1_cfg_g.and_or(), InterruptCombination::Or);
        assert_eq!(config.int1_cfg_g.xhie(), Enable::Enabled);
        assert_eq!(config.int1_cfg_g.xlie(), Enable::Enabled);
        assert_eq!(config.gyro_int_ths_x, 1000);
        assert_eq!(config.gyro_int_ths_y, 1000);
        assert_eq!(config.gyro_int_ths_z, 1000);
    }

    #[test]
    fn test_per_axis_gyro_threshold() {
        let config = Lsm9ds0Config::new().with_gyro_int_threshold_xyz(100, 200, 300);

        assert_eq!(config.gyro_int_ths_x, 100);
        assert_eq!(config.gyro_int_ths_y, 200);
        assert_eq!(config.gyro_int_ths_z, 300);
    }

    #[test]
    fn test_threshold_at_max_value() {
        // Test that max valid values work correctly
        let config = Lsm9ds0Config::new()
            .with_gyro_int_threshold(0x7FFF) // Max 15-bit value
            .with_accel_int1_threshold(0x7F); // Max 7-bit value

        assert_eq!(config.gyro_int_ths_x, 0x7FFF);
        assert_eq!(config.int_gen_1_ths, 0x7F);
    }

    #[test]
    #[should_panic(expected = "exceeds 15-bit max")]
    #[cfg(debug_assertions)]
    fn test_gyro_threshold_overflow_panics_in_debug() {
        // In debug builds, exceeding max should panic
        let _ = Lsm9ds0Config::new().with_gyro_int_threshold(0xFFFF);
    }

    #[test]
    #[should_panic(expected = "exceeds 7-bit max")]
    #[cfg(debug_assertions)]
    fn test_accel_threshold_overflow_panics_in_debug() {
        // In debug builds, exceeding max should panic
        let _ = Lsm9ds0Config::new().with_accel_int1_threshold(0xFF);
    }

    #[test]
    fn test_sensitivity_values() {
        let config = Lsm9ds0Config::new()
            .with_gyro_scale(GyroScale::Dps500)
            .with_accel_scale(AccelScale::G4)
            .with_mag_scale(MagScale::Gauss8);

        assert_eq!(config.gyro_sensitivity(), 17.5);
        assert_eq!(config.accel_sensitivity(), 0.122);
        assert_eq!(config.mag_sensitivity(), 0.32);
    }

    #[test]
    fn test_click_detection() {
        let config = Lsm9ds0Config::new().with_click_detection(32);

        assert_eq!(config.click_cfg.xs(), Enable::Enabled);
        assert_eq!(config.click_cfg.ys(), Enable::Enabled);
        assert_eq!(config.click_cfg.zs(), Enable::Enabled);
        assert_eq!(config.click_ths, 32);
    }

    #[test]
    fn test_register_to_u8_conversion() {
        let config = Lsm9ds0Config::new()
            .with_gyro_enabled(true)
            .with_gyro_data_rate(GyroDataRate::Hz380)
            .with_gyro_axes(true, true, true);

        // CTRL_REG1_G should have: DR=10, BW=00, PD=1 (Normal), ZEN=1, YEN=1, XEN=1
        // = 0b10_00_1_1_1_1 = 0x8F
        let reg_value: u8 = config.ctrl_reg1_g.into();
        assert_eq!(reg_value, 0x8F);
    }
}