feat: One-shot full charge charge

This commit is contained in:
AlbertGG
2024-12-14 22:07:46 +00:00
committed by Luke Jones
parent e7c4619ee9
commit 72ff1ab3ab
5 changed files with 81 additions and 3 deletions

View File

@@ -22,6 +22,8 @@ pub struct CliStart {
pub prev_kbd_bright: bool, pub prev_kbd_bright: bool,
#[options(meta = "", help = "Set your battery charge limit <20-100>")] #[options(meta = "", help = "Set your battery charge limit <20-100>")]
pub chg_limit: Option<u8>, pub chg_limit: Option<u8>,
#[options(help = "Toggle one-shot battery charge to 100%")]
pub one_shot_chg: bool,
#[options(command)] #[options(command)]
pub command: Option<CliCommand>, pub command: Option<CliCommand>,
} }

View File

@@ -185,7 +185,8 @@ fn do_parsed(
&& parsed.kbd_bright.is_none() && parsed.kbd_bright.is_none()
&& parsed.chg_limit.is_none() && parsed.chg_limit.is_none()
&& !parsed.next_kbd_bright && !parsed.next_kbd_bright
&& !parsed.prev_kbd_bright) && !parsed.prev_kbd_bright
&& !parsed.one_shot_chg)
|| parsed.help || parsed.help
{ {
println!("{}", CliStart::usage()); println!("{}", CliStart::usage());
@@ -314,6 +315,11 @@ fn do_parsed(
proxy.set_charge_control_end_threshold(chg_limit)?; proxy.set_charge_control_end_threshold(chg_limit)?;
} }
if parsed.one_shot_chg {
let proxy = PlatformProxyBlocking::new(&conn)?;
proxy.one_shot_full_charge()?;
}
Ok(()) Ok(())
} }

View File

