m5unified 0.3.3

Safe Rust wrapper for M5Unified.
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
//! Power-management, PMIC, sleep, and direct PMIC helpers.
//!
//! The safe wrapper exposes battery and VBUS readings, output controls, sleep
//! timers, vibration, external-port power, and direct PMIC-specific helpers.

use crate::{Date, Time};

#[derive(Debug)]
pub struct Power;

impl Power {
    /// Initialize the power-management backend.
    ///
    /// `M5Unified::begin` calls this as part of normal startup; this helper is
    /// exposed for parity with M5Unified and advanced reinitialization flows.
    pub fn begin(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_power_begin() }
    }

    /// Return the detected power-management IC type.
    pub fn pmic_type(&self) -> PowerType {
        PowerType::from_raw(unsafe { m5unified_sys::m5u_power_get_type() as i32 })
    }

    /// Return the battery level as a percentage when the board reports one.
    pub fn battery_level(&self) -> Option<u8> {
        let level = unsafe { m5unified_sys::m5u_battery_level() };
        if (0..=100).contains(&level) {
            Some(level as u8)
        } else {
            None
        }
    }

    /// Return the battery voltage in millivolts when supported.
    pub fn battery_voltage_mv(&self) -> Option<u16> {
        let mv = unsafe { m5unified_sys::m5u_battery_voltage_mv() };
        (mv >= 0).then_some(mv as u16)
    }

    /// Return VBUS voltage in millivolts when the board supports it.
    pub fn vbus_voltage_mv(&self) -> Option<u16> {
        let mv = unsafe { m5unified_sys::m5u_power_get_vbus_voltage_mv() };
        (mv >= 0).then_some(mv as u16)
    }

    /// Return battery current in milliamps.
    ///
    /// Positive values indicate charge current and negative values indicate
    /// discharge current.
    pub fn battery_current_ma(&self) -> i32 {
        unsafe { m5unified_sys::m5u_power_get_battery_current_ma() as i32 }
    }

    /// Return external-port voltage in millivolts for the selected port mask.
    pub fn ext_voltage_mv(&self, port_mask: ExtPortMask) -> f32 {
        unsafe { m5unified_sys::m5u_power_get_ext_voltage_mv(port_mask.bits()) }
    }

    /// Return external-port current in milliamps for the selected port mask.
    pub fn ext_current_ma(&self, port_mask: ExtPortMask) -> f32 {
        unsafe { m5unified_sys::m5u_power_get_ext_current_ma(port_mask.bits()) }
    }

    /// Return the explicit charging state reported by M5Unified.
    pub fn charge_state(&self) -> ChargeState {
        ChargeState::from_raw(unsafe { m5unified_sys::m5u_power_get_charge_state() as i32 })
    }

    /// Return whether M5Unified reports that the battery is charging.
    pub fn is_charging(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_is_charging() }
    }

    /// Set the board power LED brightness where the board supports it.
    pub fn set_led(&mut self, brightness: u8) {
        unsafe { m5unified_sys::m5u_power_set_led(brightness) }
    }

    /// Set external port output for the selected ports.
    pub fn set_ext_output(&mut self, enable: bool, port_mask: ExtPortMask) {
        unsafe { m5unified_sys::m5u_power_set_ext_output(enable, port_mask.bits()) }
    }

    /// Compatibility alias for M5Unified's deprecated `setExtPower` helper.
    ///
    /// This applies the same default port mask as M5Unified's C++ API.
    pub fn set_ext_power(&mut self, enable: bool) {
        self.set_ext_output(enable, ExtPortMask::ALL)
    }

    /// Return whether external port output is enabled.
    pub fn ext_output(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_get_ext_output() }
    }

    /// Set main USB power output where the board supports it.
    pub fn set_usb_output(&mut self, enable: bool) {
        unsafe { m5unified_sys::m5u_power_set_usb_output(enable) }
    }

    /// Return whether main USB power output is enabled.
    pub fn usb_output(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_get_usb_output() }
    }

    /// Enable or disable battery charging where the PMIC supports it.
    pub fn set_battery_charge(&mut self, enable: bool) {
        unsafe { m5unified_sys::m5u_power_set_battery_charge(enable) }
    }

    /// Set the PMIC charge-current target in milliamps.
    pub fn set_charge_current_ma(&mut self, max_ma: u16) {
        unsafe { m5unified_sys::m5u_power_set_charge_current(max_ma) }
    }

    /// Set the PMIC charge-voltage target in millivolts.
    pub fn set_charge_voltage_mv(&mut self, max_mv: u16) {
        unsafe { m5unified_sys::m5u_power_set_charge_voltage(max_mv) }
    }

    /// Return the PMIC key state.
    ///
    /// M5Unified reports `0` for none, `1` for long press, `2` for short click,
    /// and `3` for both. On supported PMICs this read clears the latched state.
    pub fn key_state(&self) -> u8 {
        unsafe { m5unified_sys::m5u_power_get_key_state() }
    }

    /// Configure external-port bus output where the board supports it.
    pub fn set_ext_port_bus_config(&mut self, config: ExtPortBusConfig) {
        let raw = config.to_raw();
        unsafe { m5unified_sys::m5u_power_set_ext_port_bus_config(&raw) }
    }

    /// Set vibration motor strength where the board supports it.
    pub fn set_vibration(&mut self, level: u8) {
        unsafe { m5unified_sys::m5u_power_set_vibration(level) }
    }

    /// Power the board off through M5Unified.
    pub fn power_off(&mut self) {
        unsafe { m5unified_sys::m5u_power_power_off() }
    }

    /// Enter timer sleep and wake after the given number of seconds.
    pub fn timer_sleep_seconds(&mut self, seconds: i32) {
        unsafe { m5unified_sys::m5u_power_timer_sleep_seconds(seconds) }
    }

    /// Enter timer sleep and wake at the given RTC time.
    ///
    /// As in M5Unified, seconds are ignored by the underlying implementation.
    pub fn timer_sleep_time(&mut self, time: Time) {
        let raw = time.to_raw();
        unsafe { m5unified_sys::m5u_power_timer_sleep_time(&raw) }
    }

    /// Enter timer sleep and wake at the given RTC date and time.
    ///
    /// As in M5Unified, the year, month, and seconds fields are ignored by the
    /// underlying implementation.
    pub fn timer_sleep_date_time(&mut self, date: Date, time: Time) {
        let date = date.to_raw();
        let time = time.to_raw();
        unsafe { m5unified_sys::m5u_power_timer_sleep_date_time(&date, &time) }
    }

    /// Enter ESP32 deep sleep, optionally enabling touch wakeup.
    pub fn deep_sleep_us(&mut self, micro_seconds: u64, touch_wakeup: bool) {
        unsafe { m5unified_sys::m5u_power_deep_sleep_us(micro_seconds, touch_wakeup) }
    }

    /// Enter ESP32 light sleep, optionally enabling touch wakeup.
    pub fn light_sleep_us(&mut self, micro_seconds: u64, touch_wakeup: bool) {
        unsafe { m5unified_sys::m5u_power_light_sleep_us(micro_seconds, touch_wakeup) }
    }

    pub fn axp192(&self) -> Axp192 {
        Axp192
    }

    pub fn axp2101(&self) -> Axp2101 {
        Axp2101
    }

    pub fn aw32001(&self) -> Aw32001 {
        Aw32001
    }

    pub fn bq27220(&self) -> Bq27220 {
        Bq27220
    }

    pub fn ina226(&self) -> Ina226 {
        Ina226
    }

    pub fn ina3221(&self, index: usize) -> Ina3221 {
        Ina3221 { index }
    }

    pub fn ip5306(&self) -> Ip5306 {
        Ip5306
    }

    pub fn py32pmic(&self) -> Py32Pmic {
        Py32Pmic
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ExtPortBusConfig {
    pub voltage_mv: u16,
    pub current_limit_ma: u8,
    pub enable: bool,
    pub direction_output: bool,
}

impl ExtPortBusConfig {
    fn to_raw(self) -> m5unified_sys::m5u_power_ext_port_bus_t {
        m5unified_sys::m5u_power_ext_port_bus_t {
            voltage_mv: self.voltage_mv,
            current_limit_ma: self.current_limit_ma,
            enable: self.enable,
            direction_output: self.direction_output,
        }
    }
}

impl Default for ExtPortBusConfig {
    fn default() -> Self {
        Self {
            voltage_mv: 5_000,
            current_limit_ma: 0,
            enable: false,
            direction_output: false,
        }
    }
}

/// M5Unified PMIC type.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PowerType {
    Unknown,
    Adc,
    Axp192,
    Ip5306,
    Axp2101,
    Aw32001,
    Py32Pmic,
    M5Pm1,
    Raw(i32),
}

