waveshare_ups_hat_e/
registers.rs1use crate::error::Error;
5
6pub(crate) struct RegisterBlock {
8 pub(crate) id: u8,
10 pub(crate) length: u8,
12}
13
14pub(crate) const CHARGING_REG: RegisterBlock = RegisterBlock {
16 id: 0x02,
17 length: 1,
18};
19
20pub(crate) const COMMUNICATION_REG: RegisterBlock = RegisterBlock {
22 id: 0x03,
23 length: 1,
24};
25
26pub(crate) const USBC_VBUS_REG: RegisterBlock = RegisterBlock {
28 id: 0x10,
29 length: 6,
30};
31
32pub(crate) const BATTERY_REG: RegisterBlock = RegisterBlock {
34 id: 0x20,
35 length: 12,
36};
37
38pub(crate) const CELL_VOLTAGE_REG: RegisterBlock = RegisterBlock {
40 id: 0x30,
41 length: 8,
42};
43
44pub (crate) const POWEROFF_REG: RegisterBlock = RegisterBlock {
46 id: 0x01,
47 length: 1,
48};
49
50pub (crate) const SOFTWARE_REV_REG: RegisterBlock = RegisterBlock {
52 id: 0x50,
53 length: 1,
54};
55
56#[derive(Debug)]
58pub enum ChargerActivity {
59 Standby = 0b000,
60 Trickle = 0b001,
61 ConstantCurrent = 0b010,
62 ConstantVoltage = 0b011,
63 Pending = 0b100,
64 Full = 0b101,
65 Timeout = 0b110,
66}
67
68impl TryFrom<u8> for ChargerActivity {
69 type Error = Error;
70
71 fn try_from(value: u8) -> Result<Self, Self::Error> {
72 match value {
73 0b000 => Ok(ChargerActivity::Standby),
74 0b001 => Ok(ChargerActivity::Trickle),
75 0b010 => Ok(ChargerActivity::ConstantCurrent),
76 0b011 => Ok(ChargerActivity::ConstantVoltage),
77 0b100 => Ok(ChargerActivity::Pending),
78 0b101 => Ok(ChargerActivity::Full),
79 0b110 => Ok(ChargerActivity::Timeout),
80 _ => Err(Error::InvalidChargerActivity(value)),
81 }
82 }
83}
84
85#[derive(Debug)]
87pub enum CommState {
88 Error = 0b0,
89 Normal = 0b1,
90}
91
92impl From<bool> for CommState {
93 fn from(value: bool) -> Self {
94 if value {
95 CommState::Normal
96 } else {
97 CommState::Error
98 }
99 }
100}
101
102#[derive(Debug)]
104pub enum UsbCInputState {
105 NoPower = 0b0,
106 Powered = 0b1,
107}
108
109impl From<bool> for UsbCInputState {
110 fn from(value: bool) -> Self {
111 if value {
112 UsbCInputState::Powered
113 } else {
114 UsbCInputState::NoPower
115 }
116 }
117}
118
119#[derive(Debug)]
121pub enum UsbCPowerDelivery {
122 StandardCharging = 0b0,
123 FastCharging = 0b1,
124}
125
126impl From<bool> for UsbCPowerDelivery {
127 fn from(value: bool) -> Self {
128 if value {
129 UsbCPowerDelivery::FastCharging
130 } else {
131 UsbCPowerDelivery::StandardCharging
132 }
133 }
134}
135
136#[derive(Debug)]
138pub enum ChargingState {
139 NotCharging = 0b0,
140 Charging = 0b1,
141}
142
143impl From<bool> for ChargingState {
144 fn from(value: bool) -> Self {
145 if value {
146 ChargingState::Charging
147 } else {
148 ChargingState::NotCharging
149 }
150 }
151}