steam-deck-tools/FanControl/FanController.cs

164 lines
4.7 KiB
C#
Raw Permalink Normal View History

using LibreHardwareMonitor.Hardware;
using LibreHardwareMonitor.Hardware.CPU;
using Microsoft.VisualBasic.Devices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonHelpers;
namespace FanControl
{
[TypeConverter(typeof(ExpandableObjectConverter))]
2022-11-12 17:06:34 +01:00
[RefreshProperties(RefreshProperties.Repaint)]
internal partial class FanController : IDisposable
{
[CategoryAttribute("Fan")]
public FanMode Mode { get; private set; } = FanMode.Default;
[CategoryAttribute("Fan")]
public bool KernelDriversLoaded
{
get => Instance.UseKernelDrivers;
}
[CategoryAttribute("Fan")]
2022-11-12 17:06:34 +01:00
[NotifyParentProperty(true)]
public ushort? CurrentRPM { get; private set; }
[CategoryAttribute("Fan")]
2022-11-12 17:06:34 +01:00
[NotifyParentProperty(true)]
public ushort DesiredRPM { get; private set; }
[CategoryAttribute("Board")]
public String PDVersion { get; private set; } = Vlv0100.Instance.FirmwareVersion.ToString("X");
public FanController()
{
}
private void visitHardware(IHardware hardware)
{
Dictionary<FanSensor, ISensor> matched = new Dictionary<FanSensor, ISensor>();
foreach (ISensor hwSensor in hardware.Sensors)
{
foreach (var sensor in allSensors.Values)
{
if (sensor.Matches(hwSensor))
matched[sensor] = hwSensor;
}
}
if (matched.Any())
{
hardware.Update();
foreach (var sensor in matched)
sensor.Key.Update(sensor.Value, Mode);
}
foreach (IHardware subhardware in hardware.SubHardware)
{
visitHardware(subhardware);
}
}
private ushort getDesiredRPM()
{
ushort rpm = 0;
foreach (var sensor in allSensors.Values)
if (sensor.CalculatedRPM.HasValue)
rpm = Math.Max(rpm, sensor.CalculatedRPM.Value);
return rpm;
}
[Browsable(false)]
public bool IsActive
{
get { return Vlv0100.Instance.IsOpen; }
}
public void Update(bool showForDefault = false)
{
var mutex = Instance.WaitGlobalMutex(200);
if (mutex is null)
{
// If we cannot acquire mutex slightly increase FAN to compensate just in case
Vlv0100.Instance.SetFanDesiredRPM((ushort)(Vlv0100.Instance.GetFanDesiredRPM() * 110 / 100));
return;
}
try
{
2023-01-12 22:58:40 +01:00
if (AntiCheatSettings.Default.NotYet || Mode == FanMode.Default && !showForDefault)
{
Instance.UseKernelDrivers = false;
CurrentRPM = null;
DesiredRPM = 0;
foreach (var sensor in allSensors.Values)
sensor.Reset();
return;
}
else if (!Vlv0100.Instance.IsOpen)
{
Instance.UseKernelDrivers = true;
SetMode(Mode);
}
foreach (var sensor in allSensors.Values)
sensor.Reset();
foreach (var hardware in Instance.HardwareComputer.Hardware)
visitHardware(hardware);
}
finally
{
mutex.ReleaseMutex();
}
allSensors["Batt"].Update("VLV0100", Vlv0100.Instance.GetBattTemperature(), Mode);
Vlv0100.Instance.SetFanDesiredRPM(getDesiredRPM());
CurrentRPM = Vlv0100.Instance.GetFanRPM();
DesiredRPM = Vlv0100.Instance.GetFanDesiredRPM();
}
public void SetMode(FanMode mode)
{
switch (mode)
{
case FanMode.Default:
Vlv0100.Instance.SetFanControl(false);
break;
default:
Instance.UseKernelDrivers = true;
Vlv0100.Instance.SetFanControl(true);
break;
}
this.Mode = mode;
}
public bool IsAnyInvalid()
{
foreach (var sensor in allSensors.Values)
{
if (!sensor.IsValid(Mode))
return true;
}
return false;
}
public void Dispose()
{
}
}
}