impl PowerType {
    /// Convert the raw M5Unified PMIC discriminant into a Rust enum.
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Unknown,
            1 => Self::Adc,
            2 => Self::Axp192,
            3 => Self::Ip5306,
            4 => Self::Axp2101,
            5 => Self::Aw32001,
            6 => Self::Py32Pmic,
            7 => Self::M5Pm1,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified PMIC discriminant.
    pub const fn raw(self) -> i32 {
        match self {
            Self::Unknown => 0,
            Self::Adc => 1,
            Self::Axp192 => 2,
            Self::Ip5306 => 3,
            Self::Axp2101 => 4,
            Self::Aw32001 => 5,
            Self::Py32Pmic => 6,
            Self::M5Pm1 => 7,
            Self::Raw(raw) => raw,
        }
    }
}

/// Battery charge state reported by M5Unified.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ChargeState {
    Discharging,
    Charging,
    Unknown,
    Raw(i32),
}

impl ChargeState {
    /// Convert the raw M5Unified charging-state discriminant into a Rust enum.
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Discharging,
            1 => Self::Charging,
            2 => Self::Unknown,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified charging-state discriminant.
    pub const fn raw(self) -> i32 {
        match self {
            Self::Discharging => 0,
            Self::Charging => 1,
            Self::Unknown => 2,
            Self::Raw(raw) => raw,
        }
    }
}

/// Latched AXP192 PEK button press state.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Axp192PekPress {
    None,
    Long,
    Short,
    Both,
    Raw(u8),
}

impl Axp192PekPress {
    /// Convert the raw M5Unified AXP192 PEK state into a Rust enum.
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::None,
            1 => Self::Long,
            2 => Self::Short,
            3 => Self::Both,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified AXP192 PEK state.
    pub const fn raw(self) -> u8 {
        match self {
            Self::None => 0,
            Self::Long => 1,
            Self::Short => 2,
            Self::Both => 3,
            Self::Raw(raw) => raw,
        }
    }
}

/// Charge state reported directly by the AXP2101 PMIC.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Axp2101ChargeStatus {
    Unavailable,
    Discharging,
    Standby,
    Charging,
    Raw(i32),
}

impl Axp2101ChargeStatus {
    /// Convert the raw M5Unified AXP2101 status into a Rust enum.
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            -2 => Self::Unavailable,
            -1 => Self::Discharging,
            0 => Self::Standby,
            1 => Self::Charging,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified AXP2101 status.
    pub const fn raw(self) -> i32 {
        match self {
            Self::Unavailable => -2,
            Self::Discharging => -1,
            Self::Standby => 0,
            Self::Charging => 1,
            Self::Raw(raw) => raw,
        }
    }
}

/// Latched AXP2101 PEK button press state.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Axp2101PekPress {
    None,
    Long,
    Short,
    Both,
    Raw(u8),
}

