Add battery power API

This commit is contained in:
NGnius (Graham) 2023-06-10 16:27:13 -04:00
parent ebf74871ee
commit 4ac9303754
7 changed files with 30 additions and 4 deletions

View File

@ -6,7 +6,7 @@
cargo build
mkdir -p ../bin
#cp ./target/x86_64-unknown-linux-musl/release/powertools ../bin/backend
#cp ./target/x86_64-unknown-linux-musl/debug/powertools ../bin/backend
#cp ./target/debug/powertools ../bin/backend
cp ./target/debug/powertools ../bin/backend
#cp --preserve=mode ./target/x86_64-unknown-linux-musl/release/powertools ../bin/backend
#cp --preserve=mode ./target/x86_64-unknown-linux-musl/debug/powertools ../bin/backend
#cp --preserve=mode ./target/debug/powertools ../bin/backend
cp --preserve=mode ./target/debug/powertools ../bin/backend

View File

@ -129,6 +129,10 @@ impl TBattery for Battery {
None
}
fn read_charge_power(&self) -> Option<f64> {
None
}
fn charge_limit(&mut self, _limit: Option<f64>) {}
fn get_charge_limit(&self) -> Option<f64> {

View File

@ -320,6 +320,12 @@ impl Battery {
}
}
pub fn read_charge_power() -> Result<f64, SettingError> {
let current = Self::read_current_now()? as f64 / 1000.0; // mA -> A
let voltage = Self::read_usb_voltage()?;
Ok(current * voltage)
}
pub fn read_charge_now() -> Result<f64, SettingError> {
match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CHARGE_NOW_PATH) {
Err(e) => Err(SettingError {
@ -552,6 +558,16 @@ impl TBattery for Battery {
}
}
fn read_charge_power(&self) -> Option<f64> {
match Self::read_charge_power() {
Ok(x) => Some(x as f64),
Err(e) => {
log::warn!("read_current_now err: {}", e.msg);
None
}
}
}
fn charge_limit(&mut self, limit: Option<f64>) {
// upper limit
let index = self.find_limit_event();

View File

@ -129,6 +129,8 @@ pub trait TBattery: OnSet + OnResume + OnPowerEvent + Debug + Send {
fn read_current_now(&self) -> Option<f64>;
fn read_charge_power(&self) -> Option<f64>;
fn charge_limit(&mut self, limit: Option<f64>);
fn get_charge_limit(&self) -> Option<f64>;

View File

@ -75,6 +75,10 @@ impl TBattery for Battery {
None
}
fn read_charge_power(&self) -> Option<f64> {
None
}
fn charge_limit(&mut self, _limit: Option<f64>) {}
fn get_charge_limit(&self) -> Option<f64> {

Binary file not shown.

Binary file not shown.