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
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
use super::{
BusOperation, DelayNs, I2c, RegisterOperation, SensorOperation, SevenBitAddress, SpiDevice,
bisync, i2c, prelude::*, spi,
};
use core::fmt::Debug;
use core::marker::PhantomData;
/// The Iis2dlpc generic driver struct.
#[bisync]
pub struct Iis2dlpc<B, T, S>
where
B: BusOperation,
T: DelayNs,
S: SensorState,
{
/// The bus driver.
pub bus: B,
pub tim: T,
_state: PhantomData<S>,
}
/// Driver errors.
#[derive(Debug)]
#[bisync]
pub enum Error<B> {
Bus(B), // Error at the bus level
WhoAmIError(u8), // Incorrect Iis2dlpc identifier
UnexpectedValue, // Unexpected value read from a register
}
#[bisync]
impl<P, T> Iis2dlpc<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 Iis2dlpc sensor.
///
/// # Returns
///
/// * `Result`
/// * `Self`: Returns an instance of `Iis2dlpc`.
/// * `Err`: Returns an error if the initialization fails.
pub fn new_i2c(i2c: P, address: I2CAddress, tim: T) -> Self {
// Initialize the I2C bus with the Iis2dlpc address
let bus = i2c::I2cBus::new(i2c, address as SevenBitAddress);
Self {
bus,
tim,
_state: PhantomData,
}
}
}
#[bisync]
impl<P, T> Iis2dlpc<spi::SpiBus<P>, T, OnState>
where
P: SpiDevice,
T: DelayNs,
{
/// Constructor method for using the SPI bus.
///
/// # Arguments
///
/// * `spi`: The SPI peripheral.
///
/// # Returns
///
/// * `Result`
/// * `Self`: Returns an instance of `Iis2dlpc`.
/// * `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> Iis2dlpc<B, T, S> {
/// # Arguments
///
/// * `bus`: The bus that implements BusOperation.
/// * `tim`: The timer of the COMPONENT sensor.
///
/// # Returns
///
/// * `Self`: Returns an instance of `Iis2mdc`.
#[inline]
pub fn from_bus(bus: B, tim: T) -> Self {
Self {
bus,
tim,
_state: PhantomData,
}
}
}
#[bisync]
impl<B: BusOperation, T: DelayNs, S: SensorState> SensorOperation for Iis2dlpc<B, T, S> {
type Error = Error<B::Error>;
#[inline]
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)
}
#[inline]
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> Iis2dlpc<B, T, OnState> {
/// Set the accelerometer operating mode.
///
/// This function configures the accelerometer's operating mode by updating the `mode` and `lp_mode` fields in the `CTRL1` register,
/// and the `low_noise` field in the `CTRL6` register.
///
/// ### Arguments
/// - `val`: A [`Mode`] value representing the desired operating mode. This includes settings for:
/// - `mode`: Operating mode.
/// - `lp_mode`: Low-power mode configuration.
/// - `low_noise`: Low-noise mode configuration.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn power_mode_set(&mut self, val: Mode) -> Result<(), Error<B::Error>> {
let mut ctrl1 = Ctrl1::read(self).await?;
ctrl1.set_mode(val.mode());
ctrl1.set_lp_mode(val.lp_mode());
ctrl1.write(self).await?;
let mut ctrl6 = Ctrl6::read(self).await?;
ctrl6.set_low_noise(val.low_noise());
ctrl6.write(self).await
}
/// Get the accelerometer operating mode.
///
/// This function retrieves the current operating mode of the accelerometer by reading the `mode` and `lp_mode` fields from the `CTRL1` register,
/// and the `low_noise` field from the `CTRL6` register.
///
/// ### Returns
/// - `Ok(Mode)`: The current operating mode, represented as a [`Mode`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn power_mode_get(&mut self) -> Result<Mode, Error<B::Error>> {
let ctrl1 = Ctrl1::read(self).await?;
let ctrl6 = Ctrl6::read(self).await?;
Ok(Mode::new(ctrl1.mode(), ctrl1.lp_mode(), ctrl6.low_noise()))
}
/// Set the accelerometer data rate.
///
/// This function configures the accelerometer's data rate by updating the `odr` field in the `CTRL1` register,
/// and the `slp_mode` field in the `CTRL3` register.
///
/// ### Arguments
/// - `val`: A [`Odr`] value representing the desired data rate and sleep mode configuration.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn data_rate_set(&mut self, val: Odr) -> Result<(), Error<B::Error>> {
let mut ctrl1 = Ctrl1::read(self).await?;
ctrl1.set_odr(val.odr());
ctrl1.write(self).await?;
let mut ctrl3 = Ctrl3::read(self).await?;
ctrl3.set_slp_mode(val.slp_mode());
ctrl3.write(self).await
}
/// Get the accelerometer data rate.
///
/// This function retrieves the current data rate of the accelerometer by reading the `odr` field from the `CTRL1` register,
/// and the `slp_mode` field from the `CTRL3` register.
///
/// ### Returns
/// - `Ok(Odr)`: The current data rate, represented as an [`Odr`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn data_rate_get(&mut self) -> Result<Odr, Error<B::Error>> {
let ctrl1 = Ctrl1::read(self).await?;
let ctrl3 = Ctrl3::read(self).await?;
Ok(Odr::new(ctrl1.odr(), ctrl3.slp_mode()))
}
/// Set the block data update (BDU) configuration.
///
/// This function configures the block data update (BDU) setting by updating the `bdu` field in the `CTRL2` register.
/// When BDU is enabled, the output registers are not updated until both the high and low parts are read, ensuring data consistency.
///
/// ### Arguments
/// - `val`: The desired BDU value:
/// - `0`: Continuous update.
/// - `1`: Output registers not updated until MSB and LSB are read.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn block_data_update_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_bdu(val);
ctrl2.write(self).await
}
/// Get the block data update (BDU) configuration.
///
/// This function retrieves the current block data update (BDU) setting from the `CTRL2` register.
///
/// ### Returns
/// - `Ok(u8)`: The current BDU value:
/// - `0`: Continuous update.
/// - `1`: Output registers not updated until MSB and LSB are read.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn block_data_update_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(Ctrl2::read(self).await?.bdu())
}
/// Set the accelerometer full-scale selection.
///
/// This function configures the full-scale range of the accelerometer by updating the `fs` field in the `CTRL6` register.
/// The full-scale range determines the maximum measurable acceleration.
///
/// ### Arguments
/// - `val`: A [`Fs`] value representing the desired full-scale range:
/// - `Fs2g`: ±2g (default).
/// - `Fs4g`: ±4g.
/// - `Fs8g`: ±8g.
/// - `Fs16g`: ±16g.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn full_scale_set(&mut self, val: Fs) -> Result<(), Error<B::Error>> {
let mut ctrl6 = Ctrl6::read(self).await?;
ctrl6.set_fs(val as u8);
ctrl6.write(self).await
}
/// Get the accelerometer full-scale selection.
///
/// This function retrieves the current full-scale range of the accelerometer from the `CTRL6` register.
///
/// ### Returns
/// - `Ok(Fs)`: The current full-scale range as a [`Fs`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn full_scale_get(&mut self) -> Result<Fs, Error<B::Error>> {
Ok(Fs::try_from(Ctrl6::read(self).await?.fs()).unwrap_or_default())
}
/// Get the status register.
///
/// This function retrieves the current status of the device by reading the `STATUS` register.
/// The `STATUS` register provides information about various events, such as data-ready, free-fall detection, and tap detection.
///
/// ### Returns
/// - `Ok(Status)`: The current status as a [`Status`] struct, which represents the union of registers from `STATUS`.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn status_reg_get(&mut self) -> Result<Status, Error<B::Error>> {
Status::read(self).await
}
/// Get the accelerometer new data availability flag.
///
/// This function checks whether new accelerometer data is available by reading the `drdy` field in the `STATUS` register.
///
/// ### Returns
/// - `Ok(u8)`: The value of the `drdy` field:
/// - `0`: No new data available.
/// - `1`: New data is available.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation..
pub async fn flag_data_ready_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(self.status_reg_get().await?.drdy())
}
/// Get all interrupt and status flags of the device.
///
/// This function retrieves the status of all interrupt and status flags by reading the following registers:
/// - `STATUS_DUP`
/// - `WAKE_UP_SRC`
/// - `TAP_SRC`
/// - `SIXD_SRC`
/// - `ALL_INT_SRC`
///
/// ### Returns
/// - `Ok(AllSources)`: A struct containing the values of all the above registers.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn all_sources_get(&mut self) -> Result<AllSources, Error<B::Error>> {
Ok(AllSources {
status_dup: StatusDup::read(self).await?,
wake_up_src: WakeUpSrc::read(self).await?,
tap_src: TapSrc::read(self).await?,
sixd_src: SixdSrc::read(self).await?,
all_int_src: AllIntSrc::read(self).await?,
})
}
/// Set the X-axis user offset correction.
///
/// This function configures the X-axis user offset correction value in the `X_OFS_USR` register.
/// The value's weight depends on the `USR_OFF_W` bit in the `CTRL7` register.
///
/// ### Arguments
/// - `val`: The X-axis user offset correction value to set.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the write operation.
pub async fn usr_offset_x_set(&mut self, val: i8) -> Result<(), Error<B::Error>> {
XOfsUsr::from_bits(val.cast_unsigned()).write(self).await
}
/// Get the X-axis user offset correction.
///
/// This function retrieves the X-axis user offset correction value from the `X_OFS_USR` register.
/// The value's weight depends on the `USR_OFF_W` bit in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(i8)`: The X-axis user offset correction value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn usr_offset_x_get(&mut self) -> Result<i8, Error<B::Error>> {
Ok(XOfsUsr::read(self).await?.x_ofs_usr())
}
/// Set the Y-axis user offset correction.
///
/// This function configures the Y-axis user offset correction value in the `Y_OFS_USR` register.
/// The value's weight depends on the `USR_OFF_W` bit in the `CTRL7` register.
///
/// ### Arguments
/// - `val`: The Y-axis user offset correction value to set.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the write operation.
pub async fn usr_offset_y_set(&mut self, val: i8) -> Result<(), Error<B::Error>> {
YOfsUsr::from_bits(val.cast_unsigned()).write(self).await
}
/// Get the Y-axis user offset correction.
///
/// This function retrieves the Y-axis user offset correction value from the `Y_OFS_USR` register.
/// The value's weight depends on the `USR_OFF_W` bit in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(i8)`: The Y-axis user offset correction value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn usr_offset_y_get(&mut self) -> Result<i8, Error<B::Error>> {
Ok(YOfsUsr::read(self).await?.y_ofs_usr())
}
/// Set the Z-axis user offset correction.
///
/// This function configures the Z-axis user offset correction value in the `Z_OFS_USR` register.
/// The value's weight depends on the `USR_OFF_W` bit in the `CTRL7` register.
///
/// ### Arguments
/// - `val`: The Z-axis user offset correction value to set.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the write operation.
pub async fn usr_offset_z_set(&mut self, val: i8) -> Result<(), Error<B::Error>> {
ZOfsUsr::from_bits(val.cast_unsigned()).write(self).await
}
/// Get the Z-axis user offset correction.
///
/// This function retrieves the Z-axis user offset correction value from the `Z_OFS_USR` register.
/// The value's weight depends on the `USR_OFF_W` bit in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(i8)`: The Z-axis user offset correction value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn usr_offset_z_get(&mut self) -> Result<i8, Error<B::Error>> {
Ok(ZOfsUsr::read(self).await?.z_ofs_usr())
}
/// Set the weight of XL user offset bits.
///
/// This function configures the weight of the user offset bits in the `X_OFS_USR`, `Y_OFS_USR`, and `Z_OFS_USR` registers by updating the `usr_off_w` field in the `CTRL7` register.
///
/// ### Arguments
/// - `val`: A [`UsrOffW`] value representing the desired weight:
/// - `Lsb977ug`: 977 μg/LSB (default).
/// - `Lsb15mg6`: 15.6 mg/LSB.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation..
pub async fn offset_weight_set(&mut self, val: UsrOffW) -> Result<(), Error<B::Error>> {
let mut ctrl7 = Ctrl7::read(self).await?;
ctrl7.set_usr_off_w(val as u8);
ctrl7.write(self).await
}
/// Get the weight of XL user offset bits.
///
/// This function retrieves the weight of the user offset bits from the `usr_off_w` field in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(UsrOffW)`: The current weight of the user offset bits:
/// - `Lsb977ug`: 977 μg/LSB (default).
/// - `Lsb15mg6`: 15.6 mg/LSB.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation..
pub async fn offset_weight_get(&mut self) -> Result<UsrOffW, Error<B::Error>> {
Ok(UsrOffW::try_from(Ctrl7::read(self).await?.usr_off_w()).unwrap_or_default())
}
/// Get the raw temperature data.
///
/// This function retrieves the raw temperature data from the `OUT_T_L` and `OUT_T_H` registers.
/// The value is expressed as a 16-bit word in two's complement format.
///
/// ### Returns
/// - `Ok(i16)`: The raw temperature data.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn temperature_raw_get(&mut self) -> Result<i16, Error<B::Error>> {
Ok(OutT::read(self).await?.temp())
}
/// Get the raw acceleration data.
///
/// This function retrieves the raw acceleration data for the X, Y, and Z axes from the `OUT_X_L`, `OUT_X_H`, `OUT_Y_L`, `OUT_Y_H`, `OUT_Z_L`, and `OUT_Z_H` registers.
/// The values are expressed as 16-bit words in two's complement format.
///
/// ### Returns
/// - `Ok([i16; 3])`: An array containing the raw acceleration data for the X, Y, and Z axes.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn acceleration_raw_get(&mut self) -> Result<[i16; 3], Error<B::Error>> {
Ok([
OutX::read(self).await?.x(),
OutY::read(self).await?.y(),
OutZ::read(self).await?.z(),
])
}
/// Get the device ID.
///
/// This function retrieves the device ID from the `WHO_AM_I` register.
/// The device ID is a fixed value that identifies the IIS2DLPC sensor.
///
/// ### Returns
/// - `Ok(u8)`: The device ID (expected value: `0x44`).
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read 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])
}
/// Enable or disable automatic register address increment.
///
/// This function configures the automatic register address increment feature by updating the `if_add_inc` field in the `CTRL2` register.
/// When enabled, the register address is automatically incremented during multiple-byte access.
///
/// ### Arguments
/// - `val`: The desired value for the `if_add_inc` field:
/// - `0`: Disable automatic increment.
/// - `1`: Enable automatic increment.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn auto_increment_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_if_add_inc(val);
ctrl2.write(self).await
}
/// Get the automatic register address increment configuration.
///
/// This function retrieves the current value of the `if_add_inc` field from the `CTRL2` register.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `if_add_inc` field:
/// - `0`: Automatic increment is disabled.
/// - `1`: Automatic increment is enabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn auto_increment_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(Ctrl2::read(self).await?.if_add_inc())
}
/// Perform a software reset.
///
/// This function performs a software reset by updating the `soft_reset` field in the `CTRL2` register.
/// A software reset restores the default values in all user registers.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn reset_set(&mut self) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_soft_reset(PROPERTY_ENABLE);
ctrl2.write(self).await
}
/// Get the software reset status.
///
/// This function retrieves the current value of the `soft_reset` field from the `CTRL2` register.
/// The value indicates whether a software reset has been performed.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `soft_reset` field:
/// - `0`: No reset in progress.
/// - `1`: Reset in progress.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn reset_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(Ctrl2::read(self).await?.soft_reset())
}
/// Reboot memory content and reload calibration parameters.
///
/// This function triggers a reboot of the device's memory content by updating the `boot` field in the `CTRL2` register.
/// The reboot operation reloads the calibration parameters from non-volatile memory.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn boot_set(&mut self) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_boot(PROPERTY_ENABLE);
ctrl2.write(self).await
}
/// Get the reboot memory content status.
///
/// This function retrieves the current value of the `boot` field from the `CTRL2` register.
/// The value indicates whether a reboot operation is in progress.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `boot` field:
/// - `0`: No reboot in progress.
/// - `1`: Reboot in progress.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn boot_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(Ctrl2::read(self).await?.boot())
}
/// Enable or disable the sensor self-test.
///
/// This function configures the self-test mode of the sensor by updating the `st` field in the `CTRL3` register.
/// The self-test mode allows verifying the functionality of the sensor without external stimuli.
///
/// ### Arguments
/// - `val`: A [`St`] value representing the desired self-test mode:
/// - `XlStDisable`: Self-test disabled (default).
/// - `XlStPositive`: Positive sign self-test.
/// - `XlStNegative`: Negative sign self-test.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn self_test_set(&mut self, val: St) -> Result<(), Error<B::Error>> {
let mut ctrl3 = Ctrl3::read(self).await?;
ctrl3.set_st(val as u8);
ctrl3.write(self).await
}
/// Get the sensor self-test mode.
///
/// This function retrieves the current self-test mode of the sensor from the `st` field in the `CTRL3` register.
///
/// ### Returns
/// - `Ok(St)`: The current self-test mode as a [`St`] value:
/// - `XlStDisable`: Self-test disabled (default).
/// - `XlStPositive`: Positive sign self-test.
/// - `XlStNegative`: Negative sign self-test.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn self_test_get(&mut self) -> Result<St, Error<B::Error>> {
Ok(St::try_from(Ctrl3::read(self).await?.st()).unwrap_or_default())
}
/// Set the data-ready interrupt mode.
///
/// This function configures the data-ready interrupt mode by updating the `drdy_pulsed` field in the `CTRL7` register.
/// The data-ready interrupt can be configured as either latched or pulsed mode.
///
/// ### Arguments
/// - `val`: A [`DrdyPulsed`] value representing the desired data-ready interrupt mode:
/// - `Latched`: Latched mode (default).
/// - `Pulsed`: Pulsed mode.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn data_ready_mode_set(&mut self, val: DrdyPulsed) -> Result<(), Error<B::Error>> {
let mut ctrl7 = Ctrl7::read(self).await?;
ctrl7.set_drdy_pulsed(val as u8);
ctrl7.write(self).await
}
/// Get the data-ready interrupt mode.
///
/// This function retrieves the current data-ready interrupt mode from the `drdy_pulsed` field in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(DrdyPulsed)`: The current data-ready interrupt mode as a [`DrdyPulsed`] value:
/// - `Latched`: Latched mode (default).
/// - `Pulsed`: Pulsed mode.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn data_ready_mode_get(&mut self) -> Result<DrdyPulsed, Error<B::Error>> {
Ok(DrdyPulsed::try_from(Ctrl7::read(self).await?.drdy_pulsed()).unwrap_or_default())
}
/// Set the accelerometer filtering path for outputs.
///
/// This function configures the filtering path for accelerometer outputs by updating the `fds` field in the `CTRL6` register
/// and the `usr_off_on_out` field in the `CTRL7` register.
///
/// ### Arguments
/// - `val`: A [`Fds`] value representing the desired filtering path:
/// - `LpfOnOut`: Low-pass filter on output (default).
/// - `UserOffsetOnOut`: User offset on output.
/// - `HighPassOnOut`: High-pass filter on output.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn filter_path_set(&mut self, val: Fds) -> Result<(), Error<B::Error>> {
let mut ctrl6 = Ctrl6::read(self).await?;
ctrl6.set_fds(val.fds());
ctrl6.write(self).await?;
let mut ctrl7 = Ctrl7::read(self).await?;
ctrl7.set_usr_off_on_out(val.usr_off_on_out());
ctrl7.write(self).await
}
/// Get the accelerometer filtering path for outputs.
///
/// This function retrieves the current filtering path for accelerometer outputs by reading the `fds` field from the `CTRL6` register
/// and the `usr_off_on_out` field from the `CTRL7` register.
///
/// ### Returns
/// - `Ok(Fds)`: The current filtering path as a [`Fds`] value:
/// - `LpfOnOut`: Low-pass filter on output (default).
/// - `UserOffsetOnOut`: User offset on output.
/// - `HighPassOnOut`: High-pass filter on output.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn filter_path_get(&mut self) -> Result<Fds, Error<B::Error>> {
let ctrl6 = Ctrl6::read(self).await?;
let ctrl7 = Ctrl7::read(self).await?;
Ok(Fds::new(ctrl6.fds(), ctrl7.usr_off_on_out()))
}
/// Set the accelerometer cutoff filter frequency.
///
/// This function configures the cutoff frequency for the accelerometer's low-pass or high-pass filter by updating the `bw_filt` field in the `CTRL6` register.
///
/// ### Arguments
/// - `val`: A [`BwFilt`] value representing the desired cutoff frequency:
/// - `OdrDiv2`: ODR/2 (default).
/// - `OdrDiv4`: ODR/4.
/// - `OdrDiv10`: ODR/10.
/// - `OdrDiv20`: ODR/20.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn filter_bandwidth_set(&mut self, val: BwFilt) -> Result<(), Error<B::Error>> {
let mut ctrl6 = Ctrl6::read(self).await?;
ctrl6.set_bw_filt(val as u8);
ctrl6.write(self).await
}
/// Get the accelerometer cutoff filter frequency.
///
/// This function retrieves the current cutoff frequency for the accelerometer's low-pass or high-pass filter by reading the `bw_filt` field from the `CTRL6` register.
///
/// ### Returns
/// - `Ok(BwFilt)`: The current cutoff frequency as a [`BwFilt`] value:
/// - `OdrDiv2`: ODR/2 (default).
/// - `OdrDiv4`: ODR/4.
/// - `OdrDiv10`: ODR/10.
/// - `OdrDiv20`: ODR/20.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn filter_bandwidth_get(&mut self) -> Result<BwFilt, Error<B::Error>> {
Ok(BwFilt::try_from(Ctrl6::read(self).await?.bw_filt()).unwrap_or_default())
}
/// Enable or disable the high-pass filter reference mode.
///
/// This function configures the high-pass filter reference mode by updating the `hp_ref_mode` field in the `CTRL7` register.
///
/// ### Arguments
/// - `val`: The desired value for the `hp_ref_mode` field:
/// - `0`: Disable high-pass filter reference mode.
/// - `1`: Enable high-pass filter reference mode.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn reference_mode_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut ctrl7 = Ctrl7::read(self).await?;
ctrl7.set_hp_ref_mode(val);
ctrl7.write(self).await
}
/// Get the high-pass filter reference mode status.
///
/// This function retrieves the current status of the high-pass filter reference mode from the `hp_ref_mode` field in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `hp_ref_mode` field:
/// - `0`: High-pass filter reference mode is disabled.
/// - `1`: High-pass filter reference mode is enabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn reference_mode_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(Ctrl7::read(self).await?.hp_ref_mode())
}
/// Set the SPI serial interface mode.
///
/// This function configures the SPI serial interface mode by updating the `sim` field in the `CTRL2` register.
/// The SPI interface can operate in either 4-wire or 3-wire mode.
///
/// ### Arguments
/// - `val`: A [`Sim`] value representing the desired SPI mode:
/// - `Spi4Wire`: 4-wire SPI mode (default).
/// - `Spi3Wire`: 3-wire SPI mode.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn spi_mode_set(&mut self, val: Sim) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_sim(val as u8);
ctrl2.write(self).await
}
/// Get the SPI serial interface mode.
///
/// This function retrieves the current SPI serial interface mode from the `sim` field in the `CTRL2` register.
///
/// ### Returns
/// - `Ok(Sim)`: The current SPI mode as a [`Sim`] value:
/// - `Spi4Wire`: 4-wire SPI mode (default).
/// - `Spi3Wire`: 3-wire SPI mode.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn spi_mode_get(&mut self) -> Result<Sim, Error<B::Error>> {
Ok(Sim::try_from(Ctrl2::read(self).await?.sim()).unwrap_or_default())
}
/// Enable or disable the I²C interface.
///
/// This function configures the I²C interface by updating the `i2c_disable` field in the `CTRL2` register.
/// The I²C interface can be enabled or disabled based on the provided value.
///
/// ### Arguments
/// - `val`: A [`I2cDisable`] value representing the desired I²C interface state:
/// - `I2cEnable`: Enable the I²C interface (default).
/// - `I2cDisable`: Disable the I²C interface.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn i2c_interface_set(&mut self, val: I2cDisable) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_i2c_disable(val as u8);
ctrl2.write(self).await
}
/// Get the I²C interface state.
///
/// This function retrieves the current state of the I²C interface from the `i2c_disable` field in the `CTRL2` register.
///
/// ### Returns
/// - `Ok(I2cDisable)`: The current I²C interface state as a [`I2cDisable`] value:
/// - `I2cEnable`: I²C interface is enabled (default).
/// - `I2cDisable`: I²C interface is disabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn i2c_interface_get(&mut self) -> Result<I2cDisable, Error<B::Error>> {
Ok(I2cDisable::try_from(Ctrl2::read(self).await?.i2c_disable()).unwrap_or_default())
}
/// Configure the CS pull-up resistor.
///
/// This function configures the CS pull-up resistor by updating the `cs_pu_disc` field in the `CTRL2` register.
/// The pull-up resistor can be connected or disconnected based on the provided value.
///
/// ### Arguments
/// - `val`: A [`CsPuDisc`] value representing the desired CS pull-up configuration:
/// - `PullUpConnect`: Connect the pull-up resistor (default).
/// - `PullUpDisconnect`: Disconnect the pull-up resistor.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn cs_mode_set(&mut self, val: CsPuDisc) -> Result<(), Error<B::Error>> {
let mut ctrl2 = Ctrl2::read(self).await?;
ctrl2.set_cs_pu_disc(val as u8);
ctrl2.write(self).await
}
/// Get the CS pull-up resistor configuration.
///
/// This function retrieves the current CS pull-up resistor configuration from the `cs_pu_disc` field in the `CTRL2` register.
///
/// ### Returns
/// - `Ok(CsPuDisc)`: The current CS pull-up configuration as a [`CsPuDisc`] value:
/// - `PullUpConnect`: Pull-up resistor is connected (default).
/// - `PullUpDisconnect`: Pull-up resistor is disconnected.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn cs_mode_get(&mut self) -> Result<CsPuDisc, Error<B::Error>> {
Ok(CsPuDisc::try_from(Ctrl2::read(self).await?.cs_pu_disc()).unwrap_or_default())
}
/// Interrupt active-high/low.
///
/// # Arguments
///
/// * `val`: change the values of h_lactive in reg CTRL3.
///
/// # Returns
///
/// * `Result`
/// * `()`
/// * `Err`: Returns an error if the operation fails.
pub async fn pin_polarity_set(&mut self, val: HLactive) -> Result<(), Error<B::Error>> {
let mut ctrl3 = Ctrl3::read(self).await?;
ctrl3.set_h_lactive(val as u8);
ctrl3.write(self).await
}
/// Interrupt active-high/low.
///
/// # Returns
///
/// * `Result`
/// * `HLactive`: Get the values of h_lactive in reg CTRL3.
/// * `Err`: Returns an error if the operation fails.
pub async fn pin_polarity_get(&mut self) -> Result<HLactive, Error<B::Error>> {
Ok(HLactive::try_from(Ctrl3::read(self).await?.h_lactive()).unwrap_or_default())
}
/// Latched/pulsed interrupt.
///
/// # Arguments
///
/// * `val`: change the values of lir in reg CTRL3.
///
/// # Returns
///
/// * `Result`
/// * `()`
/// * `Err`: Returns an error if the operation fails.
pub async fn int_notification_set(&mut self, val: Lir) -> Result<(), Error<B::Error>> {
let mut ctrl3 = Ctrl3::read(self).await?;
ctrl3.set_lir(val as u8);
ctrl3.write(self).await
}
/// Latched/pulsed interrupt.
///
/// # Arguments
///
/// * `val`: Get the values of lir in reg CTRL3.
///
/// # Returns
///
/// * `Result`
/// * `()`
pub async fn int_notification_get(&mut self) -> Result<Lir, Error<B::Error>> {
Ok(Lir::try_from(Ctrl3::read(self).await?.lir()).unwrap_or_default())
}
/// Push-pull/open drain selection on interrupt pads.
///
/// # Arguments
///
/// * `val`: change the values of pp_od in reg CTRL3.
///
/// # Returns
///
/// * `Result`
/// * `()`
/// * `Err`: Returns an error if the operation fails.
pub async fn pin_mode_set(&mut self, val: PpOd) -> Result<(), Error<B::Error>> {
let mut ctrl3 = Ctrl3::read(self).await?;
ctrl3.set_pp_od(val as u8);
ctrl3.write(self).await
}
/// Push-pull/open drain selection on interrupt pads.
///
/// # Returns
///
/// * `Result`
/// * `PpOd`: Get the values of pp_od in reg CTRL3.
/// * `Err`: Returns an error if the operation fails.
pub async fn pin_mode_get(&mut self) -> Result<PpOd, Error<B::Error>> {
Ok(PpOd::try_from(Ctrl3::read(self).await?.pp_od()).unwrap_or_default())
}
/// Select the signal that need to route on int1 pad.
pub async fn pin_int1_route_set(
&mut self,
val: &Ctrl4Int1PadCtrl,
) -> Result<(), Error<B::Error>> {
let ctrl5 = Ctrl5Int2PadCtrl::read(self).await?;
let mut ctrl7: Ctrl7 = Ctrl7::read(self).await?;
if (ctrl5.int2_sleep_state()
| ctrl5.int2_sleep_chg()
| val.int1_tap()
| val.int1_ff()
| val.int1_wu()
| val.int1_single_tap()
| val.int1_6d())
!= 0
{
ctrl7.set_interrupts_enable(PROPERTY_ENABLE);
} else {
ctrl7.set_interrupts_enable(PROPERTY_DISABLE);
}
val.write(self).await?;
ctrl7.write(self).await
}
/// Select the signal that need to route on int1 pad.
pub async fn pin_int1_route_get(&mut self) -> Result<Ctrl4Int1PadCtrl, Error<B::Error>> {
Ctrl4Int1PadCtrl::read(self).await
}
/// Select the signal that need to route on int2 pad.
pub async fn pin_int2_route_set(
&mut self,
val: &Ctrl5Int2PadCtrl,
) -> Result<(), Error<B::Error>> {
let ctrl4 = Ctrl4Int1PadCtrl::read(self).await?;
let mut ctrl7 = Ctrl7::read(self).await?;
if (val.int2_sleep_state()
| val.int2_sleep_chg()
| ctrl4.int1_tap()
| ctrl4.int1_ff()
| ctrl4.int1_wu()
| ctrl4.int1_single_tap()
| ctrl4.int1_6d())
!= 0
{
ctrl7.set_interrupts_enable(PROPERTY_ENABLE);
} else {
ctrl7.set_interrupts_enable(PROPERTY_DISABLE);
}
val.write(self).await?;
ctrl7.write(self).await
}
/// Select the signal that need to route on int2 pad.
///
/// # Returns
///
/// * `Result`
/// * `Ctrl5Int2PadCtrl`: register CTRL5_INT2_PAD_CTRL.
/// * `Err`: Returns an error if the operation fails.
pub async fn pin_int2_route_get(&mut self) -> Result<Ctrl5Int2PadCtrl, Error<B::Error>> {
Ctrl5Int2PadCtrl::read(self).await
}
/// All interrupt signals become available on INT1 pin.
///
/// # Arguments
///
/// * `val`: Change the values of int2_on_int1 in reg CTRL_REG7.
///
/// # Returns
///
/// * `Result`
/// * `()`
/// * `Err`: Returns an error if the operation fails.
pub async fn all_on_int1_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = Ctrl7::read(self).await?;
reg.set_int2_on_int1(val);
reg.write(self).await
}
/// All interrupt signals become available on INT1 pin.
///
/// # Returns
///
/// * `Result`
/// * `u8`: change the values of int2_on_int1 in reg CTRL_REG7.
/// * `Err`: Returns an error if the operation fails.
pub async fn all_on_int1_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(Ctrl7::read(self).await?.int2_on_int1())
}
/// Set the wake-up threshold.
///
/// This function configures the wake-up threshold by updating the `wk_ths` field in the `WAKE_UP_THS` register.
/// The threshold is expressed in LSB, where 1 LSB = FS_XL / 64.
///
/// ### Arguments
/// - `val`: The desired wake-up threshold value.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn wkup_threshold_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = WakeUpThs::read(self).await?;
reg.set_wk_ths(val);
reg.write(self).await
}
/// Get the wake-up threshold.
///
/// This function retrieves the current wake-up threshold from the `wk_ths` field in the `WAKE_UP_THS` register.
/// The threshold is expressed in LSB, where 1 LSB = FS_XL / 64.
///
/// ### Returns
/// - `Ok(u8)`: The current wake-up threshold value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn wkup_threshold_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(WakeUpThs::read(self).await?.wk_ths())
}
/// Set the wake-up duration event.
///
/// This function configures the wake-up duration by updating the `wake_dur` field in the `WAKE_UP_DUR` register.
/// The duration is expressed in LSB, where 1 LSB = 1 / ODR.
///
/// ### Arguments
/// - `val`: The desired wake-up duration value.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn wkup_dur_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = WakeUpDur::read(self).await?;
reg.set_wake_dur(val);
reg.write(self).await
}
/// Get the wake-up duration event.
///
/// This function retrieves the current wake-up duration from the `wake_dur` field in the `WAKE_UP_DUR` register.
/// The duration is expressed in LSB, where 1 LSB = 1 / ODR.
///
/// ### Returns
/// - `Ok(u8)`: The current wake-up duration value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn wkup_dur_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(WakeUpDur::read(self).await?.wake_dur())
}
/// Set the data sent to the wake-up interrupt function.
///
/// This function configures the data source for the wake-up interrupt function by updating the `usr_off_on_wu` field in the `CTRL7` register.
/// The data source can be either high-pass filtered data or user offset data.
///
/// ### Arguments
/// - `val`: A [`UsrOffOnWu`] value representing the desired data source:
/// - `HpFeed`: High-pass filtered data (default).
/// - `UserOffsetFeed`: User offset data.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn wkup_feed_data_set(&mut self, val: UsrOffOnWu) -> Result<(), Error<B::Error>> {
let mut reg = Ctrl7::read(self).await?;
reg.set_usr_off_on_wu(val as u8);
reg.write(self).await
}
/// Get the data sent to the wake-up interrupt function.
///
/// This function retrieves the current data source for the wake-up interrupt function from the `usr_off_on_wu` field in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(UsrOffOnWu)`: The current data source as a [`UsrOffOnWu`] value:
/// - `HpFeed`: High-pass filtered data (default).
/// - `UserOffsetFeed`: User offset data.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn wkup_feed_data_get(&mut self) -> Result<UsrOffOnWu, Error<B::Error>> {
Ok(UsrOffOnWu::try_from(Ctrl7::read(self).await?.usr_off_on_wu()).unwrap_or_default())
}
/// Configure activity/inactivity or stationary/motion detection.
///
/// This function configures the activity/inactivity or stationary/motion detection by updating the `sleep_on` field in the `WAKE_UP_THS` register
/// and the `stationary` field in the `WAKE_UP_DUR` register.
///
/// ### Arguments
/// - `val`: A [`SleepOn`] value representing the desired detection mode:
/// - `NoDetection`: No detection (default).
/// - `DetectActInact`: Detect activity/inactivity.
/// - `DetectStatMotion`: Detect stationary/motion.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn act_mode_set(&mut self, val: SleepOn) -> Result<(), Error<B::Error>> {
let mut wake_up_ths = WakeUpThs::read(self).await?;
let mut wake_up_dur: WakeUpDur = WakeUpDur::read(self).await?;
wake_up_ths.set_sleep_on(val.sleep_on());
wake_up_dur.set_stationary(val.stationary());
wake_up_ths.write(self).await?;
wake_up_dur.write(self).await
}
/// Get the activity/inactivity or stationary/motion detection configuration.
///
/// This function retrieves the current detection mode by reading the `sleep_on` field from the `WAKE_UP_THS` register
/// and the `stationary` field from the `WAKE_UP_DUR` register.
///
/// ### Returns
/// - `Ok(SleepOn)`: The current detection mode as a [`SleepOn`] value:
/// - `NoDetection`: No detection (default).
/// - `DetectActInact`: Detect activity/inactivity.
/// - `DetectStatMotion`: Detect stationary/motion.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn act_mode_get(&mut self) -> Result<SleepOn, Error<B::Error>> {
let wake_up_ths = WakeUpThs::read(self).await?;
let wake_up_dur: WakeUpDur = WakeUpDur::read(self).await?;
Ok(SleepOn::new(
wake_up_ths.sleep_on(),
wake_up_dur.stationary(),
))
}
/// Set the duration to enter sleep mode.
///
/// This function configures the duration required to enter sleep mode by updating the `sleep_dur` field in the `WAKE_UP_DUR` register.
/// The duration is expressed in LSB, where 1 LSB = 512 / ODR.
///
/// ### Arguments
/// - `val`: The desired sleep duration value.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn act_sleep_dur_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = WakeUpDur::read(self).await?;
reg.set_sleep_dur(val);
reg.write(self).await
}
/// Get the duration to enter sleep mode.
///
/// This function retrieves the current sleep duration from the `sleep_dur` field in the `WAKE_UP_DUR` register.
/// The duration is expressed in LSB, where 1 LSB = 512 / ODR.
///
/// ### Returns
/// - `Ok(u8)`: The current sleep duration value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn act_sleep_dur_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(WakeUpDur::read(self).await?.sleep_dur())
}
/// Set the threshold for tap recognition on the X-axis.
///
/// This function configures the tap threshold for the X-axis by updating the `tap_thsx` field in the `TAP_THS_X` register.
///
/// ### Arguments
/// - `val`: The desired tap threshold value for the X-axis.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_threshold_x_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsX::read(self).await?;
reg.set_tap_thsx(val);
reg.write(self).await
}
/// Get the threshold for tap recognition on the X-axis.
///
/// This function retrieves the current tap threshold for the X-axis from the `tap_thsx` field in the `TAP_THS_X` register.
///
/// ### Returns
/// - `Ok(u8)`: The current tap threshold value for the X-axis.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_threshold_x_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsX::read(self).await?.tap_thsx())
}
/// Set the threshold for tap recognition on the Y-axis.
///
/// This function configures the tap threshold for the Y-axis by updating the `tap_thsy` field in the `TAP_THS_Y` register.
///
/// ### Arguments
/// - `val`: The desired tap threshold value for the Y-axis.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_threshold_y_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsY::read(self).await?;
reg.set_tap_thsy(val);
reg.write(self).await
}
/// Get the threshold for tap recognition on the Y-axis.
///
/// This function retrieves the current tap threshold for the Y-axis from the `tap_thsy` field in the `TAP_THS_Y` register.
///
/// ### Returns
/// - `Ok(u8)`: The current tap threshold value for the Y-axis.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_threshold_y_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsY::read(self).await?.tap_thsy())
}
/// Set the axis priority for tap detection.
///
/// This function configures the axis priority for tap detection by updating the `tap_prior` field in the `TAP_THS_Y` register.
///
/// ### Arguments
/// - `val`: A [`TapPrior`] value representing the desired axis priority:
/// - `Xyz`: X > Y > Z (default).
/// - `Yxz`: Y > X > Z.
/// - `Xzy`: X > Z > Y.
/// - `Zyx`: Z > Y > X.
/// - `Yzx`: Y > Z > X.
/// - `Zxy`: Z > X > Y.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_axis_priority_set(&mut self, val: TapPrior) -> Result<(), Error<B::Error>> {
let mut reg = TapThsY::read(self).await?;
reg.set_tap_prior(val as u8);
reg.write(self).await
}
/// Get the axis priority for tap detection.
///
/// This function retrieves the current axis priority for tap detection from the `tap_prior` field in the `TAP_THS_Y` register.
///
/// ### Returns
/// - `Ok(TapPrior)`: The current axis priority as a [`TapPrior`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_axis_priority_get(&mut self) -> Result<TapPrior, Error<B::Error>> {
Ok(TapPrior::try_from(TapThsY::read(self).await?.tap_prior()).unwrap_or_default())
}
/// Set the threshold for tap recognition on the Z-axis.
///
/// This function configures the tap threshold for the Z-axis by updating the `tap_thsz` field in the `TAP_THS_Z` register.
///
/// ### Arguments
/// - `val`: The desired tap threshold value for the Z-axis.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_threshold_z_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsZ::read(self).await?;
reg.set_tap_thsz(val);
reg.write(self).await
}
/// Get the threshold for tap recognition on the Z-axis.
///
/// This function retrieves the current tap threshold for the Z-axis from the `tap_thsz` field in the `TAP_THS_Z` register.
///
/// ### Returns
/// - `Ok(u8)`: The current tap threshold value for the Z-axis.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_threshold_z_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsZ::read(self).await?.tap_thsz())
}
/// Enable Z direction in tap recognition.
///
/// This function enables or disables tap recognition on the Z-axis by updating the `tap_z_en` field in the `TAP_THS_Z` register.
///
/// ### Arguments
/// - `val`: The desired value for the `tap_z_en` field:
/// - `0`: Disable Z-axis tap recognition.
/// - `1`: Enable Z-axis tap recognition.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_detection_on_z_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsZ::read(self).await?;
reg.set_tap_z_en(val);
reg.write(self).await
}
/// Get the Z direction tap recognition status.
///
/// This function retrieves the current status of tap recognition on the Z-axis from the `tap_z_en` field in the `TAP_THS_Z` register.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `tap_z_en` field:
/// - `0`: Z-axis tap recognition is disabled.
/// - `1`: Z-axis tap recognition is enabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_detection_on_z_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsZ::read(self).await?.tap_z_en())
}
/// Enable Y direction in tap recognition.
///
/// This function enables or disables tap recognition on the Y-axis by updating the `tap_y_en` field in the `TAP_THS_Z` register.
///
/// ### Arguments
/// - `val`: The desired value for the `tap_y_en` field:
/// - `0`: Disable Y-axis tap recognition.
/// - `1`: Enable Y-axis tap recognition.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_detection_on_y_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsZ::read(self).await?;
reg.set_tap_y_en(val);
reg.write(self).await
}
/// Get the Y direction tap recognition status.
///
/// This function retrieves the current status of tap recognition on the Y-axis from the `tap_y_en` field in the `TAP_THS_Z` register.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `tap_y_en` field:
/// - `0`: Y-axis tap recognition is disabled.
/// - `1`: Y-axis tap recognition is enabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_detection_on_y_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsZ::read(self).await?.tap_y_en())
}
/// Enable X direction in tap recognition.
///
/// This function enables or disables tap recognition on the X-axis by updating the `tap_x_en` field in the `TAP_THS_Z` register.
///
/// ### Arguments
/// - `val`: The desired value for the `tap_x_en` field:
/// - `0`: Disable X-axis tap recognition.
/// - `1`: Enable X-axis tap recognition.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_detection_on_x_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsZ::read(self).await?;
reg.set_tap_x_en(val);
reg.write(self).await
}
/// Get the X direction tap recognition status.
///
/// This function retrieves the current status of tap recognition on the X-axis from the `tap_x_en` field in the `TAP_THS_Z` register.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `tap_x_en` field:
/// - `0`: X-axis tap recognition is disabled.
/// - `1`: X-axis tap recognition is enabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_detection_on_x_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsZ::read(self).await?.tap_x_en())
}
/// Set the maximum duration for tap recognition.
///
/// This function configures the maximum time an over-threshold signal is detected to be recognized as a tap event.
/// The duration is set in the `shock` field of the `INT_DUR` register.
/// - The default value (`00b`) corresponds to `4 * ODR_XL` time.
/// - If the `shock` bits are set to a different value, 1 LSB corresponds to `8 * ODR_XL` time.
///
/// ### Arguments
/// - `val`: The desired maximum duration value for tap recognition.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_shock_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = IntDur::read(self).await?;
reg.set_shock(val);
reg.write(self).await
}
/// Get the maximum duration for tap recognition.
///
/// This function retrieves the current maximum time an over-threshold signal is detected to be recognized as a tap event.
/// The duration is stored in the `shock` field of the `INT_DUR` register.
///
/// ### Returns
/// - `Ok(u8)`: The current maximum duration value for tap recognition.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_shock_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(IntDur::read(self).await?.shock())
}
/// Set the quiet time for tap recognition.
///
/// This function configures the quiet time after the first detected tap during which no over-threshold event should occur.
/// The quiet time is set in the `quiet` field of the `INT_DUR` register.
/// - The default value (`00b`) corresponds to `2 * ODR_XL` time.
/// - If the `quiet` bits are set to a different value, 1 LSB corresponds to `4 * ODR_XL` time.
///
/// ### Arguments
/// - `val`: The desired quiet time value.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_quiet_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = IntDur::read(self).await?;
reg.set_quiet(val);
reg.write(self).await
}
/// Get the quiet time for tap recognition.
///
/// This function retrieves the current quiet time after the first detected tap during which no over-threshold event should occur.
/// The quiet time is stored in the `quiet` field of the `INT_DUR` register.
///
/// ### Returns
/// - `Ok(u8)`: The current quiet time value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_quiet_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(IntDur::read(self).await?.quiet())
}
/// Set the maximum duration for double-tap recognition.
///
/// This function configures the maximum time between two consecutive detected taps to determine a double-tap event.
/// The duration is set in the `latency` field of the `INT_DUR` register.
/// - The default value (`0000b`) corresponds to `16 * ODR_XL` time.
/// - If the `latency` bits are set to a different value, 1 LSB corresponds to `32 * ODR_XL` time.
///
/// ### Arguments
/// - `val`: The desired maximum duration value for double-tap recognition.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_dur_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = IntDur::read(self).await?;
reg.set_latency(val);
reg.write(self).await
}
/// Get the maximum duration for double-tap recognition.
///
/// This function retrieves the current maximum time between two consecutive detected taps to determine a double-tap event.
/// The duration is stored in the `latency` field of the `INT_DUR` register.
///
/// ### Returns
/// - `Ok(u8)`: The current maximum duration value for double-tap recognition.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_dur_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(IntDur::read(self).await?.latency())
}
/// Enable or disable single/double-tap event detection.
///
/// This function configures the single/double-tap event detection by updating the `single_double_tap` field in the `WAKE_UP_THS` register.
/// The mode determines whether only single-tap events or both single- and double-tap events are detected.
///
/// ### Arguments
/// - `val`: A [`SingleDoubleTap`] value representing the desired tap mode:
/// - `OnlySingle`: Detect only single-tap events (default).
/// - `BothSingleDouble`: Detect both single- and double-tap events.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn tap_mode_set(&mut self, val: SingleDoubleTap) -> Result<(), Error<B::Error>> {
let mut reg = WakeUpThs::read(self).await?;
reg.set_single_double_tap(val as u8);
reg.write(self).await
}
/// Get the single/double-tap event detection mode.
///
/// This function retrieves the current single/double-tap event detection mode from the `single_double_tap` field in the `WAKE_UP_THS` register.
///
/// ### Returns
/// - `Ok(SingleDoubleTap)`: The current tap mode as a [`SingleDoubleTap`] value:
/// - `OnlySingle`: Detect only single-tap events (default).
/// - `BothSingleDouble`: Detect both single- and double-tap events.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_mode_get(&mut self) -> Result<SingleDoubleTap, Error<B::Error>> {
Ok(
SingleDoubleTap::try_from(WakeUpThs::read(self).await?.single_double_tap())
.unwrap_or_default(),
)
}
/// Read the tap/double-tap source register.
///
/// This function retrieves the tap/double-tap source information from the `TAP_SRC` register.
/// The `TAP_SRC` register provides details about the tap events, such as the axis of detection and the type of tap event.
///
/// ### Returns
/// - `Ok(TapSrc)`: The tap source information as a [`TapSrc`] struct.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn tap_src_get(&mut self) -> Result<TapSrc, Error<B::Error>> {
TapSrc::read(self).await
}
/// Set the threshold for 4D/6D orientation detection.
///
/// This function configures the threshold for 4D/6D orientation detection by updating the `6d_ths` field in the `TAP_THS_X` register.
///
/// ### Arguments
/// - `val`: The desired threshold value for 4D/6D orientation detection.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn sixd_threshold_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsX::read(self).await?;
reg.set_six_d_ths(val);
reg.write(self).await
}
/// Get the threshold for 4D/6D orientation detection.
///
/// This function retrieves the current threshold for 4D/6D orientation detection from the `6d_ths` field in the `TAP_THS_X` register.
///
/// ### Returns
/// - `Ok(u8)`: The current threshold value for 4D/6D orientation detection.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn sixd_threshold_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsX::read(self).await?.six_d_ths())
}
/// Enable or disable 4D orientation detection.
///
/// This function configures the 4D orientation detection by updating the `4d_en` field in the `TAP_THS_X` register.
///
/// ### Arguments
/// - `val`: The desired value for the `4d_en` field:
/// - `0`: Disable 4D orientation detection.
/// - `1`: Enable 4D orientation detection.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn fourd_mode_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = TapThsX::read(self).await?;
reg.set_four_d_en(val);
reg.write(self).await
}
/// Get the 4D orientation detection status.
///
/// This function retrieves the current status of 4D orientation detection from the `4d_en` field in the `TAP_THS_X` register.
///
/// ### Returns
/// - `Ok(u8)`: The current value of the `4d_en` field:
/// - `0`: 4D orientation detection is disabled.
/// - `1`: 4D orientation detection is enabled.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn fourd_mode_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(TapThsX::read(self).await?.four_d_en())
}
/// Read the 6D tap source register.
///
/// This function retrieves the 6D tap source information from the `SIXD_SRC` register.
/// The `SIXD_SRC` register provides details about the 6D orientation events, such as axis-specific thresholds and event detection.
///
/// ### Returns
/// - `Ok(SixdSrc)`: The 6D source information as a [`SixdSrc`] struct.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn sixd_src_get(&mut self) -> Result<SixdSrc, Error<B::Error>> {
SixdSrc::read(self).await
}
/// Set the data source for the 6D interrupt function.
///
/// This function configures the data source for the 6D interrupt function by updating the `lpass_on6d` field in the `CTRL7` register.
/// The data source can be either ODR/2 low-pass filtered data or LPF2 output data.
///
/// ### Arguments
/// - `val`: A [`LpassOn6d`] value representing the desired data source:
/// - `OdrDiv2Feed`: ODR/2 low-pass filtered data (default).
/// - `Lpf2Feed`: LPF2 output data.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn sixd_feed_data_set(&mut self, val: LpassOn6d) -> Result<(), Error<B::Error>> {
let mut reg = Ctrl7::read(self).await?;
reg.set_lpass_on6d(val as u8);
reg.write(self).await
}
/// Get the data source for the 6D interrupt function.
///
/// This function retrieves the current data source for the 6D interrupt function from the `lpass_on6d` field in the `CTRL7` register.
///
/// ### Returns
/// - `Ok(LpassOn6d)`: The current data source as a [`LpassOn6d`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn sixd_feed_data_get(&mut self) -> Result<LpassOn6d, Error<B::Error>> {
Ok(LpassOn6d::try_from(Ctrl7::read(self).await?.lpass_on6d()).unwrap_or_default())
}
/// Set the wake-up duration event.
///
/// This function configures the wake-up duration event by updating the `ff_dur` field in the `WAKE_UP_DUR` and `FREE_FALL` registers.
/// The duration is expressed in LSB, where 1 LSB = 1 / ODR.
///
/// ### Arguments
/// - `val`: The desired wake-up duration value.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn ff_dur_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut wake_up_dur = WakeUpDur::read(self).await?;
let mut free_fall = FreeFall::read(self).await?;
wake_up_dur.set_ff_dur((val & 0x20) >> 5);
free_fall.set_ff_dur(val & 0x1F);
wake_up_dur.write(self).await?;
free_fall.write(self).await
}
/// Get the wake-up duration event.
///
/// This function retrieves the current wake-up duration event from the `ff_dur` field in the `WAKE_UP_DUR` and `FREE_FALL` registers.
/// The duration is expressed in LSB, where 1 LSB = 1 / ODR.
///
/// ### Returns
/// - `Ok(u8)`: The current wake-up duration value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn ff_dur_get(&mut self) -> Result<u8, Error<B::Error>> {
let wake_up_dur = WakeUpDur::read(self).await?;
let free_fall = FreeFall::read(self).await?;
Ok((wake_up_dur.ff_dur() << 5) + free_fall.ff_dur())
}
/// Set the free-fall threshold.
///
/// This function configures the free-fall threshold by updating the `ff_ths` field in the `FREE_FALL` register.
/// The threshold determines the sensitivity of the free-fall detection.
///
/// ### Arguments
/// - `val`: A [`FfThs`] value representing the desired free-fall threshold:
/// - `FfTsh5lsbFs2g`: 5 LSB @ ±2g (default).
/// - `FfTsh7lsbFs2g`: 7 LSB @ ±2g.
/// - `FfTsh8lsbFs2g`: 8 LSB @ ±2g.
/// - `FfTsh10lsbFs2g`: 10 LSB @ ±2g.
/// - `FfTsh11lsbFs2g`: 11 LSB @ ±2g.
/// - `FfTsh13lsbFs2g`: 13 LSB @ ±2g.
/// - `FfTsh15lsbFs2g`: 15 LSB @ ±2g.
/// - `FfTsh16lsbFs2g`: 16 LSB @ ±2g.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn ff_threshold_set(&mut self, val: FfThs) -> Result<(), Error<B::Error>> {
let mut reg = FreeFall::read(self).await?;
reg.set_ff_ths(val as u8);
reg.write(self).await
}
/// Get the free-fall threshold.
///
/// This function retrieves the current free-fall threshold from the `ff_ths` field in the `FREE_FALL` register.
///
/// ### Returns
/// - `Ok(FfThs)`: The current free-fall threshold as a [`FfThs`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn ff_threshold_get(&mut self) -> Result<FfThs, Error<B::Error>> {
Ok(FfThs::try_from(FreeFall::read(self).await?.ff_ths()).unwrap_or_default())
}
/// Set the FIFO watermark level.
///
/// This function configures the FIFO watermark level by updating the `fth` field in the `FIFO_CTRL` register.
/// The watermark level determines the threshold at which the FIFO generates an interrupt when the number of unread samples reaches the specified level.
///
/// ### Arguments
/// - `val`: The desired FIFO watermark level.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn fifo_watermark_set(&mut self, val: u8) -> Result<(), Error<B::Error>> {
let mut reg = FifoCtrl::read(self).await?;
reg.set_fth(val);
reg.write(self).await
}
/// Get the FIFO watermark level.
///
/// This function retrieves the current FIFO watermark level from the `fth` field in the `FIFO_CTRL` register.
///
/// ### Returns
/// - `Ok(u8)`: The current FIFO watermark level.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn fifo_watermark_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(FifoCtrl::read(self).await?.fth())
}
/// Set the FIFO mode.
///
/// This function configures the FIFO operating mode by updating the `fmode` field in the `FIFO_CTRL` register.
/// The FIFO mode determines how data is managed in the FIFO buffer.
///
/// ### Arguments
/// - `val`: A [`Fmode`] value representing the desired FIFO mode:
/// - `BypassMode`: FIFO is disabled (default).
/// - `FifoMode`: FIFO stops collecting data when full.
/// - `StreamToFifoMode`: Stream mode until a trigger event, then FIFO mode.
/// - `BypassToStreamMode`: Bypass mode until a trigger event, then stream mode.
/// - `StreamMode`: Continuously updates FIFO, overwriting old data when full.
///
/// ### Returns
/// - `Ok(())`: If the operation is successful.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read or write operation.
pub async fn fifo_mode_set(&mut self, val: Fmode) -> Result<(), Error<B::Error>> {
let mut reg = FifoCtrl::read(self).await?;
reg.set_fmode(val as u8);
reg.write(self).await
}
/// Get the FIFO mode.
///
/// This function retrieves the current FIFO operating mode from the `fmode` field in the `FIFO_CTRL` register.
///
/// ### Returns
/// - `Ok(Fmode)`: The current FIFO mode as a [`Fmode`] value.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn fifo_mode_get(&mut self) -> Result<Fmode, Error<B::Error>> {
Ok(Fmode::try_from(FifoCtrl::read(self).await?.fmode()).unwrap_or_default())
}
/// Get the number of unread samples stored in the FIFO.
///
/// This function retrieves the number of unread samples currently stored in the FIFO buffer from the `diff` field in the `FIFO_SAMPLES` register.
///
/// ### Returns
/// - `Ok(u8)`: The number of unread samples in the FIFO.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn fifo_data_level_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(FifoSamples::read(self).await?.diff())
}
/// Get the FIFO overrun status.
///
/// This function retrieves the FIFO overrun status from the `fifo_ovr` field in the `FIFO_SAMPLES` register.
/// The overrun status indicates whether the FIFO buffer has overwritten old data due to being full.
///
/// ### Returns
/// - `Ok(u8)`: The current FIFO overrun status:
/// - `0`: No overrun has occurred.
/// - `1`: FIFO has overwritten old data.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn fifo_ovr_flag_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(FifoSamples::read(self).await?.fifo_ovr())
}
/// Get the FIFO threshold status flag.
///
/// This function retrieves the FIFO threshold status flag from the `fifo_fth` field in the `FIFO_SAMPLES` register.
/// The threshold status indicates whether the number of unread samples in the FIFO has reached the configured watermark level.
///
/// ### Returns
/// - `Ok(u8)`: The current FIFO threshold status flag:
/// - `0`: FIFO filling is below the threshold level.
/// - `1`: FIFO filling has reached or exceeded the threshold level.
/// - `Err(Error::Bus)`: If there is an error at the bus level during the read operation.
pub async fn fifo_wtm_flag_get(&mut self) -> Result<u8, Error<B::Error>> {
Ok(FifoSamples::read(self).await?.fifo_fth())
}
}
/// Convert from full-scale ±2g to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±2g.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs2_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 0.244
}
/// Convert from full-scale ±4g to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±4g.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs4_to_mg(lsb: i16) -> f32 {
// (lsb as f32) * 0.122
(lsb as f32) * 0.488
}
/// Convert from full-scale ±8g to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±8g.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs8_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 0.976
}
/// Convert from full-scale ±16g to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±16g.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs16_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 1.952
}
/// Convert from full-scale ±2g (low-power mode 1) to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±2g in low-power mode 1.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs2_lp1_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 0.976
}
/// Convert from full-scale ±4g (low-power mode 1) to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±4g in low-power mode 1.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs4_lp1_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 1.952
}
/// Convert from full-scale ±8g (low-power mode 1) to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±8g in low-power mode 1.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs8_lp1_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 3.904
}
/// Convert from full-scale ±16g (low-power mode 1) to mg.
///
/// This function converts a raw sensor value in least significant bits (LSB) to mg for a full-scale range of ±16g in low-power mode 1.
///
/// ### Arguments
/// - `lsb`: The raw value in LSB.
///
/// ### Returns
/// - `f32`: The converted value in mg.
#[bisync]
pub fn from_fs16_lp1_to_mg(lsb: i16) -> f32 {
(lsb as f32) * 7.808
}
/// Convert from LSB to Celsius.
///
/// This function converts a raw temperature value in least significant bits (LSB) to degrees Celsius (°C).
///
/// ### Arguments
/// - `lsb`: The raw temperature value in LSB.
///
/// ### Returns
/// - `f32`: The temperature in degrees Celsius.
#[bisync]
pub fn from_lsb_to_celsius(lsb: i16) -> f32 {
(lsb as f32 / 16.0) + 25.0
}
/// I²C Address Map.
///
/// This enum represents the possible I²C addresses for the IIS2DLPC sensor, depending on the configuration of the SA0 pin.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq)]
#[bisync]
pub enum I2CAddress {
/// I²C address when SA0 is connected to GND.
I2cAddL = 0x18,
/// I²C address when SA0 is connected to VDD.
I2cAddH = 0x19,
}
/// Device ID for the IIS2DLPC sensor.
///
/// The `WhoAmI` register contains this value to identify the device.
#[bisync]
pub const ID: u8 = 0x44;
#[bisync]
pub const PROPERTY_ENABLE: u8 = 1;
#[bisync]
pub const PROPERTY_DISABLE: u8 = 0;