impl Axp2101PekPress {
    /// Convert the raw M5Unified AXP2101 PEK state into a Rust enum.
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::None,
            1 => Self::Long,
            2 => Self::Short,
            3 => Self::Both,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified AXP2101 PEK state.
    pub const fn raw(self) -> u8 {
        match self {
            Self::None => 0,
            Self::Long => 1,
            Self::Short => 2,
            Self::Both => 3,
            Self::Raw(raw) => raw,
        }
    }
}

/// Charge state reported directly by the AW32001 charger.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Aw32001ChargeStatus {
    Unknown,
    NotCharging,
    PreCharge,
    Charging,
    ChargeDone,
    Raw(i32),
}

impl Aw32001ChargeStatus {
    /// Convert the raw M5Unified AW32001 charge status into a Rust enum.
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            -1 => Self::Unknown,
            0 => Self::NotCharging,
            1 => Self::PreCharge,
            2 => Self::Charging,
            3 => Self::ChargeDone,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified AW32001 charge status.
    pub const fn raw(self) -> i32 {
        match self {
            Self::Unknown => -1,
            Self::NotCharging => 0,
            Self::PreCharge => 1,
            Self::Charging => 2,
            Self::ChargeDone => 3,
            Self::Raw(raw) => raw,
        }
    }
}

/// Latched PY32 PMIC power-key press state.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Py32PmicPekPress {
    None,
    Short,
    Raw(u8),
}

impl Py32PmicPekPress {
    /// Convert the raw M5Unified PY32 PMIC PEK state into a Rust enum.
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::None,
            2 => Self::Short,
            other => Self::Raw(other),
        }
    }

    /// Return the raw M5Unified PY32 PMIC PEK state.
    pub const fn raw(self) -> u8 {
        match self {
            Self::None => 0,
            Self::Short => 2,
            Self::Raw(raw) => raw,
        }
    }
}

/// INA226 sample averaging rate.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Ina226Sampling {
    Rate1,
    Rate4,
    Rate16,
    Rate64,
    Rate128,
    Rate256,
    Rate512,
    Rate1024,
    Raw(u8),
}

impl Ina226Sampling {
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::Rate1,
            1 => Self::Rate4,
            2 => Self::Rate16,
            3 => Self::Rate64,
            4 => Self::Rate128,
            5 => Self::Rate256,
            6 => Self::Rate512,
            7 => Self::Rate1024,
            other => Self::Raw(other),
        }
    }

    pub const fn raw(self) -> u8 {
        match self {
            Self::Rate1 => 0,
            Self::Rate4 => 1,
            Self::Rate16 => 2,
            Self::Rate64 => 3,
            Self::Rate128 => 4,
            Self::Rate256 => 5,
            Self::Rate512 => 6,
            Self::Rate1024 => 7,
            Self::Raw(raw) => raw,
        }
    }
}

/// INA226 bus or shunt conversion time.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Ina226ConversionTime {
    Us140,
    Us204,
    Us332,
    Us588,
    Us1100,
    Us2116,
    Us4156,
    Us8244,
    Raw(u8),
}

impl Ina226ConversionTime {
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::Us140,
            1 => Self::Us204,
            2 => Self::Us332,
            3 => Self::Us588,
            4 => Self::Us1100,
            5 => Self::Us2116,
            6 => Self::Us4156,
            7 => Self::Us8244,
            other => Self::Raw(other),
        }
    }

    pub const fn raw(self) -> u8 {
        match self {
            Self::Us140 => 0,
            Self::Us204 => 1,
            Self::Us332 => 2,
            Self::Us588 => 3,
            Self::Us1100 => 4,
            Self::Us2116 => 5,
            Self::Us4156 => 6,
            Self::Us8244 => 7,
            Self::Raw(raw) => raw,
        }
    }
}

/// INA226 operation mode.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Ina226Mode {
    PowerDown,
    ShuntVoltageSingle,
    BusVoltageSingle,
    ShuntAndBusSingle,
    ShuntVoltage,
    BusVoltage,
    ShuntAndBus,
    Raw(u8),
}

impl Ina226Mode {
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::PowerDown,
            1 => Self::ShuntVoltageSingle,
            2 => Self::BusVoltageSingle,
            3 => Self::ShuntAndBusSingle,
            5 => Self::ShuntVoltage,
            6 => Self::BusVoltage,
            7 => Self::ShuntAndBus,
            other => Self::Raw(other),
        }
    }

    pub const fn raw(self) -> u8 {
        match self {
            Self::PowerDown => 0,
            Self::ShuntVoltageSingle => 1,
            Self::BusVoltageSingle => 2,
            Self::ShuntAndBusSingle => 3,
            Self::ShuntVoltage => 5,
            Self::BusVoltage => 6,
            Self::ShuntAndBus => 7,
            Self::Raw(raw) => raw,
        }
    }
}

/// INA226 power monitor configuration.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Ina226Config {
    pub shunt_res_ohm: f32,
    pub max_expected_current_a: f32,
    pub sampling_rate: Ina226Sampling,
    pub shunt_conversion_time: Ina226ConversionTime,
    pub bus_conversion_time: Ina226ConversionTime,
    pub mode: Ina226Mode,
}

impl Ina226Config {
    fn to_raw(self) -> m5unified_sys::m5u_power_ina226_config_t {
        m5unified_sys::m5u_power_ina226_config_t {
            shunt_res: self.shunt_res_ohm,
            max_expected_current: self.max_expected_current_a,
            sampling_rate: self.sampling_rate.raw(),
            shunt_conversion_time: self.shunt_conversion_time.raw(),
            bus_conversion_time: self.bus_conversion_time.raw(),
            mode: self.mode.raw(),
        }
    }
}

impl Default for Ina226Config {
    fn default() -> Self {
        Self {
            shunt_res_ohm: 0.1,
            max_expected_current_a: 2.0,
            sampling_rate: Ina226Sampling::Rate16,
            shunt_conversion_time: Ina226ConversionTime::Us1100,
            bus_conversion_time: Ina226ConversionTime::Us1100,
            mode: Ina226Mode::ShuntAndBus,
        }
    }
}

