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 cargo build
mkdir -p ../bin mkdir -p ../bin
#cp ./target/x86_64-unknown-linux-musl/release/powertools ../bin/backend #cp --preserve=mode ./target/x86_64-unknown-linux-musl/release/powertools ../bin/backend
#cp ./target/x86_64-unknown-linux-musl/debug/powertools ../bin/backend #cp --preserve=mode ./target/x86_64-unknown-linux-musl/debug/powertools ../bin/backend
#cp ./target/debug/powertools ../bin/backend #cp --preserve=mode ./target/debug/powertools ../bin/backend
cp ./target/debug/powertools ../bin/backend cp --preserve=mode ./target/debug/powertools ../bin/backend

View file

@ -129,6 +129,10 @@ impl TBattery for Battery {
None None
} }
fn read_charge_power(&self) -> Option<f64> {
None
}
fn charge_limit(&mut self, _limit: Option<f64>) {} fn charge_limit(&mut self, _limit: Option<f64>) {}
fn get_charge_limit(&self) -> 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> { pub fn read_charge_now() -> Result<f64, SettingError> {
match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CHARGE_NOW_PATH) { match usdpl_back::api::files::read_single::<_, u64, _>(BATTERY_CHARGE_NOW_PATH) {
Err(e) => Err(SettingError { 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>) { fn charge_limit(&mut self, limit: Option<f64>) {
// upper limit // upper limit
let index = self.find_limit_event(); 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_current_now(&self) -> Option<f64>;
fn read_charge_power(&self) -> Option<f64>;
fn charge_limit(&mut self, limit: Option<f64>); fn charge_limit(&mut self, limit: Option<f64>);
fn get_charge_limit(&self) -> Option<f64>; fn get_charge_limit(&self) -> Option<f64>;

View file

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

Binary file not shown.

Binary file not shown.