Skip to content

Commit

Permalink
Fix possible exception in battery fully charged #3317
Browse files Browse the repository at this point in the history
  • Loading branch information
seerge committed Oct 28, 2024
1 parent e6a547d commit 083e918
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 20 additions & 5 deletions app/Battery/BatteryControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,43 @@ namespace GHelper.Battery
internal class BatteryControl
{

static bool _chargeFull = AppConfig.Is("charge_full");
public static bool chargeFull
{
get
{
return _chargeFull;
}
set
{
AppConfig.Set("charge_full", value ? 1 : 0);
_chargeFull = value;
}
}

public static void ToggleBatteryLimitFull()
{
if (AppConfig.Is("charge_full")) SetBatteryChargeLimit();
if (chargeFull) SetBatteryChargeLimit();
else SetBatteryLimitFull();
}

public static void SetBatteryLimitFull()
{
AppConfig.Set("charge_full", 1);
chargeFull = true;
Program.acpi.DeviceSet(AsusACPI.BatteryLimit, 100, "BatteryLimit");
Program.settingsForm.VisualiseBatteryFull();
}

public static void UnSetBatteryLimitFull()
{
AppConfig.Set("charge_full", 0);
chargeFull = false;
Logger.WriteLine("Battery fully charged");
Program.settingsForm.Invoke(Program.settingsForm.VisualiseBatteryFull);
}

public static void AutoBattery(bool init = false)
{
if (AppConfig.Is("charge_full") && !init) SetBatteryLimitFull();
if (chargeFull && !init) SetBatteryLimitFull();
else SetBatteryChargeLimit();
}

Expand All @@ -46,7 +61,7 @@ public static void SetBatteryChargeLimit(int limit = -1)
Program.acpi.DeviceSet(AsusACPI.BatteryLimit, limit, "BatteryLimit");

AppConfig.Set("charge_limit", limit);
AppConfig.Set("charge_full", 0);
chargeFull = false;

Program.settingsForm.VisualiseBattery(limit);
}
Expand Down
4 changes: 1 addition & 3 deletions app/HardwareControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static void ReadSensors()
if (fullCapacity > 0 && chargeCapacity > 0)
{
batteryCapacity = Math.Min(100, (decimal)chargeCapacity / (decimal)fullCapacity * 100);
if (batteryCapacity > 99) BatteryControl.UnSetBatteryLimitFull();
if (batteryCapacity > 99 && BatteryControl.chargeFull) BatteryControl.UnSetBatteryLimitFull();
if (chargeWatt)
{
batteryCharge = Math.Round((decimal)chargeCapacity / 1000, 1).ToString() + "Wh";
Expand All @@ -251,8 +251,6 @@ public static void ReadSensors()
batteryCharge = Math.Round(batteryCapacity, 1) + "%";
}
}


}

public static bool IsUsedGPU(int threshold = 10)
Expand Down

0 comments on commit 083e918

Please sign in to comment.