/// External port mask for boards with independently switchable power outputs.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ExtPortMask(u16);

impl ExtPortMask {
    pub const NONE: Self = Self(0);
    pub const PA: Self = Self(1 << 0);
    pub const PB1: Self = Self(1 << 1);
    pub const PB2: Self = Self(1 << 2);
    pub const PC1: Self = Self(1 << 3);
    pub const PC2: Self = Self(1 << 4);
    pub const USB: Self = Self(1 << 5);
    pub const PWR485: Self = Self(1 << 6);
    pub const PWRCAN: Self = Self(1 << 7);
    pub const MAIN: Self = Self(1 << 15);
    pub const ALL: Self = Self(0x00FF);

    pub const fn from_bits(bits: u16) -> Self {
        Self(bits)
    }

    pub const fn bits(self) -> u16 {
        self.0
    }
}

impl Default for ExtPortMask {
    fn default() -> Self {
        Self::NONE
    }
}

impl core::ops::BitOr for ExtPortMask {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl core::ops::BitOrAssign for ExtPortMask {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

impl core::ops::BitAnd for ExtPortMask {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self(self.0 & rhs.0)
    }
}

impl core::ops::BitAndAssign for ExtPortMask {
    fn bitand_assign(&mut self, rhs: Self) {
        self.0 &= rhs.0;
    }
}

#[derive(Debug)]
pub struct Axp192;

impl Axp192 {
    /// Initialize the direct AXP192 backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_begin() }
    }

    /// Return the direct AXP192 battery level as a percentage when available.
    pub fn battery_level(&self) -> Option<u8> {
        let level = unsafe { m5unified_sys::m5u_power_axp192_get_battery_level() };
        if (0..=100).contains(&level) {
            Some(level as u8)
        } else {
            None
        }
    }

    /// Enable or disable battery charging through the AXP192.
    pub fn set_battery_charge(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_battery_charge(enable) }
    }

    /// Set the AXP192 charge-current target in milliamps.
    pub fn set_charge_current_ma(&self, max_ma: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_charge_current(max_ma) }
    }

    /// Set the AXP192 charge-voltage target in millivolts.
    pub fn set_charge_voltage_mv(&self, max_mv: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_charge_voltage(max_mv) }
    }

    /// Return whether the AXP192 reports that the battery is charging.
    pub fn is_charging(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_is_charging() }
    }

    fn voltage_raw(voltage_mv: Option<u16>) -> i32 {
        voltage_mv.map(i32::from).unwrap_or(-1)
    }

    fn set_dcdc(&self, channel: u8, voltage_mv: Option<u16>) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_dcdc(channel, Self::voltage_raw(voltage_mv)) }
    }

    fn set_ldo(&self, channel: u8, voltage_mv: Option<u16>) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_ldo(channel, Self::voltage_raw(voltage_mv)) }
    }

    /// Set DCDC1 output in millivolts, or disable it with `None`.
    pub fn set_dcdc1_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_dcdc(1, voltage_mv)
    }

    /// Set DCDC2 output in millivolts, or disable it with `None`.
    pub fn set_dcdc2_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_dcdc(2, voltage_mv)
    }

    /// Set DCDC3 output in millivolts, or disable it with `None`.
    pub fn set_dcdc3_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_dcdc(3, voltage_mv)
    }

    /// Set LDOio0 output in millivolts, or disable it with `None`.
    pub fn set_ldo0_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(0, voltage_mv)
    }

    /// Set LDO2 output in millivolts, or disable it with `None`.
    pub fn set_ldo2_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(2, voltage_mv)
    }

    /// Set LDO3 output in millivolts, or disable it with `None`.
    pub fn set_ldo3_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(3, voltage_mv)
    }

    /// Set one AXP192 GPIO output state.
    pub fn set_gpio(&self, gpio_num: u8, state: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_gpio(gpio_num, state) }
    }

    pub fn set_gpio0(&self, state: bool) -> bool {
        self.set_gpio(0, state)
    }

    pub fn set_gpio1(&self, state: bool) -> bool {
        self.set_gpio(1, state)
    }

    pub fn set_gpio2(&self, state: bool) -> bool {
        self.set_gpio(2, state)
    }

    pub fn set_gpio3(&self, state: bool) -> bool {
        self.set_gpio(3, state)
    }

    pub fn set_gpio4(&self, state: bool) -> bool {
        self.set_gpio(4, state)
    }

    /// Power the board off through the AXP192.
    pub fn power_off(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_power_off() }
    }

    /// Enable or disable the AXP192 ADC channels.
    pub fn set_adc_state(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_adc_state(enable) }
    }

    /// Set the AXP192 ADC rate.
    pub fn set_adc_rate(&self, rate: u8) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_adc_rate(rate) }
    }

    /// Enable or disable AXP192 EXTEN output.
    pub fn set_exten(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_exten(enable) }
    }

    /// Enable or disable AXP192 backup output.
    pub fn set_backup(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_set_backup(enable) }
    }

    /// Return whether ACIN is present.
    pub fn is_acin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_is_acin() }
    }

    /// Return whether VBUS is present.
    pub fn is_vbus(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_is_vbus() }
    }

    /// Return whether the AXP192 reports a battery present.
    pub fn battery_present(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_get_bat_state() }
    }

    /// Return whether AXP192 EXTEN output is enabled.
    pub fn exten(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp192_get_exten() }
    }

    /// Return battery voltage in volts.
    pub fn battery_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_battery_voltage_v() }
    }

    /// Return battery discharge current in milliamps.
    pub fn battery_discharge_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_battery_discharge_current_ma() }
    }

    /// Return battery charge current in milliamps.
    pub fn battery_charge_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_battery_charge_current_ma() }
    }

    /// Return battery power in milliwatts.
    pub fn battery_power_mw(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_battery_power_mw() }
    }

    /// Return ACIN voltage in volts.
    pub fn acin_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_acin_voltage_v() }
    }

    /// Return ACIN current in milliamps.
    pub fn acin_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_acin_current_ma() }
    }

    /// Return VBUS voltage in volts.
    pub fn vbus_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_vbus_voltage_v() }
    }

    /// Return VBUS current in milliamps.
    pub fn vbus_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_vbus_current_ma() }
    }

    /// Return APS voltage in volts.
    pub fn aps_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_aps_voltage_v() }
    }

    /// Return internal PMIC temperature in degrees Celsius.
    pub fn internal_temperature_c(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp192_get_internal_temperature_c() }
    }

    /// Return the latched AXP192 PEK press state.
    ///
    /// On supported PMICs this read clears the latched state.
    pub fn pek_press(&self) -> Axp192PekPress {
        Axp192PekPress::from_raw(unsafe { m5unified_sys::m5u_power_axp192_get_pek_press() })
    }
}

