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
44#[derive(Debug)]
46pub enum ChargerActivity {
47 Standby = 0b000,
48 Trickle = 0b001,
49 ConstantCurrent = 0b010,
50 ConstantVoltage = 0b011,
51 Pending = 0b100,
52 Full = 0b101,
53 Timeout = 0b110,
54}
55
56impl TryFrom<u8> for ChargerActivity {
57 type Error = Error;
58
59 fn try_from(value: u8) -> Result<Self, Self::Error> {
60 match value {
61 0b000 => Ok(ChargerActivity::Standby),
62 0b001 => Ok(ChargerActivity::Trickle),
63 0b010 => Ok(ChargerActivity::ConstantCurrent),
64 0b011 => Ok(ChargerActivity::ConstantVoltage),
65 0b100 => Ok(ChargerActivity::Pending),
66 0b101 => Ok(ChargerActivity::Full),
67 0b110 => Ok(ChargerActivity::Timeout),
68 _ => Err(Error::InvalidChargerActivity(value)),
69 }
70 }
71}
72
73#[derive(Debug)]
75pub enum UsbCInputState {
76 NoPower = 0b0,
77 Powered = 0b1,
78}
79
80impl From<bool> for UsbCInputState {
81 fn from(value: bool) -> Self {
82 if value {
83 UsbCInputState::Powered
84 } else {
85 UsbCInputState::NoPower
86 }
87 }
88}
89
90#[derive(Debug)]
92pub enum UsbCPowerDelivery {
93 StandardCharging = 0b0,
94 FastCharging = 0b1,
95}
96
97impl From<bool> for UsbCPowerDelivery {
98 fn from(value: bool) -> Self {
99 if value {
100 UsbCPowerDelivery::FastCharging
101 } else {
102 UsbCPowerDelivery::StandardCharging
103 }
104 }
105}
106
107#[derive(Debug)]
109pub enum ChargingState {
110 NotCharging = 0b0,
111 Charging = 0b1,
112}
113
114impl From<bool> for ChargingState {
115 fn from(value: bool) -> Self {
116 if value {
117 ChargingState::Charging
118 } else {
119 ChargingState::NotCharging
120 }
121 }
122}