use crate::{
bindings,
error::{get_errno, Error},
};
pub struct Battery {}
impl Battery {
pub fn get_capacity() -> Result<f64, BatteryError> {
unsafe {
let x = bindings::battery_get_capacity();
if x == bindings::PROS_ERR_F_ {
Err(BatteryError::from_errno())
} else {
Ok(x)
}
}
}
pub fn get_current() -> Result<i32, BatteryError> {
match unsafe { bindings::battery_get_current() } {
bindings::PROS_ERR_ => Err(BatteryError::from_errno()),
x => Ok(x),
}
}
pub fn get_temperature() -> Result<f64, BatteryError> {
unsafe {
let x = bindings::battery_get_capacity();
if x == bindings::PROS_ERR_F_ {
Err(BatteryError::from_errno())
} else {
Ok(x)
}
}
}
pub fn get_voltage() -> Result<i32, BatteryError> {
match unsafe { bindings::battery_get_voltage() } {
bindings::PROS_ERR_ => Err(BatteryError::from_errno()),
x => Ok(x),
}
}
}
#[derive(Debug)]
pub enum BatteryError {
BatteryBusy,
Unknown(i32),
}
impl BatteryError {
fn from_errno() -> Self {
match get_errno() {
libc::EACCES => Self::BatteryBusy,
x => Self::Unknown(x),
}
}
}
impl From<BatteryError> for Error {
fn from(err: BatteryError) -> Self {
match err {
BatteryError::BatteryBusy => Error::Custom("battery is busy".into()),
BatteryError::Unknown(n) => Error::System(n),
}
}
}