use crate::error::Error;
pub(crate) struct RegisterBlock {
pub(crate) id: u8,
pub(crate) length: u8,
}
pub(crate) const CHARGING_REG: RegisterBlock = RegisterBlock {
id: 0x02,
length: 1,
};
pub(crate) const COMMUNICATION_REG: RegisterBlock = RegisterBlock {
id: 0x03,
length: 1,
};
pub(crate) const USBC_VBUS_REG: RegisterBlock = RegisterBlock {
id: 0x10,
length: 6,
};
pub(crate) const BATTERY_REG: RegisterBlock = RegisterBlock {
id: 0x20,
length: 12,
};
pub(crate) const CELL_VOLTAGE_REG: RegisterBlock = RegisterBlock {
id: 0x30,
length: 8,
};
pub (crate) const POWEROFF_REG: RegisterBlock = RegisterBlock {
id: 0x01,
length: 1,
};
pub (crate) const SOFTWARE_REV_REG: RegisterBlock = RegisterBlock {
id: 0x50,
length: 1,
};
#[derive(Debug)]
pub enum ChargerActivity {
Standby = 0b000,
Trickle = 0b001,
ConstantCurrent = 0b010,
ConstantVoltage = 0b011,
Pending = 0b100,
Full = 0b101,
Timeout = 0b110,
}
impl TryFrom<u8> for ChargerActivity {
type Error = Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0b000 => Ok(ChargerActivity::Standby),
0b001 => Ok(ChargerActivity::Trickle),
0b010 => Ok(ChargerActivity::ConstantCurrent),
0b011 => Ok(ChargerActivity::ConstantVoltage),
0b100 => Ok(ChargerActivity::Pending),
0b101 => Ok(ChargerActivity::Full),
0b110 => Ok(ChargerActivity::Timeout),
_ => Err(Error::InvalidChargerActivity(value)),
}
}
}
#[derive(Debug)]
pub enum CommState {
Error = 0b0,
Normal = 0b1,
}
impl From<bool> for CommState {
fn from(value: bool) -> Self {
if value {
CommState::Normal
} else {
CommState::Error
}
}
}
#[derive(Debug)]
pub enum UsbCInputState {
NoPower = 0b0,
Powered = 0b1,
}
impl From<bool> for UsbCInputState {
fn from(value: bool) -> Self {
if value {
UsbCInputState::Powered
} else {
UsbCInputState::NoPower
}
}
}
#[derive(Debug)]
pub enum UsbCPowerDelivery {
StandardCharging = 0b0,
FastCharging = 0b1,
}
impl From<bool> for UsbCPowerDelivery {
fn from(value: bool) -> Self {
if value {
UsbCPowerDelivery::FastCharging
} else {
UsbCPowerDelivery::StandardCharging
}
}
}
#[derive(Debug)]
pub enum ChargingState {
NotCharging = 0b0,
Charging = 0b1,
}
impl From<bool> for ChargingState {
fn from(value: bool) -> Self {
if value {
ChargingState::Charging
} else {
ChargingState::NotCharging
}
}
}