waveshare_ups_hat_e/
registers.rs

1// Copyright (c) 2025 Stuart Stock
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use crate::error::Error;
5
6/// A logical grouping of registers for similar/related functionality
7pub(crate) struct RegisterBlock {
8    /// Register ID
9    pub(crate) id: u8,
10    /// Number of bytes to read from the register
11    pub(crate) length: u8,
12}
13
14/// Plugged in, power delivery detected, charger activity
15pub(crate) const CHARGING_REG: RegisterBlock = RegisterBlock {
16    id: 0x02,
17    length: 1,
18};
19
20/// Status of the BQ4050 and IP2368
21pub(crate) const COMMUNICATION_REG: RegisterBlock = RegisterBlock {
22    id: 0x03,
23    length: 1,
24};
25
26/// USB-C Power voltage, current, power
27pub(crate) const USBC_VBUS_REG: RegisterBlock = RegisterBlock {
28    id: 0x10,
29    length: 6,
30};
31
32/// Battery voltage, current, remaining runtime, time-to-full
33pub(crate) const BATTERY_REG: RegisterBlock = RegisterBlock {
34    id: 0x20,
35    length: 12,
36};
37
38/// Cell voltages for all four batteries
39pub(crate) const CELL_VOLTAGE_REG: RegisterBlock = RegisterBlock {
40    id: 0x30,
41    length: 8,
42};
43
44/// Write to power-off, read to see if power-off is pending
45pub (crate) const POWEROFF_REG: RegisterBlock = RegisterBlock {
46    id: 0x01,
47    length: 1,
48};
49
50/// Software revision of the UPS firmware
51pub (crate) const SOFTWARE_REV_REG: RegisterBlock = RegisterBlock {
52    id: 0x50,
53    length: 1,
54};
55
56/// What kind of charging (if any) is taking place?
57#[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/// State of the UPS microcontroller's communications with an on-board chip.
86#[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/// Is USB-C power detected?
103#[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/// Was USB-C power delivery negotiated (`FastCharging`) or not (`StandardCharging`)?
120#[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/// Is the UPS charging or not?
137#[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}