#[derive(Debug)]
pub struct Aw32001;

impl Aw32001 {
    /// Initialize the direct AW32001 backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_aw32001_begin() }
    }

    /// Enable or disable battery charging through the AW32001.
    pub fn set_battery_charge(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_aw32001_set_battery_charge(enable) }
    }

    /// Set the AW32001 charge-current target in milliamps.
    pub fn set_charge_current_ma(&self, max_ma: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_aw32001_set_charge_current(max_ma) }
    }

    /// Set the AW32001 charge-voltage target in millivolts.
    pub fn set_charge_voltage_mv(&self, max_mv: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_aw32001_set_charge_voltage(max_mv) }
    }

    /// Return whether the AW32001 reports that the battery is charging.
    pub fn is_charging(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_aw32001_is_charging() }
    }

    /// Return the configured AW32001 charge current in milliamps.
    pub fn charge_current_ma(&self) -> Option<u16> {
        let current = unsafe { m5unified_sys::m5u_power_aw32001_get_charge_current() };
        (current != 0).then_some(current)
    }

    /// Return the configured AW32001 charge voltage in millivolts.
    pub fn charge_voltage_mv(&self) -> Option<u16> {
        let voltage = unsafe { m5unified_sys::m5u_power_aw32001_get_charge_voltage() };
        (voltage != 0).then_some(voltage)
    }

    /// Return the direct AW32001 charge status.
    pub fn charge_status(&self) -> Aw32001ChargeStatus {
        Aw32001ChargeStatus::from_raw(unsafe {
            m5unified_sys::m5u_power_aw32001_get_charge_status()
        })
    }
}

#[derive(Debug)]
pub struct Bq27220;

impl Bq27220 {
    /// Initialize the direct BQ27220 battery gauge backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_bq27220_begin() }
    }

    /// Return battery current in milliamps.
    pub fn current_ma(&self) -> i16 {
        unsafe { m5unified_sys::m5u_power_bq27220_get_current_ma() }
    }

    /// Return battery voltage in millivolts.
    pub fn voltage_mv(&self) -> i16 {
        unsafe { m5unified_sys::m5u_power_bq27220_get_voltage_mv() }
    }

    /// Return battery current in amps.
    pub fn current_a(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_bq27220_get_current_a() }
    }

    /// Return battery voltage in volts.
    pub fn voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_bq27220_get_voltage_v() }
    }
}

#[derive(Debug)]
pub struct Ina226;

impl Ina226 {
    /// Initialize the direct INA226 power monitor backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_ina226_begin() }
    }

    /// Configure INA226 sampling, conversion, and calibration parameters.
    pub fn configure(&self, config: Ina226Config) -> bool {
        let raw = config.to_raw();
        unsafe { m5unified_sys::m5u_power_ina226_config(&raw) }
    }

    /// Return bus voltage in volts.
    pub fn bus_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_ina226_get_bus_voltage_v() }
    }

    /// Return shunt voltage in volts.
    pub fn shunt_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_ina226_get_shunt_voltage_v() }
    }

    /// Return shunt current in amps.
    pub fn shunt_current_a(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_ina226_get_shunt_current_a() }
    }

    /// Return measured power in watts.
    pub fn power_w(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_ina226_get_power_w() }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Ina3221 {
    index: usize,
}

impl Ina3221 {
    pub const CHANNEL_COUNT: usize = 3;

    /// Return the zero-based upstream monitor index.
    pub const fn index(&self) -> usize {
        self.index
    }

    /// Initialize the selected direct INA3221 power monitor.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_ina3221_begin(self.index) }
    }

    /// Set the shunt resistance for the selected channel in milliohms.
    pub fn set_shunt_res_milliohm(&self, channel: usize, res: u32) -> bool {
        unsafe {
            m5unified_sys::m5u_power_ina3221_set_shunt_res(
                self.index,
                Self::channel_raw(channel),
                res,
            )
        }
    }

    /// Return bus voltage for the selected channel in volts.
    pub fn bus_voltage_v(&self, channel: usize) -> f32 {
        unsafe {
            m5unified_sys::m5u_power_ina3221_get_bus_voltage_v(
                self.index,
                Self::channel_raw(channel),
            )
        }
    }

    /// Return shunt voltage for the selected channel in volts.
    pub fn shunt_voltage_v(&self, channel: usize) -> f32 {
        unsafe {
            m5unified_sys::m5u_power_ina3221_get_shunt_voltage_v(
                self.index,
                Self::channel_raw(channel),
            )
        }
    }

    /// Return current for the selected channel in amps.
    pub fn current_a(&self, channel: usize) -> f32 {
        unsafe {
            m5unified_sys::m5u_power_ina3221_get_current_a(self.index, Self::channel_raw(channel))
        }
    }

    /// Return bus voltage for the selected channel in millivolts.
    pub fn bus_voltage_mv(&self, channel: usize) -> i32 {
        unsafe {
            m5unified_sys::m5u_power_ina3221_get_bus_voltage_mv(
                self.index,
                Self::channel_raw(channel),
            )
        }
    }

    /// Return shunt voltage for the selected channel in millivolts.
    pub fn shunt_voltage_mv(&self, channel: usize) -> i32 {
        unsafe {
            m5unified_sys::m5u_power_ina3221_get_shunt_voltage_mv(
                self.index,
                Self::channel_raw(channel),
            )
        }
    }

    fn channel_raw(channel: usize) -> u8 {
        if channel > u8::MAX as usize {
            u8::MAX
        } else {
            channel as u8
        }
    }
}

