a7105/registers/
battery.rs1use super::*;
2use defmt::Format;
3
4#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
5pub struct BatteryDetectConfig {
6 pub sleep_voltage_setting: SleepModeVoltageSetting,
8 pub nonsleep_voltage_setting: NonSleepModeVoltageSetting,
10 pub detect_threshold: DetectThreshold,
12 pub detect_enabled: bool,
14}
15
16impl Register for BatteryDetectConfig {
17 fn id() -> u8 {
18 0x27
19 }
20}
21
22impl WritableRegister for BatteryDetectConfig {}
23
24impl From<BatteryDetectConfig> for u8 {
25 fn from(val: BatteryDetectConfig) -> Self {
26 u8::from(val.detect_enabled)
27 | match val.detect_threshold {
28 DetectThreshold::V20 => 0b000,
29 DetectThreshold::V21 => 0b001,
30 DetectThreshold::V22 => 0b010,
31 DetectThreshold::V23 => 0b011,
32 DetectThreshold::V24 => 0b100,
33 DetectThreshold::V25 => 0b101,
34 DetectThreshold::V26 => 0b110,
35 DetectThreshold::V27 => 0b111,
36 } << 1
37 | match val.nonsleep_voltage_setting {
38 NonSleepModeVoltageSetting::V18 => 0b11,
39 NonSleepModeVoltageSetting::V19 => 0b10,
40 NonSleepModeVoltageSetting::V20 => 0b01,
41 NonSleepModeVoltageSetting::V21 => 0b00,
42 } << 5
43 | match val.sleep_voltage_setting {
44 SleepModeVoltageSetting::ThreeFifth => 0b0,
45 SleepModeVoltageSetting::ThreeForths => 0b1,
46 } << 7
47 }
48}
49
50#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
51pub enum SleepModeVoltageSetting {
52 #[default]
54 ThreeFifth,
55 ThreeForths,
57}
58
59#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
60pub enum NonSleepModeVoltageSetting {
61 V18,
63 V19,
65 V20,
67 #[default]
69 V21,
70}
71
72#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
73pub enum DetectThreshold {
74 V20,
76 V21,
78 V22,
80 #[default]
82 V23,
83 V24,
85 V25,
87 V26,
89 V27,
91}
92
93#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
94pub struct BatteryDetectResult {
95 pub voltage_above_threshold: bool,
97}
98
99impl Register for BatteryDetectResult {
100 fn id() -> u8 {
101 0x27
102 }
103}
104
105impl ReadableRegister for BatteryDetectResult {}
106
107impl From<u8> for BatteryDetectResult {
108 fn from(val: u8) -> Self {
109 Self {
110 voltage_above_threshold: (val & 0b0001_0000) != 0,
111 }
112 }
113}
114
115#[cfg(test)]
116mod test {
117 use super::super::Register as _;
118 use super::*;
119
120 #[test]
121 fn test_battery_detect() {
122 let default: u8 = BatteryDetectConfig::default().into();
123 assert_eq!(default, 0b0000_0110);
124
125 assert_eq!(BatteryDetectConfig::id(), 0x27);
126 assert_eq!(BatteryDetectResult::id(), 0x27);
127 }
128}