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/// What kind of charging (if any) is taking place?
45#[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/// Is USB-C power detected?
74#[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/// Was USB-C power delivery negotiated (`FastCharging`) or not (`StandardCharging`)?
91#[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/// Is the UPS charging or not?
108#[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}