#[derive(Debug)]
pub struct Ip5306;

impl Ip5306 {
    /// Initialize the direct IP5306 backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_ip5306_begin() }
    }

    /// Return the direct IP5306 battery level as a percentage when available.
    pub fn battery_level(&self) -> Option<u8> {
        let level = unsafe { m5unified_sys::m5u_power_ip5306_get_battery_level() };
        if (0..=100).contains(&level) {
            Some(level as u8)
        } else {
            None
        }
    }

    /// Enable or disable battery charging through the IP5306.
    pub fn set_battery_charge(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_ip5306_set_battery_charge(enable) }
    }

    /// Set the IP5306 charge-current target in milliamps.
    pub fn set_charge_current_ma(&self, max_ma: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_ip5306_set_charge_current(max_ma) }
    }

    /// Set the IP5306 charge-voltage target in millivolts.
    pub fn set_charge_voltage_mv(&self, max_mv: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_ip5306_set_charge_voltage(max_mv) }
    }

    /// Return whether the IP5306 reports that the battery is charging.
    pub fn is_charging(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_ip5306_is_charging() }
    }

    /// Set whether the IP5306 keeps boost output alive under low load.
    pub fn set_power_boost_keep_on(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_ip5306_set_power_boost_keep_on(enable) }
    }
}

#[derive(Debug)]
pub struct Py32Pmic;

impl Py32Pmic {
    /// Initialize the direct PY32 PMIC backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_begin() }
    }

    /// Enable or disable external output through the PY32 PMIC.
    pub fn set_ext_output(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_set_ext_output(enable) }
    }

    /// Enable or disable battery charging through the PY32 PMIC.
    pub fn set_battery_charge(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_set_battery_charge(enable) }
    }

    /// Set the PY32 PMIC charge-current target in milliamps.
    ///
    /// M5Unified currently exposes this as unsupported and returns `false`.
    pub fn set_charge_current_ma(&self, max_ma: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_set_charge_current(max_ma) }
    }

    /// Set the PY32 PMIC charge-voltage target in millivolts.
    ///
    /// M5Unified currently exposes this as unsupported and returns `false`.
    pub fn set_charge_voltage_mv(&self, max_mv: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_set_charge_voltage(max_mv) }
    }

    /// Return whether the PY32 PMIC reports that the battery is charging.
    ///
    /// M5Unified currently returns `false` for this direct PY32 PMIC helper.
    pub fn is_charging(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_is_charging() }
    }

    /// Return the configured PY32 PMIC charge current in milliamps.
    pub fn charge_current_ma(&self) -> Option<u16> {
        let current = unsafe { m5unified_sys::m5u_power_py32pmic_get_charge_current() };
        (current != 0).then_some(current)
    }

    /// Return the configured PY32 PMIC charge voltage in millivolts.
    pub fn charge_voltage_mv(&self) -> Option<u16> {
        let voltage = unsafe { m5unified_sys::m5u_power_py32pmic_get_charge_voltage() };
        (voltage != 0).then_some(voltage)
    }

    /// Return the latched PY32 PMIC PEK press state.
    ///
    /// On supported PMICs this read clears the latched short-press state.
    pub fn pek_press(&self) -> Py32PmicPekPress {
        Py32PmicPekPress::from_raw(unsafe { m5unified_sys::m5u_power_py32pmic_get_pek_press() })
    }

    /// Power the board off through the PY32 PMIC.
    pub fn power_off(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_py32pmic_power_off() }
    }
}

#[derive(Debug)]
pub struct Axp2101;

impl Axp2101 {
    const LDO_KIND_ALDO: i32 = 0;
    const LDO_KIND_BLDO: i32 = 1;
    const LDO_KIND_DLDO: i32 = 2;

    pub const IRQ_ALL: u64 = u64::MAX;
    pub const IRQ_BAT_WORK_UNDER_TEMP: u64 = 1 << 0;
    pub const IRQ_BAT_WORK_OVER_TEMP: u64 = 1 << 1;
    pub const IRQ_BAT_CHG_UNDER_TEMP: u64 = 1 << 2;
    pub const IRQ_BAT_CHG_OVER_TEMP: u64 = 1 << 3;
    pub const IRQ_GAUGE_NEW_SOC: u64 = 1 << 4;
    pub const IRQ_GAUGE_WDT_TIMEOUT: u64 = 1 << 5;
    pub const IRQ_WARNING_LEVEL1: u64 = 1 << 6;
    pub const IRQ_WARNING_LEVEL2: u64 = 1 << 7;
    pub const IRQ_PKEY_POSITIVE_EDGE: u64 = 1 << 8;
    pub const IRQ_PKEY_NEGATIVE_EDGE: u64 = 1 << 9;
    pub const IRQ_PKEY_LONG_PRESS: u64 = 1 << 10;
    pub const IRQ_PKEY_SHORT_PRESS: u64 = 1 << 11;
    pub const IRQ_BAT_REMOVE: u64 = 1 << 12;
    pub const IRQ_BAT_INSERT: u64 = 1 << 13;
    pub const IRQ_VBUS_REMOVE: u64 = 1 << 14;
    pub const IRQ_VBUS_INSERT: u64 = 1 << 15;
    pub const IRQ_BAT_OVER_VOLTAGE: u64 = 1 << 16;
    pub const IRQ_CHARGER_TIMER: u64 = 1 << 17;
    pub const IRQ_DIE_OVER_TEMP: u64 = 1 << 18;
    pub const IRQ_BAT_CHG_START: u64 = 1 << 19;
    pub const IRQ_BAT_CHG_DONE: u64 = 1 << 20;
    pub const IRQ_BATFET_OVER_CURR: u64 = 1 << 21;
    pub const IRQ_LDO_OVER_CURR: u64 = 1 << 22;
    pub const IRQ_WDT_EXPIRE: u64 = 1 << 23;

