From def9012056c3e98963833e8ca03518e37ffb4a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Sun, 13 Nov 2022 07:51:47 +0100 Subject: [PATCH] Restore fan mode on resume --- FanControl/FanControlForm.cs | 17 +++++++++++++++++ FanControl/FanControllerSensors.cs | 1 + FanControl/FanSensor.cs | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/FanControl/FanControlForm.cs b/FanControl/FanControlForm.cs index 88c3ed5..f616e09 100644 --- a/FanControl/FanControlForm.cs +++ b/FanControl/FanControlForm.cs @@ -50,6 +50,23 @@ namespace FanControl } notifyIcon.ShowBalloonTip(3000, Text, "Fan Control Started", ToolTipIcon.Info); + + Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; + } + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + + Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; + } + + private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e) + { + // Restore fan mode on resume + if (e.Mode == Microsoft.Win32.PowerModes.Resume) + { + fanControl.SetMode(fanControl.Mode); + } } private void setFanMode(FanController.FanMode mode) diff --git a/FanControl/FanControllerSensors.cs b/FanControl/FanControllerSensors.cs index 53bcd8b..e7959dd 100644 --- a/FanControl/FanControllerSensors.cs +++ b/FanControl/FanControllerSensors.cs @@ -22,6 +22,7 @@ namespace FanControl SensorType = SensorType.Power, ValueDeadZone = 0.1f, AvgSamples = 20, + MaxValue = 25, // TODO: On resume a bogus value is returned Profiles = new Dictionary() { { diff --git a/FanControl/FanSensor.cs b/FanControl/FanSensor.cs index bdf3a4e..3b6bcec 100644 --- a/FanControl/FanSensor.cs +++ b/FanControl/FanSensor.cs @@ -18,6 +18,7 @@ namespace FanControl public float ValueDeadZone { get; set; } public int AvgSamples { get; set; } = 5; + public float? MaxValue { get; set; } internal string HardwareName { get; set; } = ""; internal HardwareType HardwareType { get; set; } @@ -153,6 +154,9 @@ namespace FanControl if (!newValue.HasValue || newValue <= 0.0) return false; + if (MaxValue.HasValue) + newValue = Math.Min(newValue.Value, MaxValue.Value); + if (AllSamples.Count == 0 || Math.Abs(AllSamples.Last() - newValue.Value) >= ValueDeadZone) { AllSamples.Add(newValue.Value);