From 46659a474d66bfc22f0082c8c7003de67a3372b1 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 9 Oct 2023 11:19:29 -0400 Subject: [PATCH] Unset battery charge limit properly, fix #127 --- backend/limits_core/src/json/base.rs | 2 +- backend/src/settings/steam_deck/battery.rs | 16 ++++++++++++++++ backend/src/state/steam_deck/battery.rs | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/limits_core/src/json/base.rs b/backend/limits_core/src/json/base.rs index 803d8bd..c08372a 100644 --- a/backend/limits_core/src/json/base.rs +++ b/backend/limits_core/src/json/base.rs @@ -174,7 +174,7 @@ impl Default for Base { super::DeveloperMessage { id: 1, title: "Welcome".to_owned(), - body: "Thanks for installing PowerTools! For more information, please check the wiki. For bugs and requests, please create an issue on GitHub.".to_owned(), + body: "Thanks for installing PowerTools! For more information, please check the wiki. For bugs and requests, please create an issue.".to_owned(), url: Some("https://git.ngni.us/NG-SD-Plugins/PowerTools/wiki".to_owned()), } ], diff --git a/backend/src/settings/steam_deck/battery.rs b/backend/src/settings/steam_deck/battery.rs index d03bfd6..548a656 100644 --- a/backend/src/settings/steam_deck/battery.rs +++ b/backend/src/settings/steam_deck/battery.rs @@ -586,6 +586,7 @@ impl OnPowerEvent for Battery { .unwrap_or_else(|mut e| errors.append(&mut e)); let attr_exists = MAX_BATTERY_CHARGE_LEVEL_ATTR.exists(&*self.sysfs_hwmon); log::info!("Does battery limit attribute (max_battery_charge_level) exist? {}", attr_exists); + let mut charge_limit_set_now = false; for ev in &mut self.events { if attr_exists { if let EventTrigger::BatteryAbove(level) = ev.trigger { @@ -597,12 +598,27 @@ impl OnPowerEvent for Battery { setting: crate::settings::SettingVariant::Battery, } )); + self.state.charge_limit_set = true; + charge_limit_set_now = true; } } } ev.on_power_event(new_mode) .unwrap_or_else(|mut e| errors.append(&mut e)); } + if self.state.charge_limit_set != charge_limit_set_now { + // only true when charge_limit_set is false and self.state.charge_limit_set is true + self.state.charge_limit_set = false; + if attr_exists { + self.sysfs_hwmon.set(MAX_BATTERY_CHARGE_LEVEL_ATTR, 100) + .unwrap_or_else(|e| errors.push( + SettingError { + msg: format!("Failed to reset (write to) {:?}: {}", MAX_BATTERY_CHARGE_LEVEL_ATTR, e), + setting: crate::settings::SettingVariant::Battery, + } + )); + } + } if errors.is_empty() { Ok(()) } else { diff --git a/backend/src/state/steam_deck/battery.rs b/backend/src/state/steam_deck/battery.rs index 3c66e36..19ce82f 100644 --- a/backend/src/state/steam_deck/battery.rs +++ b/backend/src/state/steam_deck/battery.rs @@ -3,6 +3,7 @@ pub struct Battery { pub charge_rate_set: bool, pub charge_mode_set: bool, pub charger_state: ChargeState, + pub charge_limit_set: bool, } impl std::default::Default for Battery { @@ -11,6 +12,7 @@ impl std::default::Default for Battery { charge_rate_set: true, charge_mode_set: true, charger_state: ChargeState::Unknown, + charge_limit_set: true, } } }