Skip to content

Commit

Permalink
CommonHelpers: Vlv0100: Use Instance instead of static.
Browse files Browse the repository at this point in the history
  • Loading branch information
ayufan committed Dec 14, 2023
1 parent 943cfd9 commit b47c5fa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
12 changes: 6 additions & 6 deletions CommonHelpers/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public static bool UseKernelDrivers
useKernelDrivers = value;

if (value)
Vlv0100.Open();
Vlv0100.Instance.Open();
else
Vlv0100.Close();
Vlv0100.Instance.Close();

// CPU requires reading RyzenSMU
HardwareComputer.IsCpuEnabled = value;
Expand Down Expand Up @@ -140,13 +140,13 @@ public static void Open(String title, bool useKernelDrivers, String? runOnce = n
{
UseKernelDrivers = useKernelDrivers;

if (Vlv0100.IsOpen && !Vlv0100.IsSupported)
if (Vlv0100.Instance.IsOpen && !Vlv0100.Instance.IsSupported)
{
String message = "";
message += "Current device is not supported.\n";
message += "FirmwareVersion: " + Vlv0100.FirmwareVersion.ToString("X") + "\n";
message += "BoardID: " + Vlv0100.BoardID.ToString("X") + "\n";
message += "PDCS: " + Vlv0100.PDCS.ToString("X") + "\n";
message += "FirmwareVersion: " + Vlv0100.Instance.FirmwareVersion.ToString("X") + "\n";
message += "BoardID: " + Vlv0100.Instance.BoardID.ToString("X") + "\n";
message += "PDCS: " + Vlv0100.Instance.PDCS.ToString("X") + "\n";

Fatal(title, message);
}
Expand Down
52 changes: 32 additions & 20 deletions CommonHelpers/Vlv0100.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CommonHelpers
{
public class Vlv0100
public class Vlv0100 : IDisposable
{
// Those addresses are taken from DSDT for VLV0100
// and might change at any time with a BIOS update
Expand Down Expand Up @@ -49,28 +49,41 @@ public bool IsSupported(ushort deviceFirmware, byte deviceBoardID, byte devicePD
new DeviceVersion() { Firmware = 0x1050, BoardID = 0x5, PDCS = 0 /* 0x2F */, BatteryTempLE = true }
};

private static InpOut? inpOut;
public static Vlv0100 Instance = new Vlv0100();

public static bool IsOpen
~Vlv0100()
{
Close();
}

public void Dispose()
{
GC.SuppressFinalize(this);
Close();
}

private InpOut? inpOut;

public bool IsOpen
{
get { return inpOut is not null; }
}

public static DeviceVersion? SupportedDevice
public DeviceVersion? SupportedDevice
{
get { return deviceVersions.First((v) => v.IsSupported(FirmwareVersion, BoardID, PDCS)); }
get { return deviceVersions.FirstOrDefault((v) => v.IsSupported(FirmwareVersion, BoardID, PDCS)); }
}

public static bool IsSupported
public bool IsSupported
{
get { return SupportedDevice is not null; }
}

public static ushort FirmwareVersion { get; private set; }
public static byte BoardID { get; private set; }
public static byte PDCS { get; private set; }
public ushort FirmwareVersion { get; private set; }
public byte BoardID { get; private set; }
public byte PDCS { get; private set; }

public static bool Open()
public bool Open()
{
if (inpOut != null)
return true;
Expand All @@ -96,7 +109,6 @@ public static bool Open()
PDCS = data[0];
else
PDCS = 0xFF;

return true;
}
catch (Exception e)
Expand All @@ -107,38 +119,38 @@ public static bool Open()
}
}

public static void Close()
public void Close()
{
SetFanControl(false);
using (inpOut) { }
inpOut = null;
}

public static ushort GetFanDesiredRPM()
public ushort GetFanDesiredRPM()
{
var data = inpOut?.ReadMemory(FSLO_FSHI, 2);
if (data is null)
return 0;
return BitConverter.ToUInt16(data);
}

public static ushort? GetFanRPM()
public ushort? GetFanRPM()
{
var data = inpOut?.ReadMemory(FNRL_FNRH, 2);
if (data is null)
return null;
return BitConverter.ToUInt16(data);
}

public static void SetFanControl(Boolean userControlled)
public void SetFanControl(Boolean userControlled)
{
SetGain(10);
SetRampRate(userControlled ? (byte)10 : (byte)20);

inpOut?.DlPortWritePortUchar(IO6C, userControlled ? (byte)0xCC : (byte)0xCD);
}

public static void SetFanDesiredRPM(ushort rpm)
public void SetFanDesiredRPM(ushort rpm)
{
if (rpm > MAX_FAN_RPM)
rpm = MAX_FAN_RPM;
Expand All @@ -147,15 +159,15 @@ public static void SetFanDesiredRPM(ushort rpm)
inpOut?.WriteMemory(FSLO_FSHI, data);
}

public static bool GetFanCheck()
public bool GetFanCheck()
{
var data = inpOut?.ReadMemory(FNCK, 1);
if (data is null)
return false;
return (data[0] & 0x1) != 0;
}

public static float GetBattTemperature()
public float GetBattTemperature()
{
var data = inpOut?.ReadMemory(BATH_BATL, 2);
if (data is null)
Expand All @@ -166,12 +178,12 @@ public static float GetBattTemperature()
return (float)(value - 0x0AAC) / 10.0f;
}

private static void SetGain(ushort gain)
private void SetGain(ushort gain)
{
byte[] data = BitConverter.GetBytes(gain);
inpOut?.WriteMemory(GNLO_GNHI, data);
}
private static void SetRampRate(byte rampRate)
private void SetRampRate(byte rampRate)
{
byte[] data = BitConverter.GetBytes(rampRate);
inpOut?.WriteMemory(FRPR, data);
Expand Down
20 changes: 10 additions & 10 deletions FanControl/FanController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public bool KernelDriversLoaded
public ushort DesiredRPM { get; private set; }

[CategoryAttribute("Board")]
public String PDVersion { get; private set; } = Vlv0100.FirmwareVersion.ToString("X");
public String PDVersion { get; private set; } = Vlv0100.Instance.FirmwareVersion.ToString("X");

public FanController()
{
Expand Down Expand Up @@ -80,7 +80,7 @@ private ushort getDesiredRPM()
[Browsable(false)]
public bool IsActive
{
get { return Vlv0100.IsOpen; }
get { return Vlv0100.Instance.IsOpen; }
}

public void Update(bool showForDefault = false)
Expand All @@ -89,7 +89,7 @@ public void Update(bool showForDefault = false)
if (mutex is null)
{
// If we cannot acquire mutex slightly increase FAN to compensate just in case
Vlv0100.SetFanDesiredRPM((ushort)(Vlv0100.GetFanDesiredRPM() * 110 / 100));
Vlv0100.Instance.SetFanDesiredRPM((ushort)(Vlv0100.Instance.GetFanDesiredRPM() * 110 / 100));
return;
}

Expand All @@ -104,7 +104,7 @@ public void Update(bool showForDefault = false)
sensor.Reset();
return;
}
else if (!Vlv0100.IsOpen)
else if (!Vlv0100.Instance.IsOpen)
{
Instance.UseKernelDrivers = true;
SetMode(Mode);
Expand All @@ -121,25 +121,25 @@ public void Update(bool showForDefault = false)
mutex.ReleaseMutex();
}

allSensors["Batt"].Update("VLV0100", Vlv0100.GetBattTemperature(), Mode);
allSensors["Batt"].Update("VLV0100", Vlv0100.Instance.GetBattTemperature(), Mode);

Vlv0100.SetFanDesiredRPM(getDesiredRPM());
Vlv0100.Instance.SetFanDesiredRPM(getDesiredRPM());

CurrentRPM = Vlv0100.GetFanRPM();
DesiredRPM = Vlv0100.GetFanDesiredRPM();
CurrentRPM = Vlv0100.Instance.GetFanRPM();
DesiredRPM = Vlv0100.Instance.GetFanDesiredRPM();
}

public void SetMode(FanMode mode)
{
switch (mode)
{
case FanMode.Default:
Vlv0100.SetFanControl(false);
Vlv0100.Instance.SetFanControl(false);
break;

default:
Instance.UseKernelDrivers = true;
Vlv0100.SetFanControl(true);
Vlv0100.Instance.SetFanControl(true);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion PerformanceOverlay/Sensors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private IEnumerable<float> GetNumericValues(Sensors sensors)
{
Value = delegate ()
{
return CommonHelpers.Vlv0100.GetFanRPM();
return Vlv0100.Instance.GetFanRPM();
},
Format = "F0"
}
Expand Down

0 comments on commit b47c5fa

Please sign in to comment.