    /// Initialize the direct AXP2101 backend when this board has one.
    pub fn begin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_begin() }
    }

    /// Return the direct AXP2101 battery level as a percentage when available.
    pub fn battery_level(&self) -> Option<u8> {
        let level = unsafe { m5unified_sys::m5u_power_axp2101_get_battery_level() };
        if (0..=100).contains(&level) {
            Some(level as u8)
        } else {
            None
        }
    }

    /// Enable or disable battery charging through the AXP2101.
    pub fn set_battery_charge(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_battery_charge(enable) }
    }

    /// Set the AXP2101 pre-charge current target in milliamps.
    pub fn set_pre_charge_current_ma(&self, max_ma: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_pre_charge_current(max_ma) }
    }

    /// Set the AXP2101 charge-current target in milliamps.
    pub fn set_charge_current_ma(&self, max_ma: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_charge_current(max_ma) }
    }

    /// Set the AXP2101 charge-voltage target in millivolts.
    pub fn set_charge_voltage_mv(&self, max_mv: u16) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_charge_voltage(max_mv) }
    }

    /// Return the direct AXP2101 charge status.
    pub fn charge_status(&self) -> Axp2101ChargeStatus {
        Axp2101ChargeStatus::from_raw(unsafe {
            m5unified_sys::m5u_power_axp2101_get_charge_status()
        })
    }

    /// Return whether the AXP2101 reports that the battery is charging.
    pub fn is_charging(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_is_charging() }
    }

    fn set_ldo(&self, kind: i32, channel: i32, voltage_mv: Option<u16>) -> bool {
        let voltage_mv = voltage_mv.map(i32::from).unwrap_or(-1);
        unsafe { m5unified_sys::m5u_power_axp2101_set_ldo(kind, channel, voltage_mv) }
    }

    fn ldo_enabled(&self, kind: i32, channel: i32) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_get_ldo_enabled(kind, channel) }
    }

    /// Set ALDO1 output in millivolts, or disable it with `None`.
    pub fn set_aldo1_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_ALDO, 1, voltage_mv)
    }

    /// Set ALDO2 output in millivolts, or disable it with `None`.
    pub fn set_aldo2_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_ALDO, 2, voltage_mv)
    }

    /// Set ALDO3 output in millivolts, or disable it with `None`.
    pub fn set_aldo3_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_ALDO, 3, voltage_mv)
    }

    /// Set ALDO4 output in millivolts, or disable it with `None`.
    pub fn set_aldo4_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_ALDO, 4, voltage_mv)
    }

    /// Set BLDO1 output in millivolts, or disable it with `None`.
    pub fn set_bldo1_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_BLDO, 1, voltage_mv)
    }

    /// Set BLDO2 output in millivolts, or disable it with `None`.
    pub fn set_bldo2_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_BLDO, 2, voltage_mv)
    }

    /// Set DLDO1 output in millivolts, or disable it with `None`.
    pub fn set_dldo1_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_DLDO, 1, voltage_mv)
    }

    /// Set DLDO2 output in millivolts, or disable it with `None`.
    pub fn set_dldo2_mv(&self, voltage_mv: Option<u16>) -> bool {
        self.set_ldo(Self::LDO_KIND_DLDO, 2, voltage_mv)
    }

    /// Return whether ALDO1 is enabled.
    pub fn aldo1_enabled(&self) -> bool {
        self.ldo_enabled(Self::LDO_KIND_ALDO, 1)
    }

    /// Return whether ALDO2 is enabled.
    pub fn aldo2_enabled(&self) -> bool {
        self.ldo_enabled(Self::LDO_KIND_ALDO, 2)
    }

    /// Return whether ALDO3 is enabled.
    pub fn aldo3_enabled(&self) -> bool {
        self.ldo_enabled(Self::LDO_KIND_ALDO, 3)
    }

    /// Return whether ALDO4 is enabled.
    pub fn aldo4_enabled(&self) -> bool {
        self.ldo_enabled(Self::LDO_KIND_ALDO, 4)
    }

    /// Return whether BLDO1 is enabled.
    pub fn bldo1_enabled(&self) -> bool {
        self.ldo_enabled(Self::LDO_KIND_BLDO, 1)
    }

    /// Return whether BLDO2 is enabled.
    pub fn bldo2_enabled(&self) -> bool {
        self.ldo_enabled(Self::LDO_KIND_BLDO, 2)
    }

    /// Power the board off through the AXP2101.
    pub fn power_off(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_power_off() }
    }

    /// Enable or disable the AXP2101 ADC channels.
    pub fn set_adc_state(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_adc_state(enable) }
    }

    /// Set the AXP2101 ADC rate.
    ///
    /// M5Unified currently accepts the value but does not program a register.
    pub fn set_adc_rate(&self, rate: u8) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_adc_rate(rate) }
    }

    /// Enable or disable AXP2101 backup output.
    ///
    /// M5Unified currently exposes this as a no-op on AXP2101.
    pub fn set_backup(&self, enable: bool) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_set_backup(enable) }
    }

    /// Return whether ACIN is present.
    pub fn is_acin(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_is_acin() }
    }

    /// Return whether VBUS is present.
    pub fn is_vbus(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_is_vbus() }
    }

    /// Return whether the AXP2101 reports a battery present.
    pub fn battery_present(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_get_bat_state() }
    }

    /// Return battery voltage in volts.
    pub fn battery_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_battery_voltage_v() }
    }

    /// Return battery discharge current in milliamps.
    pub fn battery_discharge_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_battery_discharge_current_ma() }
    }

    /// Return battery charge current in milliamps.
    pub fn battery_charge_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_battery_charge_current_ma() }
    }

    /// Return battery power in milliwatts.
    pub fn battery_power_mw(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_battery_power_mw() }
    }

    /// Return ACIN voltage in volts.
    pub fn acin_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_acin_voltage_v() }
    }

    /// Return ACIN current in milliamps.
    pub fn acin_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_acin_current_ma() }
    }

    /// Return VBUS voltage in volts.
    pub fn vbus_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_vbus_voltage_v() }
    }

    /// Return VBUS current in milliamps.
    pub fn vbus_current_ma(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_vbus_current_ma() }
    }

    /// Return TS pin voltage in volts.
    pub fn ts_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_ts_voltage_v() }
    }

    /// Return APS voltage in volts.
    pub fn aps_voltage_v(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_aps_voltage_v() }
    }

    /// Return internal PMIC temperature in degrees Celsius.
    pub fn internal_temperature_c(&self) -> f32 {
        unsafe { m5unified_sys::m5u_power_axp2101_get_internal_temperature_c() }
    }

    /// Return the latched AXP2101 PEK press state.
    ///
    /// On supported PMICs this read clears the latched state.
    pub fn pek_press(&self) -> Axp2101PekPress {
        Axp2101PekPress::from_raw(unsafe { m5unified_sys::m5u_power_axp2101_get_pek_press() })
    }

    pub fn disable_irq(&self, mask: u64) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_disable_irq(mask) }
    }

    pub fn enable_irq(&self, mask: u64) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_enable_irq(mask) }
    }

    pub fn clear_irq_statuses(&self) -> bool {
        unsafe { m5unified_sys::m5u_power_axp2101_clear_irq_statuses() }
    }

    pub fn irq_statuses(&self) -> Axp2101IrqStatus {
        Axp2101IrqStatus::from_status_register_order(unsafe {
            m5unified_sys::m5u_power_axp2101_get_irq_statuses()
        })
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct Axp2101IrqStatus {
    pub raw: u64,
}

impl Axp2101IrqStatus {
    fn from_status_register_order(raw: u64) -> Self {
        Self {
            raw: ((raw >> 16) & 0xFF) | (raw & 0xFF00) | ((raw & 0xFF) << 16),
        }
    }

    pub fn contains(&self, mask: u64) -> bool {
        self.raw & mask != 0
    }

    pub fn is_empty(&self) -> bool {
        self.raw == 0
    }

    pub fn battery_work_under_temperature(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_WORK_UNDER_TEMP)
    }

    pub fn battery_work_over_temperature(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_WORK_OVER_TEMP)
    }

    pub fn battery_charger_under_temperature(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_CHG_UNDER_TEMP)
    }

    pub fn battery_charger_over_temperature(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_CHG_OVER_TEMP)
    }

    pub fn gauge_new_soc(&self) -> bool {
        self.contains(Axp2101::IRQ_GAUGE_NEW_SOC)
    }

    pub fn gauge_watchdog_timeout(&self) -> bool {
        self.contains(Axp2101::IRQ_GAUGE_WDT_TIMEOUT)
    }

    pub fn warning_level1(&self) -> bool {
        self.contains(Axp2101::IRQ_WARNING_LEVEL1)
    }

    pub fn warning_level2(&self) -> bool {
        self.contains(Axp2101::IRQ_WARNING_LEVEL2)
    }

    pub fn pkey_positive_edge(&self) -> bool {
        self.contains(Axp2101::IRQ_PKEY_POSITIVE_EDGE)
    }

    pub fn pkey_negative_edge(&self) -> bool {
        self.contains(Axp2101::IRQ_PKEY_NEGATIVE_EDGE)
    }

    pub fn pkey_long_press(&self) -> bool {
        self.contains(Axp2101::IRQ_PKEY_LONG_PRESS)
    }

    pub fn pkey_short_press(&self) -> bool {
        self.contains(Axp2101::IRQ_PKEY_SHORT_PRESS)
    }

    pub fn battery_remove(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_REMOVE)
    }

    pub fn battery_insert(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_INSERT)
    }

    pub fn vbus_insert(&self) -> bool {
        self.contains(Axp2101::IRQ_VBUS_INSERT)
    }

    pub fn vbus_remove(&self) -> bool {
        self.contains(Axp2101::IRQ_VBUS_REMOVE)
    }

    pub fn battery_over_voltage(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_OVER_VOLTAGE)
    }

    pub fn charger_timer_expired(&self) -> bool {
        self.contains(Axp2101::IRQ_CHARGER_TIMER)
    }

    pub fn die_over_temperature(&self) -> bool {
        self.contains(Axp2101::IRQ_DIE_OVER_TEMP)
    }

    pub fn battery_charge_start(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_CHG_START)
    }

    pub fn battery_charge_done(&self) -> bool {
        self.contains(Axp2101::IRQ_BAT_CHG_DONE)
    }

    pub fn batfet_over_current(&self) -> bool {
        self.contains(Axp2101::IRQ_BATFET_OVER_CURR)
    }

    pub fn ldo_over_current(&self) -> bool {
        self.contains(Axp2101::IRQ_LDO_OVER_CURR)
    }

    pub fn watchdog_expired(&self) -> bool {
        self.contains(Axp2101::IRQ_WDT_EXPIRE)
    }
}

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

    #[test]
    fn axp2101_status_register_order_is_normalized_to_irq_masks() {
        let upstream_raw = (0b0000_0101_u64 << 16) | (0b1000_0000_u64 << 8) | 0b0100_0000_u64;
        let status = Axp2101IrqStatus::from_status_register_order(upstream_raw);

        assert!(status.battery_work_under_temperature());
        assert!(status.battery_charger_under_temperature());
        assert!(status.vbus_insert());
        assert!(status.ldo_over_current());
        assert!(!status.battery_work_over_temperature());
    }
}