@@ -7,8 +7,11 @@ const CONFIG_FILE: &str = "asusd.ron";
#[derive(Deserialize, Serialize, Debug, PartialEq, PartialOrd)] #[derive(Deserialize, Serialize, Debug, PartialEq, PartialOrd)]
pub struct Config { pub struct Config {
/// Save charge limit for restoring on boot/resume // The current charge limit applied
pub charge_control_end_threshold: u8, pub charge_control_end_threshold: u8,
/// Save charge limit for restoring
#[serde(skip)]
pub base_charge_control_end_threshold: u8,
pub panel_od: bool, pub panel_od: bool,
pub boot_sound: bool, pub boot_sound: bool,
pub mini_led_mode: bool, pub mini_led_mode: bool,
@@ -64,6 +67,7 @@ impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
charge_control_end_threshold: 100, charge_control_end_threshold: 100,
base_charge_control_end_threshold: 100,
panel_od: false, panel_od: false,
boot_sound: false, boot_sound: false,
mini_led_mode: false, mini_led_mode: false,
@@ -116,7 +120,7 @@ impl StdConfigLoad1<Config507> for Config {}
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct Config507 { pub struct Config507 {
/// Save charge limit for restoring on boot // The current charge limit applied
pub charge_control_end_threshold: u8, pub charge_control_end_threshold: u8,
pub panel_od: bool, pub panel_od: bool,
pub mini_led_mode: bool, pub mini_led_mode: bool,
@@ -139,7 +143,9 @@ pub struct Config507 {
impl From<Config507> for Config { impl From<Config507> for Config {
fn from(c: Config507) -> Self { fn from(c: Config507) -> Self {
Self { Self {
// Restore the base charge limit
charge_control_end_threshold: c.charge_control_end_threshold, charge_control_end_threshold: c.charge_control_end_threshold,
base_charge_control_end_threshold: c.charge_control_end_threshold,
panel_od: c.panel_od, panel_od: c.panel_od,
boot_sound: false, boot_sound: false,
disable_nvidia_powerd_on_battery: c.disable_nvidia_powerd_on_battery, disable_nvidia_powerd_on_battery: c.disable_nvidia_powerd_on_battery,

View File

@@ -181,6 +181,24 @@ impl CtrlPlatform {
Ok(()) Ok(())
} }
async fn restore_charge_limit(&self) {
let limit = self.config.lock().await.base_charge_control_end_threshold;
if limit > 0
&& std::mem::replace(
&mut self.config.lock().await.charge_control_end_threshold,
limit,
) != limit
{
self.power
.set_charge_control_end_threshold(limit)
.map_err(|e| {
error!("Couldn't restore charge limit: {e}");
})
.ok();
self.config.lock().await.write();
}
}
async fn run_ac_or_bat_cmd(&self, power_plugged: bool) { async fn run_ac_or_bat_cmd(&self, power_plugged: bool) {
let prog: Vec<String> = if power_plugged { let prog: Vec<String> = if power_plugged {
// AC ONLINE // AC ONLINE
@@ -353,10 +371,24 @@ impl CtrlPlatform {
} }
self.power.set_charge_control_end_threshold(limit)?; self.power.set_charge_control_end_threshold(limit)?;
self.config.lock().await.charge_control_end_threshold = limit; self.config.lock().await.charge_control_end_threshold = limit;
self.config.lock().await.base_charge_control_end_threshold = limit;
self.config.lock().await.write(); self.config.lock().await.write();
Ok(()) Ok(())
} }
async fn one_shot_full_charge(&self) -> Result<(), FdoErr> {
let base_limit = std::mem::replace(
&mut self.config.lock().await.charge_control_end_threshold,
100,
);
if base_limit != 100 {
self.power.set_charge_control_end_threshold(100)?;
self.config.lock().await.base_charge_control_end_threshold = base_limit;
self.config.lock().await.write();
}
Ok(())
}
#[zbus(property)] #[zbus(property)]
fn gpu_mux_mode(&self) -> Result<u8, FdoErr> { fn gpu_mux_mode(&self) -> Result<u8, FdoErr> {
self.platform.get_gpu_mux_mode().map_err(|err| { self.platform.get_gpu_mux_mode().map_err(|err| {
@@ -723,12 +755,17 @@ impl ReloadAndNotify for CtrlPlatform {
if *config != data { if *config != data {
info!("asusd.ron updated externally, reloading and updating internal copy"); info!("asusd.ron updated externally, reloading and updating internal copy");
let mut base_charge_control_end_threshold = None;
if self.power.has_charge_control_end_threshold() { if self.power.has_charge_control_end_threshold() {
let limit = data.charge_control_end_threshold; let limit = data.charge_control_end_threshold;
warn!("setting charge_control_end_threshold to {limit}"); warn!("setting charge_control_end_threshold to {limit}");
self.power.set_charge_control_end_threshold(limit)?; self.power.set_charge_control_end_threshold(limit)?;
self.charge_control_end_threshold_changed(signal_context) self.charge_control_end_threshold_changed(signal_context)
.await?; .await?;
base_charge_control_end_threshold = (config.base_charge_control_end_threshold > 0)
.then_some(config.base_charge_control_end_threshold)
.or(Some(limit));
} }
if self.platform.has_throttle_thermal_policy() if self.platform.has_throttle_thermal_policy()
@@ -777,6 +814,8 @@ impl ReloadAndNotify for CtrlPlatform {
ppt_reload_and_notify!(nv_temp_target, "nv_temp_target"); ppt_reload_and_notify!(nv_temp_target, "nv_temp_target");
*config = data; *config = data;
config.base_charge_control_end_threshold =
base_charge_control_end_threshold.unwrap_or_default();
} }
Ok(()) Ok(())
@@ -787,6 +826,7 @@ impl crate::Reloadable for CtrlPlatform {
async fn reload(&mut self) -> Result<(), RogError> { async fn reload(&mut self) -> Result<(), RogError> {
info!("Begin Platform settings restore"); info!("Begin Platform settings restore");
if self.power.has_charge_control_end_threshold() { if self.power.has_charge_control_end_threshold() {
// self.restore_charge_limit().await;
let limit = self.config.lock().await.charge_control_end_threshold; let limit = self.config.lock().await.charge_control_end_threshold;
info!("reloading charge_control_end_threshold to {limit}"); info!("reloading charge_control_end_threshold to {limit}");
self.power.set_charge_control_end_threshold(limit)?; self.power.set_charge_control_end_threshold(limit)?;
@@ -947,6 +987,23 @@ impl CtrlTask for CtrlPlatform {
}) })
.ok(); .ok();
} }
if shutting_down
&& platform2.power.has_charge_control_end_threshold()
&& lock.base_charge_control_end_threshold > 0
{
info!("RogPlatform restoring charge_control_end_threshold");
platform2
.power
.set_charge_control_end_threshold(
lock.base_charge_control_end_threshold,
)
.map_err(|err| {
warn!("CtrlCharge: charge_control_end_threshold {}", err);
err
})
.ok();
}
} }
}, },
move |_lid_closed| { move |_lid_closed| {
@@ -964,6 +1021,10 @@ impl CtrlTask for CtrlPlatform {
.await; .await;
} }
platform3.run_ac_or_bat_cmd(power_plugged).await; platform3.run_ac_or_bat_cmd(power_plugged).await;
// In case one-shot charge was used, restore the old charge limit
if platform3.power.has_charge_control_end_threshold() && !power_plugged {
platform3.restore_charge_limit().await;
}
} }
}, },
) )

View File

@@ -45,6 +45,9 @@ pub trait Platform {
#[zbus(property)] #[zbus(property)]
fn set_charge_control_end_threshold(&self, value: u8) -> zbus::Result<()>; fn set_charge_control_end_threshold(&self, value: u8) -> zbus::Result<()>;
// Toggle one-shot charge to 100%
fn one_shot_full_charge(&self) -> zbus::Result<()>;
/// DgpuDisable property /// DgpuDisable property
#[zbus(property)] #[zbus(property)]
fn dgpu_disable(&self) -> zbus::Result<bool>; fn dgpu_disable(&self) -> zbus::Result<bool>;