Skip to content

Commit

Permalink
PowerControl: Add Charge Limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ayufan committed Dec 14, 2023
1 parent b47c5fa commit 874cf6f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
27 changes: 25 additions & 2 deletions CommonHelpers/Vlv0100.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Vlv0100 : IDisposable
static IntPtr PDFV = new IntPtr(0xFE700C00 + 0x4C);
static IntPtr XBID = new IntPtr(0xFE700300 + 0xBD);
static IntPtr PDCT = new IntPtr(0xFE700C00 + 0x01);
static IntPtr MCBL = new IntPtr(0xFE700B00 + 0x9F);
static ushort IO6C = 0x6C;

public const ushort MAX_FAN_RPM = 0x1C84;
Expand All @@ -26,6 +27,7 @@ public struct DeviceVersion
public byte PDCS { get; set; }

public bool BatteryTempLE { get; set; }
public bool MaxBatteryCharge { get; set; }

public bool IsSupported(ushort deviceFirmware, byte deviceBoardID, byte devicePDCS)
{
Expand All @@ -42,11 +44,11 @@ public bool IsSupported(ushort deviceFirmware, byte deviceBoardID, byte devicePD
private static readonly DeviceVersion[] deviceVersions = {
// Steam Deck - LCD version
new DeviceVersion() { Firmware = 0xB030, BoardID = 0x6, PDCS = 0 /* 0x2B */, BatteryTempLE = false },
new DeviceVersion() { Firmware = 0xB030, BoardID = 0xA, PDCS = 0 /* 0x2B */, BatteryTempLE = false },
new DeviceVersion() { Firmware = 0xB030, BoardID = 0xA, PDCS = 0 /* 0x2B */, BatteryTempLE = false, MaxBatteryCharge = true },

// Steam Deck - OLED version
// new DeviceVersion() { Firmware = 0x1030, BoardID = 0x5, PDCS = 0 /* 0x2F */, BatteryTempLE = true },
new DeviceVersion() { Firmware = 0x1050, BoardID = 0x5, PDCS = 0 /* 0x2F */, BatteryTempLE = true }
new DeviceVersion() { Firmware = 0x1050, BoardID = 0x5, PDCS = 0 /* 0x2F */, BatteryTempLE = true, MaxBatteryCharge = true }
};

public static Vlv0100 Instance = new Vlv0100();
Expand Down Expand Up @@ -178,6 +180,27 @@ public float GetBattTemperature()
return (float)(value - 0x0AAC) / 10.0f;
}

public int? GetMaxBatteryCharge()
{
if (SupportedDevice?.MaxBatteryCharge != true)
return null;
var data = inpOut?.ReadMemory(MCBL, 1);
if (data is null)
return null;
if (data[0] > 100)
return null;
return data[0];
}
public void SetMaxBatteryCharge(int chargeLimit)
{
if (SupportedDevice?.MaxBatteryCharge != true)
return;
if (chargeLimit < 0 || chargeLimit > 100)
return;
byte[] data = BitConverter.GetBytes(chargeLimit);
inpOut?.WriteMemory(MCBL, data);
}

private void SetGain(ushort gain)
{
byte[] data = BitConverter.GetBytes(gain);
Expand Down
3 changes: 2 additions & 1 deletion PowerControl/MenuStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal class MenuStack
Options.PerformanceOverlay.ModeInstance,
Options.PerformanceOverlay.KernelDriversInstance,
Options.FanControl.Instance,
Options.SteamController.Instance
Options.SteamController.Instance,
Options.BatteryChargeLimit.Instance
}
};

Expand Down
32 changes: 32 additions & 0 deletions PowerControl/Options/BatteryChargeLimit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using CommonHelpers;

namespace PowerControl.Options
{
public static class BatteryChargeLimit
{
public static Menu.MenuItemWithOptions Instance = new Menu.MenuItemWithOptions()
{
Name = "Charge Limit",
ApplyDelay = 1000,
Options = { "70%", "80%", "90%", "100%" },
ActiveOption = "?",
ApplyValue = (selected) =>
{
var value = int.Parse(selected.ToString().TrimEnd('%'));

using (var vlv0100 = new Vlv0100())
{
if (!vlv0100.Open())
return null;

vlv0100.SetMaxBatteryCharge(value);

var newValue = vlv0100.GetMaxBatteryCharge();
if (newValue is null)
return null;
return newValue.ToString() + "%";
}
}
};
}
}
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## #{GIT_TAG_NAME}

- PowerControl: Add Charge Limit (70%, 80%, 90%, 100%)

## 0.7.1

- SteamDeck OLED: Support BIOS 107 with temperature readings
Expand Down

0 comments on commit 874cf6f

Please sign in to comment.