From bc1e86d4eac84bbb86271fad9543a5e6c4640e19 Mon Sep 17 00:00:00 2001 From: jack Date: Thu, 2 Apr 2026 18:32:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 26 +- ChromeboxFanControl.sln | 58 +- ChromeboxFanControl/AppConfig.cs | 272 ++-- .../ChromeboxFanControl.csproj | 64 +- ChromeboxFanControl/CpuTempReader.cs | 204 +-- ChromeboxFanControl/EctoolRunner.cs | 230 ++-- ChromeboxFanControl/FanController.cs | 592 ++++----- ChromeboxFanControl/FanCurve.cs | 82 +- ChromeboxFanControl/FanSampleEventArgs.cs | 30 +- ChromeboxFanControl/MainForm.cs | 1172 ++++++++--------- ChromeboxFanControl/Program.cs | 54 +- .../Properties/Resources.Designer.cs | 130 +- ChromeboxFanControl/Properties/Resources.resx | 238 ++-- .../Properties/Resources.zh-Hans.resx | 238 ++-- .../Properties/Resources.zh-Hant.resx | 238 ++-- ChromeboxFanControl/app.manifest | 32 +- ChromeboxFanControl/appsettings.json | 34 +- .../ChromeboxFanControlService.csproj | 54 +- .../FanControlWorker.cs | 86 +- ChromeboxFanControlService/Program.cs | 32 +- ChromeboxFanControlService/appsettings.json | 30 +- .../ChromeboxECControlSensor.cs | 82 +- FanControl.ChromeboxEC/ChromeboxECPlugin.cs | 82 +- .../ChromeboxECTempSensor.cs | 54 +- FanControl.ChromeboxEC/EctoolRunner.cs | 27 +- .../FanControl.ChromeboxEC.csproj | 36 +- .../FanControl.ChromeboxEC.json.example | 12 +- README.md | 382 +++--- docs/ectool-commands-zh.md | 666 +++++----- wix/Product-Service.wxs | 96 +- wix/Product.wxs | 132 +- wix/exclude-service.xsl | 22 +- 32 files changed, 2745 insertions(+), 2742 deletions(-) diff --git a/.gitignore b/.gitignore index a623233..c7cf4a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,13 @@ -bin/ -obj/ -.vs/ -*.user -*.suo -dist/ -dist-svc/ -dist-installer/ -*.log -wix/obj/ -wix/Harvested.wxs -wix/Harvested-Service.wxs -FanControl.ChromeboxEC/lib/FanControl.Plugins.dll +bin/ +obj/ +.vs/ +*.user +*.suo +dist/ +dist-svc/ +dist-installer/ +*.log +wix/obj/ +wix/Harvested.wxs +wix/Harvested-Service.wxs +FanControl.ChromeboxEC/lib/FanControl.Plugins.dll diff --git a/ChromeboxFanControl.sln b/ChromeboxFanControl.sln index e72351d..71dc12b 100644 --- a/ChromeboxFanControl.sln +++ b/ChromeboxFanControl.sln @@ -1,29 +1,29 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromeboxFanControl", "ChromeboxFanControl\ChromeboxFanControl.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromeboxFanControlService", "ChromeboxFanControlService\ChromeboxFanControlService.csproj", "{B2C3D4E5-F6A7-8901-BCDE-F12345678901}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FanControl.ChromeboxEC", "FanControl.ChromeboxEC\FanControl.ChromeboxEC.csproj", "{C3D4E5F6-A7B8-9012-CDEF-123456789012}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU - {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.Build.0 = Release|Any CPU - {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromeboxFanControl", "ChromeboxFanControl\ChromeboxFanControl.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromeboxFanControlService", "ChromeboxFanControlService\ChromeboxFanControlService.csproj", "{B2C3D4E5-F6A7-8901-BCDE-F12345678901}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FanControl.ChromeboxEC", "FanControl.ChromeboxEC\FanControl.ChromeboxEC.csproj", "{C3D4E5F6-A7B8-9012-CDEF-123456789012}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU + {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.Build.0 = Release|Any CPU + {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C3D4E5F6-A7B8-9012-CDEF-123456789012}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/ChromeboxFanControl/AppConfig.cs b/ChromeboxFanControl/AppConfig.cs index 6acaa90..1b54a2e 100644 --- a/ChromeboxFanControl/AppConfig.cs +++ b/ChromeboxFanControl/AppConfig.cs @@ -1,136 +1,136 @@ -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace ChromeboxFanControl; - -public sealed class AppConfig -{ - /// UI language: "auto" (system), "en", "zh-Hans", "zh-Hant". Restart to apply. - public string Language { get; set; } = "auto"; - public string EctoolPath { get; set; } = @"C:\Program Files\crosec\ectool.exe"; - public int PollIntervalMs { get; set; } = 1500; - /// Call ectool for RPM every N control cycles (1 = every cycle). - public int FanRpmPollEveryNCycles { get; set; } = 1; - public string[] FanRpmArgs { get; set; } = ["pwmgetfanrpm", "0"]; - /// Ectool args to read fan duty (0-100). Empty = skip, use commanded duty. - public string[]? FanDutyArgs { get; set; } - public string[] AutoFanCtrlArgs { get; set; } = ["autofanctrl"]; - public string TempSource { get; set; } = "AverageCore"; - public int RampUpSteps { get; set; } = 3; - public int RampUpMinDeltaPercent { get; set; } = 20; - public int FailSafeAfterConsecutiveErrors { get; set; } = 5; - public int FailSafeFanPercent { get; set; } = 100; - public bool FailSafeRestoreAutoFan { get; set; } - public int ChartHistoryMinutes { get; set; } = 15; - public int ChartMaxPoints { get; set; } = 900; - public int[] CurvePoints { get; set; } = - [ - 0, 0, 10, 25, 40, 55, 70, 80, 90, 95, 97, 98, 99, 100 - ]; - - public static JsonSerializerOptions JsonOptions { get; } = new() - { - WriteIndented = true, - PropertyNameCaseInsensitive = true, - Converters = { new JsonStringEnumConverter() } - }; - - public static string UserConfigPath => - Path.Combine( - Environment.UserInteractive - ? Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) - : Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), - "ChromeboxFanControl", - "config.json"); - - public static AppConfig LoadMerged() - { - var cfg = new AppConfig(); - var baseDir = AppContext.BaseDirectory; - var appSettings = Path.Combine(baseDir, "appsettings.json"); - if (File.Exists(appSettings)) - ApplyJson(File.ReadAllText(appSettings), cfg); - - if (File.Exists(UserConfigPath)) - { - try - { - ApplyJson(File.ReadAllText(UserConfigPath), cfg); - } - catch - { - /* keep base */ - } - } - - return cfg; - } - - private static void ApplyJson(string json, AppConfig dst) - { - var src = JsonSerializer.Deserialize(json, JsonOptions); - if (src == null) - return; - - if (!string.IsNullOrEmpty(src.Language)) - dst.Language = src.Language; - dst.EctoolPath = src.EctoolPath; - dst.PollIntervalMs = Math.Clamp(src.PollIntervalMs, 250, 60_000); - dst.FanRpmPollEveryNCycles = Math.Max(1, src.FanRpmPollEveryNCycles); - if (src.FanRpmArgs is { Length: > 0 }) - dst.FanRpmArgs = src.FanRpmArgs; - dst.FanDutyArgs = src.FanDutyArgs; - if (src.AutoFanCtrlArgs is { Length: > 0 }) - dst.AutoFanCtrlArgs = src.AutoFanCtrlArgs; - dst.TempSource = string.Equals(src.TempSource, "MaxCore", StringComparison.OrdinalIgnoreCase) ? "MaxCore" : "AverageCore"; - dst.RampUpSteps = Math.Clamp(src.RampUpSteps, 1, 10); - dst.RampUpMinDeltaPercent = Math.Clamp(src.RampUpMinDeltaPercent, 0, 50); - dst.FailSafeAfterConsecutiveErrors = Math.Max(1, src.FailSafeAfterConsecutiveErrors); - dst.FailSafeFanPercent = Math.Clamp(src.FailSafeFanPercent, 0, 100); - dst.FailSafeRestoreAutoFan = src.FailSafeRestoreAutoFan; - dst.ChartHistoryMinutes = Math.Clamp(src.ChartHistoryMinutes, 1, 120); - dst.ChartMaxPoints = src.ChartMaxPoints > 0 ? src.ChartMaxPoints : 900; - if (src.CurvePoints is { Length: 14 }) - dst.CurvePoints = (int[])src.CurvePoints.Clone(); - else if (src.CurvePoints is { Length: 13 } old13) - dst.CurvePoints = MigrateCurveTo14(old13); - else if (src.CurvePoints is { Length: 20 } old20) - dst.CurvePoints = MigrateCurveTo14(old20, new[] { 0, 5, 11, 16, 21, 26, 32, 37, 42, 47, 53, 58, 63, 68, 74, 79, 84, 90, 95, 100 }); - } - - private static int[] MigrateCurveTo14(int[] oldCurve, int[]? oldTemps = null) - { - var temps = oldTemps ?? (oldCurve.Length == 13 ? new[] { 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 } : new[] { 0, 5, 11, 16, 21, 26, 32, 37, 42, 47, 53, 58, 63, 68, 74, 79, 84, 90, 95, 100 }); - var newTemps = FanCurve.TempBreakpoints; - var result = new int[14]; - for (var i = 0; i < 14; i++) - { - var t = newTemps[i]; - if (t <= temps[0]) - result[i] = Math.Clamp(oldCurve[0], 0, 100); - else if (t >= temps[^1]) - result[i] = Math.Clamp(oldCurve[^1], 0, 100); - else - { - for (var j = 0; j < temps.Length - 1; j++) - { - if (t < temps[j + 1]) - { - var frac = (t - temps[j]) / (double)(temps[j + 1] - temps[j]); - result[i] = (int)Math.Round(Math.Clamp(oldCurve[j], 0, 100) + (Math.Clamp(oldCurve[j + 1], 0, 100) - Math.Clamp(oldCurve[j], 0, 100)) * frac); - break; - } - } - } - } - return result; - } - - public void SaveUser() - { - var dir = Path.GetDirectoryName(UserConfigPath); - if (!string.IsNullOrEmpty(dir)) - Directory.CreateDirectory(dir); - File.WriteAllText(UserConfigPath, JsonSerializer.Serialize(this, JsonOptions)); - } -} +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace ChromeboxFanControl; + +public sealed class AppConfig +{ + /// UI language: "auto" (system), "en", "zh-Hans", "zh-Hant". Restart to apply. + public string Language { get; set; } = "auto"; + public string EctoolPath { get; set; } = @"C:\Program Files\crosec\ectool.exe"; + public int PollIntervalMs { get; set; } = 1500; + /// Call ectool for RPM every N control cycles (1 = every cycle). + public int FanRpmPollEveryNCycles { get; set; } = 1; + public string[] FanRpmArgs { get; set; } = ["pwmgetfanrpm", "0"]; + /// Ectool args to read fan duty (0-100). Empty = skip, use commanded duty. + public string[]? FanDutyArgs { get; set; } + public string[] AutoFanCtrlArgs { get; set; } = ["autofanctrl"]; + public string TempSource { get; set; } = "AverageCore"; + public int RampUpSteps { get; set; } = 3; + public int RampUpMinDeltaPercent { get; set; } = 20; + public int FailSafeAfterConsecutiveErrors { get; set; } = 5; + public int FailSafeFanPercent { get; set; } = 100; + public bool FailSafeRestoreAutoFan { get; set; } + public int ChartHistoryMinutes { get; set; } = 15; + public int ChartMaxPoints { get; set; } = 900; + public int[] CurvePoints { get; set; } = + [ + 0, 0, 10, 25, 40, 55, 70, 80, 90, 95, 97, 98, 99, 100 + ]; + + public static JsonSerializerOptions JsonOptions { get; } = new() + { + WriteIndented = true, + PropertyNameCaseInsensitive = true, + Converters = { new JsonStringEnumConverter() } + }; + + public static string UserConfigPath => + Path.Combine( + Environment.UserInteractive + ? Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + : Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), + "ChromeboxFanControl", + "config.json"); + + public static AppConfig LoadMerged() + { + var cfg = new AppConfig(); + var baseDir = AppContext.BaseDirectory; + var appSettings = Path.Combine(baseDir, "appsettings.json"); + if (File.Exists(appSettings)) + ApplyJson(File.ReadAllText(appSettings), cfg); + + if (File.Exists(UserConfigPath)) + { + try + { + ApplyJson(File.ReadAllText(UserConfigPath), cfg); + } + catch + { + /* keep base */ + } + } + + return cfg; + } + + private static void ApplyJson(string json, AppConfig dst) + { + var src = JsonSerializer.Deserialize(json, JsonOptions); + if (src == null) + return; + + if (!string.IsNullOrEmpty(src.Language)) + dst.Language = src.Language; + dst.EctoolPath = src.EctoolPath; + dst.PollIntervalMs = Math.Clamp(src.PollIntervalMs, 250, 60_000); + dst.FanRpmPollEveryNCycles = Math.Max(1, src.FanRpmPollEveryNCycles); + if (src.FanRpmArgs is { Length: > 0 }) + dst.FanRpmArgs = src.FanRpmArgs; + dst.FanDutyArgs = src.FanDutyArgs; + if (src.AutoFanCtrlArgs is { Length: > 0 }) + dst.AutoFanCtrlArgs = src.AutoFanCtrlArgs; + dst.TempSource = string.Equals(src.TempSource, "MaxCore", StringComparison.OrdinalIgnoreCase) ? "MaxCore" : "AverageCore"; + dst.RampUpSteps = Math.Clamp(src.RampUpSteps, 1, 10); + dst.RampUpMinDeltaPercent = Math.Clamp(src.RampUpMinDeltaPercent, 0, 50); + dst.FailSafeAfterConsecutiveErrors = Math.Max(1, src.FailSafeAfterConsecutiveErrors); + dst.FailSafeFanPercent = Math.Clamp(src.FailSafeFanPercent, 0, 100); + dst.FailSafeRestoreAutoFan = src.FailSafeRestoreAutoFan; + dst.ChartHistoryMinutes = Math.Clamp(src.ChartHistoryMinutes, 1, 120); + dst.ChartMaxPoints = src.ChartMaxPoints > 0 ? src.ChartMaxPoints : 900; + if (src.CurvePoints is { Length: 14 }) + dst.CurvePoints = (int[])src.CurvePoints.Clone(); + else if (src.CurvePoints is { Length: 13 } old13) + dst.CurvePoints = MigrateCurveTo14(old13); + else if (src.CurvePoints is { Length: 20 } old20) + dst.CurvePoints = MigrateCurveTo14(old20, new[] { 0, 5, 11, 16, 21, 26, 32, 37, 42, 47, 53, 58, 63, 68, 74, 79, 84, 90, 95, 100 }); + } + + private static int[] MigrateCurveTo14(int[] oldCurve, int[]? oldTemps = null) + { + var temps = oldTemps ?? (oldCurve.Length == 13 ? new[] { 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 } : new[] { 0, 5, 11, 16, 21, 26, 32, 37, 42, 47, 53, 58, 63, 68, 74, 79, 84, 90, 95, 100 }); + var newTemps = FanCurve.TempBreakpoints; + var result = new int[14]; + for (var i = 0; i < 14; i++) + { + var t = newTemps[i]; + if (t <= temps[0]) + result[i] = Math.Clamp(oldCurve[0], 0, 100); + else if (t >= temps[^1]) + result[i] = Math.Clamp(oldCurve[^1], 0, 100); + else + { + for (var j = 0; j < temps.Length - 1; j++) + { + if (t < temps[j + 1]) + { + var frac = (t - temps[j]) / (double)(temps[j + 1] - temps[j]); + result[i] = (int)Math.Round(Math.Clamp(oldCurve[j], 0, 100) + (Math.Clamp(oldCurve[j + 1], 0, 100) - Math.Clamp(oldCurve[j], 0, 100)) * frac); + break; + } + } + } + } + return result; + } + + public void SaveUser() + { + var dir = Path.GetDirectoryName(UserConfigPath); + if (!string.IsNullOrEmpty(dir)) + Directory.CreateDirectory(dir); + File.WriteAllText(UserConfigPath, JsonSerializer.Serialize(this, JsonOptions)); + } +} diff --git a/ChromeboxFanControl/ChromeboxFanControl.csproj b/ChromeboxFanControl/ChromeboxFanControl.csproj index b3ae4d5..c008da8 100644 --- a/ChromeboxFanControl/ChromeboxFanControl.csproj +++ b/ChromeboxFanControl/ChromeboxFanControl.csproj @@ -1,32 +1,32 @@ - - - - $(NoWarn);NU1701 - WinExe - net8.0-windows - enable - true - enable - app.manifest - ChromeboxFanControl - ChromeboxFanControl - - true - - - - - - - - - - PreserveNewest - - - - - en;zh-Hans;zh-Hant - - - + + + + $(NoWarn);NU1701 + WinExe + net8.0-windows + enable + true + enable + app.manifest + ChromeboxFanControl + ChromeboxFanControl + + true + + + + + + + + + + PreserveNewest + + + + + en;zh-Hans;zh-Hant + + + diff --git a/ChromeboxFanControl/CpuTempReader.cs b/ChromeboxFanControl/CpuTempReader.cs index bfaee68..0d10353 100644 --- a/ChromeboxFanControl/CpuTempReader.cs +++ b/ChromeboxFanControl/CpuTempReader.cs @@ -1,102 +1,102 @@ -using LibreHardwareMonitor.Hardware; - -namespace ChromeboxFanControl; - -public enum CpuTempAggregation -{ - AverageCore, - MaxCore -} - -/// -/// CPU temperature via LibreHardwareMonitor only (no fan sensors). -/// -public sealed class CpuTempReader : IDisposable -{ - private readonly Computer _computer = new() - { - IsCpuEnabled = true, - IsMotherboardEnabled = true - }; - private bool _open; - - public CpuTempReader() - { - _computer.Open(); - _open = true; - } - - public void Dispose() - { - if (!_open) - return; - _computer.Close(); - _open = false; - } - - /// Returns null if no reading available. - public double? ReadCpuTemp(CpuTempAggregation mode) - { - foreach (var hw in _computer.Hardware) - { - hw.Update(); - foreach (var sh in hw.SubHardware) - sh.Update(); - } - - var coreTemps = new List(); - foreach (var hw in _computer.Hardware) - { - CollectCoreTemps(hw, coreTemps); - foreach (var sh in hw.SubHardware) - CollectCoreTemps(sh, coreTemps); - } - - if (coreTemps.Count > 0) - { - return mode == CpuTempAggregation.MaxCore - ? coreTemps.Max() - : coreTemps.Average(); - } - - // Fallback: any CPU temperature sensor - var any = new List(); - foreach (var hw in _computer.Hardware) - { - CollectAnyCpuTemp(hw, any); - foreach (var sh in hw.SubHardware) - CollectAnyCpuTemp(sh, any); - } - - if (any.Count == 0) - return null; - return mode == CpuTempAggregation.MaxCore ? any.Max() : any.Average(); - } - - private static readonly string[] CoreTempPatterns = - [ - "Core", "Package", "Tctl", "Tdie", "CCD", "CPU Package", - "Core Average", "Core Max", "Core #" - ]; - - private static void CollectCoreTemps(IHardware hw, List list) - { - foreach (var s in hw.Sensors) - { - if (s.SensorType != SensorType.Temperature || !s.Value.HasValue) - continue; - var name = s.Name; - if (CoreTempPatterns.Any(p => name.Contains(p, StringComparison.OrdinalIgnoreCase))) - list.Add(s.Value.Value); - } - } - - private static void CollectAnyCpuTemp(IHardware hw, List list) - { - foreach (var s in hw.Sensors) - { - if (s.SensorType == SensorType.Temperature && s.Value.HasValue) - list.Add(s.Value.Value); - } - } -} +using LibreHardwareMonitor.Hardware; + +namespace ChromeboxFanControl; + +public enum CpuTempAggregation +{ + AverageCore, + MaxCore +} + +/// +/// CPU temperature via LibreHardwareMonitor only (no fan sensors). +/// +public sealed class CpuTempReader : IDisposable +{ + private readonly Computer _computer = new() + { + IsCpuEnabled = true, + IsMotherboardEnabled = true + }; + private bool _open; + + public CpuTempReader() + { + _computer.Open(); + _open = true; + } + + public void Dispose() + { + if (!_open) + return; + _computer.Close(); + _open = false; + } + + /// Returns null if no reading available. + public double? ReadCpuTemp(CpuTempAggregation mode) + { + foreach (var hw in _computer.Hardware) + { + hw.Update(); + foreach (var sh in hw.SubHardware) + sh.Update(); + } + + var coreTemps = new List(); + foreach (var hw in _computer.Hardware) + { + CollectCoreTemps(hw, coreTemps); + foreach (var sh in hw.SubHardware) + CollectCoreTemps(sh, coreTemps); + } + + if (coreTemps.Count > 0) + { + return mode == CpuTempAggregation.MaxCore + ? coreTemps.Max() + : coreTemps.Average(); + } + + // Fallback: any CPU temperature sensor + var any = new List(); + foreach (var hw in _computer.Hardware) + { + CollectAnyCpuTemp(hw, any); + foreach (var sh in hw.SubHardware) + CollectAnyCpuTemp(sh, any); + } + + if (any.Count == 0) + return null; + return mode == CpuTempAggregation.MaxCore ? any.Max() : any.Average(); + } + + private static readonly string[] CoreTempPatterns = + [ + "Core", "Package", "Tctl", "Tdie", "CCD", "CPU Package", + "Core Average", "Core Max", "Core #" + ]; + + private static void CollectCoreTemps(IHardware hw, List list) + { + foreach (var s in hw.Sensors) + { + if (s.SensorType != SensorType.Temperature || !s.Value.HasValue) + continue; + var name = s.Name; + if (CoreTempPatterns.Any(p => name.Contains(p, StringComparison.OrdinalIgnoreCase))) + list.Add(s.Value.Value); + } + } + + private static void CollectAnyCpuTemp(IHardware hw, List list) + { + foreach (var s in hw.Sensors) + { + if (s.SensorType == SensorType.Temperature && s.Value.HasValue) + list.Add(s.Value.Value); + } + } +} diff --git a/ChromeboxFanControl/EctoolRunner.cs b/ChromeboxFanControl/EctoolRunner.cs index 150a206..ee51a25 100644 --- a/ChromeboxFanControl/EctoolRunner.cs +++ b/ChromeboxFanControl/EctoolRunner.cs @@ -1,115 +1,115 @@ -using System.Diagnostics; -using System.Text.RegularExpressions; - -namespace ChromeboxFanControl; - -public sealed class EctoolRunner -{ - private static readonly Regex NumberRegex = new(@"\d+", RegexOptions.Compiled); - - public static (bool ok, string stdout, string stderr) RunSync( - string exePath, - IReadOnlyList args, - int timeoutMs = 15_000) => - RunAsync(exePath, args, CancellationToken.None, timeoutMs).GetAwaiter().GetResult(); - - public static async Task<(bool ok, string stdout, string stderr)> RunAsync( - string exePath, - IReadOnlyList args, - CancellationToken ct = default, - int timeoutMs = 15_000) - { - if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath)) - return (false, "", "ectool executable not found."); - - var psi = new ProcessStartInfo - { - FileName = exePath, - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true, - }; - foreach (var a in args) - psi.ArgumentList.Add(a); - - using var proc = new Process { StartInfo = psi }; - try - { - proc.Start(); - } - catch (Exception ex) - { - return (false, "", ex.Message); - } - - var stdoutTask = proc.StandardOutput.ReadToEndAsync(ct); - var stderrTask = proc.StandardError.ReadToEndAsync(ct); - var waitTask = proc.WaitForExitAsync(ct); - var completed = await Task.WhenAny(waitTask, Task.Delay(timeoutMs, ct)).ConfigureAwait(false); - if (completed != waitTask) - { - try - { - proc.Kill(entireProcessTree: true); - } - catch - { - /* ignore */ - } - - return (false, "", "ectool timed out."); - } - - await waitTask.ConfigureAwait(false); - var stdout = await stdoutTask.ConfigureAwait(false); - var stderr = await stderrTask.ConfigureAwait(false); - var ok = proc.ExitCode == 0; - return (ok, stdout, stderr); - } - - /// Pick the most plausible RPM (typically 500–20000) from ectool text output. - public static int? TryParseFanRpm(string stdout) - { - if (string.IsNullOrWhiteSpace(stdout)) - return null; - - int? best = null; - foreach (Match m in NumberRegex.Matches(stdout)) - { - if (!int.TryParse(m.Value, out var n)) - continue; - if (n is < 400 or > 50_000) - continue; - // Prefer larger numbers in typical fan range (avoid fan index "0") - if (n >= 800 && (best == null || n > best)) - best = n; - } - - if (best != null) - return best; - - foreach (Match m in NumberRegex.Matches(stdout)) - { - if (int.TryParse(m.Value, out var n) && n is >= 0 and <= 50_000) - return n; - } - - return null; - } - - /// Pick duty percent (0-100) from ectool text output. - public static int? TryParseFanDuty(string stdout) - { - if (string.IsNullOrWhiteSpace(stdout)) - return null; - - foreach (Match m in NumberRegex.Matches(stdout)) - { - if (int.TryParse(m.Value, out var n) && n is >= 0 and <= 100) - return n; - } - - return null; - } -} +using System.Diagnostics; +using System.Text.RegularExpressions; + +namespace ChromeboxFanControl; + +public sealed class EctoolRunner +{ + private static readonly Regex NumberRegex = new(@"\d+", RegexOptions.Compiled); + + public static (bool ok, string stdout, string stderr) RunSync( + string exePath, + IReadOnlyList args, + int timeoutMs = 15_000) => + RunAsync(exePath, args, CancellationToken.None, timeoutMs).GetAwaiter().GetResult(); + + public static async Task<(bool ok, string stdout, string stderr)> RunAsync( + string exePath, + IReadOnlyList args, + CancellationToken ct = default, + int timeoutMs = 15_000) + { + if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath)) + return (false, "", "ectool executable not found."); + + var psi = new ProcessStartInfo + { + FileName = exePath, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true, + }; + foreach (var a in args) + psi.ArgumentList.Add(a); + + using var proc = new Process { StartInfo = psi }; + try + { + proc.Start(); + } + catch (Exception ex) + { + return (false, "", ex.Message); + } + + var stdoutTask = proc.StandardOutput.ReadToEndAsync(ct); + var stderrTask = proc.StandardError.ReadToEndAsync(ct); + var waitTask = proc.WaitForExitAsync(ct); + var completed = await Task.WhenAny(waitTask, Task.Delay(timeoutMs, ct)).ConfigureAwait(false); + if (completed != waitTask) + { + try + { + proc.Kill(entireProcessTree: true); + } + catch + { + /* ignore */ + } + + return (false, "", "ectool timed out."); + } + + await waitTask.ConfigureAwait(false); + var stdout = await stdoutTask.ConfigureAwait(false); + var stderr = await stderrTask.ConfigureAwait(false); + var ok = proc.ExitCode == 0; + return (ok, stdout, stderr); + } + + /// Pick the most plausible RPM (typically 500–20000) from ectool text output. + public static int? TryParseFanRpm(string stdout) + { + if (string.IsNullOrWhiteSpace(stdout)) + return null; + + int? best = null; + foreach (Match m in NumberRegex.Matches(stdout)) + { + if (!int.TryParse(m.Value, out var n)) + continue; + if (n is < 400 or > 50_000) + continue; + // Prefer larger numbers in typical fan range (avoid fan index "0") + if (n >= 800 && (best == null || n > best)) + best = n; + } + + if (best != null) + return best; + + foreach (Match m in NumberRegex.Matches(stdout)) + { + if (int.TryParse(m.Value, out var n) && n is >= 0 and <= 50_000) + return n; + } + + return null; + } + + /// Pick duty percent (0-100) from ectool text output. + public static int? TryParseFanDuty(string stdout) + { + if (string.IsNullOrWhiteSpace(stdout)) + return null; + + foreach (Match m in NumberRegex.Matches(stdout)) + { + if (int.TryParse(m.Value, out var n) && n is >= 0 and <= 100) + return n; + } + + return null; + } +} diff --git a/ChromeboxFanControl/FanController.cs b/ChromeboxFanControl/FanController.cs index 1c0be6e..9d33e2b 100644 --- a/ChromeboxFanControl/FanController.cs +++ b/ChromeboxFanControl/FanController.cs @@ -1,296 +1,296 @@ -namespace ChromeboxFanControl; - -/// -/// Background loop: LHM temp → curve → ectool fanduty; ectool RPM; fail-safe handling. -/// -public sealed class FanController : IDisposable -{ - private readonly object _cfgLock = new(); - private AppConfig _config; - private CancellationTokenSource? _cts; - private Task? _loop; - private CpuTempReader? _cpu; - private volatile bool _paused; - private int _consecutiveTempErrors; - private int _cycleIndex; - private bool _failSafeActive; - private byte? _lastCommandedDuty; - private byte _rampStartDuty; - private byte _rampTargetDuty; - private int _rampStepIndex; - - public FanController(AppConfig initialConfig) - { - _config = initialConfig; - } - - public event EventHandler? Sample; - - public bool Paused - { - get => _paused; - set => _paused = value; - } - - public void UpdateConfig(AppConfig config) - { - lock (_cfgLock) - _config = config; - } - - public void Start() - { - StopInternal(waitForLoop: true); - _cpu = new CpuTempReader(); - _cts = new CancellationTokenSource(); - var token = _cts.Token; - _loop = Task.Run(() => LoopAsync(token), token); - } - - public void Stop() - { - StopInternal(waitForLoop: true); - } - - private void StopInternal(bool waitForLoop) - { - try - { - _cts?.Cancel(); - } - catch - { - /* */ - } - - if (waitForLoop && _loop != null) - { - try - { - _loop.Wait(8000); - } - catch - { - /* */ - } - } - - _cpu?.Dispose(); - _cpu = null; - _cts?.Dispose(); - _cts = null; - _loop = null; - } - - public async Task RestoreAutoFanAsync() - { - AppConfig cfg; - lock (_cfgLock) - cfg = _config; - - if (!File.Exists(cfg.EctoolPath)) - return; - - var (ok, _, err) = await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.AutoFanCtrlArgs) - .ConfigureAwait(false); - if (!ok) - DebugLog($"autofanctrl: {err}"); - } - - private async Task LoopAsync(CancellationToken ct) - { - while (!ct.IsCancellationRequested) - { - AppConfig cfg; - lock (_cfgLock) - cfg = _config; - - var mode = string.Equals(cfg.TempSource, "MaxCore", StringComparison.OrdinalIgnoreCase) - ? CpuTempAggregation.MaxCore - : CpuTempAggregation.AverageCore; - - double? tempC = null; - try - { - tempC = _cpu?.ReadCpuTemp(mode); - } - catch (Exception ex) - { - DebugLog($"LHM: {ex.Message}"); - } - - if (tempC == null) - _consecutiveTempErrors++; - else - _consecutiveTempErrors = 0; - - if (_consecutiveTempErrors >= cfg.FailSafeAfterConsecutiveErrors) - _failSafeActive = true; - else if (tempC != null) - _failSafeActive = false; - - byte? targetDuty = null; - string? ectoolMsg = null; - int? rpm = null; - int? actualDuty = null; - - if (_paused) - { - ClearRampState(); - targetDuty = null; - ectoolMsg = "paused"; - } - else if (_failSafeActive) - { - ClearRampState(); - if (cfg.FailSafeRestoreAutoFan) - { - var (okA, _, eA) = await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.AutoFanCtrlArgs, ct) - .ConfigureAwait(false); - targetDuty = null; - ectoolMsg = okA ? "fail-safe: autofanctrl" : $"fail-safe autofanctrl: {eA}"; - } - else - { - var d = (byte)Math.Clamp(cfg.FailSafeFanPercent, 0, 100); - targetDuty = d; - _lastCommandedDuty = d; - var (okF, _, eF) = await EctoolRunner - .RunAsync(cfg.EctoolPath, ["fanduty", d.ToString()], ct) - .ConfigureAwait(false); - ectoolMsg = okF ? $"fail-safe fanduty {d}%" : $"fail-safe: {eF}"; - } - } - else if (tempC == null) - { - ClearRampState(); - targetDuty = null; - ectoolMsg = "no CPU temperature (holding EC state)"; - } - else - { - var curveTarget = FanCurve.CalculateFanPercent(tempC.Value, cfg.CurvePoints); - var output = ComputeRampOutput(curveTarget, cfg); - targetDuty = output; - _lastCommandedDuty = output; - var (okD, _, eD) = await EctoolRunner - .RunAsync(cfg.EctoolPath, ["fanduty", output.ToString()], ct) - .ConfigureAwait(false); - ectoolMsg = okD ? $"fanduty {output}%" : eD; - } - - _cycleIndex++; - if (cfg.FanRpmPollEveryNCycles > 0 && - _cycleIndex % cfg.FanRpmPollEveryNCycles == 0 && - File.Exists(cfg.EctoolPath)) - { - var (okR, stdoutR, errR) = - await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.FanRpmArgs, ct).ConfigureAwait(false); - if (okR) - rpm = EctoolRunner.TryParseFanRpm(stdoutR); - else - ectoolMsg = string.IsNullOrEmpty(ectoolMsg) ? $"rpm: {errR}" : $"{ectoolMsg}; rpm: {errR}"; - - if (cfg.FanDutyArgs is { Length: > 0 }) - { - var (okDuty, stdoutDuty, errDuty) = - await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.FanDutyArgs, ct).ConfigureAwait(false); - if (okDuty) - actualDuty = EctoolRunner.TryParseFanDuty(stdoutDuty); - else - ectoolMsg = string.IsNullOrEmpty(ectoolMsg) ? $"duty: {errDuty}" : $"{ectoolMsg}; duty: {errDuty}"; - } - } - - RaiseSample(new FanSampleEventArgs - { - Time = DateTime.Now, - TempC = tempC, - TargetDutyPercent = targetDuty, - ActualDutyPercent = actualDuty, - Rpm = rpm, - LastEctoolMessage = ectoolMsg, - Paused = _paused, - FailSafe = _failSafeActive - }); - - try - { - await Task.Delay(Math.Clamp(cfg.PollIntervalMs, 250, 60_000), ct).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - break; - } - } - } - - private void RaiseSample(FanSampleEventArgs e) - { - try - { - Sample?.Invoke(this, e); - } - catch - { - /* UI must not kill loop */ - } - } - - private static void DebugLog(string msg) - { - System.Diagnostics.Debug.WriteLine($"[ChromeboxFanControl] {msg}"); - } - - private void ClearRampState() - { - _rampStepIndex = 0; - } - - private byte ComputeRampOutput(byte targetDuty, AppConfig cfg) - { - var n = Math.Clamp(cfg.RampUpSteps, 1, 10); - var minDelta = Math.Clamp(cfg.RampUpMinDeltaPercent, 0, 50); - - if (_lastCommandedDuty == null) - return targetDuty; - - var curr = _lastCommandedDuty.Value; - var delta = Math.Abs(targetDuty - curr); - - if (targetDuty == curr) - { - ClearRampState(); - return targetDuty; - } - - if (delta < minDelta) - { - ClearRampState(); - return targetDuty; - } - - if (_rampStepIndex == 0 || targetDuty != _rampTargetDuty) - { - _rampStartDuty = curr; - _rampTargetDuty = targetDuty; - _rampStepIndex = 1; - } - - if (_rampStepIndex >= n) - { - ClearRampState(); - return _rampTargetDuty; - } - - var step = (int)Math.Round((_rampTargetDuty - _rampStartDuty) * (double)_rampStepIndex / n); - var output = (byte)Math.Clamp(_rampStartDuty + step, 0, 100); - _rampStepIndex++; - return output; - } - - public void Dispose() - { - StopInternal(waitForLoop: true); - } -} +namespace ChromeboxFanControl; + +/// +/// Background loop: LHM temp → curve → ectool fanduty; ectool RPM; fail-safe handling. +/// +public sealed class FanController : IDisposable +{ + private readonly object _cfgLock = new(); + private AppConfig _config; + private CancellationTokenSource? _cts; + private Task? _loop; + private CpuTempReader? _cpu; + private volatile bool _paused; + private int _consecutiveTempErrors; + private int _cycleIndex; + private bool _failSafeActive; + private byte? _lastCommandedDuty; + private byte _rampStartDuty; + private byte _rampTargetDuty; + private int _rampStepIndex; + + public FanController(AppConfig initialConfig) + { + _config = initialConfig; + } + + public event EventHandler? Sample; + + public bool Paused + { + get => _paused; + set => _paused = value; + } + + public void UpdateConfig(AppConfig config) + { + lock (_cfgLock) + _config = config; + } + + public void Start() + { + StopInternal(waitForLoop: true); + _cpu = new CpuTempReader(); + _cts = new CancellationTokenSource(); + var token = _cts.Token; + _loop = Task.Run(() => LoopAsync(token), token); + } + + public void Stop() + { + StopInternal(waitForLoop: true); + } + + private void StopInternal(bool waitForLoop) + { + try + { + _cts?.Cancel(); + } + catch + { + /* */ + } + + if (waitForLoop && _loop != null) + { + try + { + _loop.Wait(8000); + } + catch + { + /* */ + } + } + + _cpu?.Dispose(); + _cpu = null; + _cts?.Dispose(); + _cts = null; + _loop = null; + } + + public async Task RestoreAutoFanAsync() + { + AppConfig cfg; + lock (_cfgLock) + cfg = _config; + + if (!File.Exists(cfg.EctoolPath)) + return; + + var (ok, _, err) = await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.AutoFanCtrlArgs) + .ConfigureAwait(false); + if (!ok) + DebugLog($"autofanctrl: {err}"); + } + + private async Task LoopAsync(CancellationToken ct) + { + while (!ct.IsCancellationRequested) + { + AppConfig cfg; + lock (_cfgLock) + cfg = _config; + + var mode = string.Equals(cfg.TempSource, "MaxCore", StringComparison.OrdinalIgnoreCase) + ? CpuTempAggregation.MaxCore + : CpuTempAggregation.AverageCore; + + double? tempC = null; + try + { + tempC = _cpu?.ReadCpuTemp(mode); + } + catch (Exception ex) + { + DebugLog($"LHM: {ex.Message}"); + } + + if (tempC == null) + _consecutiveTempErrors++; + else + _consecutiveTempErrors = 0; + + if (_consecutiveTempErrors >= cfg.FailSafeAfterConsecutiveErrors) + _failSafeActive = true; + else if (tempC != null) + _failSafeActive = false; + + byte? targetDuty = null; + string? ectoolMsg = null; + int? rpm = null; + int? actualDuty = null; + + if (_paused) + { + ClearRampState(); + targetDuty = null; + ectoolMsg = "paused"; + } + else if (_failSafeActive) + { + ClearRampState(); + if (cfg.FailSafeRestoreAutoFan) + { + var (okA, _, eA) = await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.AutoFanCtrlArgs, ct) + .ConfigureAwait(false); + targetDuty = null; + ectoolMsg = okA ? "fail-safe: autofanctrl" : $"fail-safe autofanctrl: {eA}"; + } + else + { + var d = (byte)Math.Clamp(cfg.FailSafeFanPercent, 0, 100); + targetDuty = d; + _lastCommandedDuty = d; + var (okF, _, eF) = await EctoolRunner + .RunAsync(cfg.EctoolPath, ["fanduty", d.ToString()], ct) + .ConfigureAwait(false); + ectoolMsg = okF ? $"fail-safe fanduty {d}%" : $"fail-safe: {eF}"; + } + } + else if (tempC == null) + { + ClearRampState(); + targetDuty = null; + ectoolMsg = "no CPU temperature (holding EC state)"; + } + else + { + var curveTarget = FanCurve.CalculateFanPercent(tempC.Value, cfg.CurvePoints); + var output = ComputeRampOutput(curveTarget, cfg); + targetDuty = output; + _lastCommandedDuty = output; + var (okD, _, eD) = await EctoolRunner + .RunAsync(cfg.EctoolPath, ["fanduty", output.ToString()], ct) + .ConfigureAwait(false); + ectoolMsg = okD ? $"fanduty {output}%" : eD; + } + + _cycleIndex++; + if (cfg.FanRpmPollEveryNCycles > 0 && + _cycleIndex % cfg.FanRpmPollEveryNCycles == 0 && + File.Exists(cfg.EctoolPath)) + { + var (okR, stdoutR, errR) = + await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.FanRpmArgs, ct).ConfigureAwait(false); + if (okR) + rpm = EctoolRunner.TryParseFanRpm(stdoutR); + else + ectoolMsg = string.IsNullOrEmpty(ectoolMsg) ? $"rpm: {errR}" : $"{ectoolMsg}; rpm: {errR}"; + + if (cfg.FanDutyArgs is { Length: > 0 }) + { + var (okDuty, stdoutDuty, errDuty) = + await EctoolRunner.RunAsync(cfg.EctoolPath, cfg.FanDutyArgs, ct).ConfigureAwait(false); + if (okDuty) + actualDuty = EctoolRunner.TryParseFanDuty(stdoutDuty); + else + ectoolMsg = string.IsNullOrEmpty(ectoolMsg) ? $"duty: {errDuty}" : $"{ectoolMsg}; duty: {errDuty}"; + } + } + + RaiseSample(new FanSampleEventArgs + { + Time = DateTime.Now, + TempC = tempC, + TargetDutyPercent = targetDuty, + ActualDutyPercent = actualDuty, + Rpm = rpm, + LastEctoolMessage = ectoolMsg, + Paused = _paused, + FailSafe = _failSafeActive + }); + + try + { + await Task.Delay(Math.Clamp(cfg.PollIntervalMs, 250, 60_000), ct).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + break; + } + } + } + + private void RaiseSample(FanSampleEventArgs e) + { + try + { + Sample?.Invoke(this, e); + } + catch + { + /* UI must not kill loop */ + } + } + + private static void DebugLog(string msg) + { + System.Diagnostics.Debug.WriteLine($"[ChromeboxFanControl] {msg}"); + } + + private void ClearRampState() + { + _rampStepIndex = 0; + } + + private byte ComputeRampOutput(byte targetDuty, AppConfig cfg) + { + var n = Math.Clamp(cfg.RampUpSteps, 1, 10); + var minDelta = Math.Clamp(cfg.RampUpMinDeltaPercent, 0, 50); + + if (_lastCommandedDuty == null) + return targetDuty; + + var curr = _lastCommandedDuty.Value; + var delta = Math.Abs(targetDuty - curr); + + if (targetDuty == curr) + { + ClearRampState(); + return targetDuty; + } + + if (delta < minDelta) + { + ClearRampState(); + return targetDuty; + } + + if (_rampStepIndex == 0 || targetDuty != _rampTargetDuty) + { + _rampStartDuty = curr; + _rampTargetDuty = targetDuty; + _rampStepIndex = 1; + } + + if (_rampStepIndex >= n) + { + ClearRampState(); + return _rampTargetDuty; + } + + var step = (int)Math.Round((_rampTargetDuty - _rampStartDuty) * (double)_rampStepIndex / n); + var output = (byte)Math.Clamp(_rampStartDuty + step, 0, 100); + _rampStepIndex++; + return output; + } + + public void Dispose() + { + StopInternal(waitForLoop: true); + } +} diff --git a/ChromeboxFanControl/FanCurve.cs b/ChromeboxFanControl/FanCurve.cs index ad1bb3f..d033fc2 100644 --- a/ChromeboxFanControl/FanCurve.cs +++ b/ChromeboxFanControl/FanCurve.cs @@ -1,41 +1,41 @@ -namespace ChromeboxFanControl; - -/// -/// Fan curve: 14 control points at 0 °C and 40–100 °C every 5 °C, linear interpolation. -/// -public static class FanCurve -{ - private const int PointCount = 14; - - /// Temperature breakpoints (°C): 0, 40, 45, 50, …, 100. - public static readonly int[] TempBreakpoints = [0, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]; - - /// CPU temperature in °C. - /// Exactly 14 values 0–100 (fan duty). - public static byte CalculateFanPercent(double tempC, ReadOnlySpan curve) - { - if (curve.Length != PointCount) - throw new ArgumentException($"Curve must contain exactly {PointCount} points.", nameof(curve)); - - var t = Math.Clamp(tempC, 0, 100); - if (t <= TempBreakpoints[0]) - return (byte)Math.Clamp(curve[0], 0, 100); - if (t >= TempBreakpoints[PointCount - 1]) - return (byte)Math.Clamp(curve[PointCount - 1], 0, 100); - - for (var i = 0; i < PointCount - 1; i++) - { - var t0 = TempBreakpoints[i]; - var t1 = TempBreakpoints[i + 1]; - if (t >= t0 && t <= t1) - { - var d0 = Math.Clamp(curve[i], 0, 100); - var d1 = Math.Clamp(curve[i + 1], 0, 100); - var frac = (t - t0) / (t1 - t0); - return (byte)Math.Round(d0 + (d1 - d0) * frac); - } - } - - return (byte)Math.Clamp(curve[PointCount - 1], 0, 100); - } -} +namespace ChromeboxFanControl; + +/// +/// Fan curve: 14 control points at 0 °C and 40–100 °C every 5 °C, linear interpolation. +/// +public static class FanCurve +{ + private const int PointCount = 14; + + /// Temperature breakpoints (°C): 0, 40, 45, 50, …, 100. + public static readonly int[] TempBreakpoints = [0, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]; + + /// CPU temperature in °C. + /// Exactly 14 values 0–100 (fan duty). + public static byte CalculateFanPercent(double tempC, ReadOnlySpan curve) + { + if (curve.Length != PointCount) + throw new ArgumentException($"Curve must contain exactly {PointCount} points.", nameof(curve)); + + var t = Math.Clamp(tempC, 0, 100); + if (t <= TempBreakpoints[0]) + return (byte)Math.Clamp(curve[0], 0, 100); + if (t >= TempBreakpoints[PointCount - 1]) + return (byte)Math.Clamp(curve[PointCount - 1], 0, 100); + + for (var i = 0; i < PointCount - 1; i++) + { + var t0 = TempBreakpoints[i]; + var t1 = TempBreakpoints[i + 1]; + if (t >= t0 && t <= t1) + { + var d0 = Math.Clamp(curve[i], 0, 100); + var d1 = Math.Clamp(curve[i + 1], 0, 100); + var frac = (t - t0) / (t1 - t0); + return (byte)Math.Round(d0 + (d1 - d0) * frac); + } + } + + return (byte)Math.Clamp(curve[PointCount - 1], 0, 100); + } +} diff --git a/ChromeboxFanControl/FanSampleEventArgs.cs b/ChromeboxFanControl/FanSampleEventArgs.cs index 10b9cc2..867f0cc 100644 --- a/ChromeboxFanControl/FanSampleEventArgs.cs +++ b/ChromeboxFanControl/FanSampleEventArgs.cs @@ -1,15 +1,15 @@ -namespace ChromeboxFanControl; - -public sealed class FanSampleEventArgs : EventArgs -{ - public DateTime Time { get; init; } - public double? TempC { get; init; } - /// Desired fan duty from curve or fail-safe; null when paused or not applicable. - public byte? TargetDutyPercent { get; init; } - /// Actual duty read from ectool when FanDutyArgs configured; else null. - public int? ActualDutyPercent { get; init; } - public int? Rpm { get; init; } - public string? LastEctoolMessage { get; init; } - public bool Paused { get; init; } - public bool FailSafe { get; init; } -} +namespace ChromeboxFanControl; + +public sealed class FanSampleEventArgs : EventArgs +{ + public DateTime Time { get; init; } + public double? TempC { get; init; } + /// Desired fan duty from curve or fail-safe; null when paused or not applicable. + public byte? TargetDutyPercent { get; init; } + /// Actual duty read from ectool when FanDutyArgs configured; else null. + public int? ActualDutyPercent { get; init; } + public int? Rpm { get; init; } + public string? LastEctoolMessage { get; init; } + public bool Paused { get; init; } + public bool FailSafe { get; init; } +} diff --git a/ChromeboxFanControl/MainForm.cs b/ChromeboxFanControl/MainForm.cs index 3f6caa8..c42f9ee 100644 --- a/ChromeboxFanControl/MainForm.cs +++ b/ChromeboxFanControl/MainForm.cs @@ -1,586 +1,586 @@ -using ChromeboxFanControl.Properties; -using ScottPlot; -using ScottPlot.WinForms; - -namespace ChromeboxFanControl; - -public sealed class MainForm : Form -{ - private AppConfig _config = AppConfig.LoadMerged(); - private FanController _fan = null!; - private readonly FormsPlot _chart = new() { Dock = DockStyle.Fill }; - private readonly FormsPlot _curveChart = new() { Dock = DockStyle.Fill }; - private readonly System.Windows.Forms.Label _lblStatus = new(); - private readonly DataGridView _gridCurve = new(); - private readonly TextBox _txtEctool = new(); - private readonly NumericUpDown _nudPoll = new(); - private readonly NumericUpDown _nudRpmEvery = new(); - private readonly TextBox _txtFanRpmArgs = new(); - private readonly TextBox _txtFanDutyArgs = new(); - private readonly TextBox _txtAutoFanArgs = new(); - private readonly ComboBox _cmbTempSource = new(); - private readonly ComboBox _cmbLanguage = new(); - private readonly NumericUpDown _nudFailCount = new(); - private readonly NumericUpDown _nudFailPct = new(); - private readonly CheckBox _chkFailAuto = new(); - private readonly NumericUpDown _nudChartMin = new(); - private readonly NumericUpDown _nudChartPoints = new(); - private readonly NumericUpDown _nudRampUpSteps = new(); - private readonly NumericUpDown _nudRampUpMinDelta = new(); - private readonly NotifyIcon _tray = new(); - private readonly ContextMenuStrip _trayMenu = new(); - private readonly ToolStripMenuItem _miPause; - private readonly object _seriesLock = new(); - private readonly List _tSec = new(); - private readonly List _temps = new(); - private readonly List _duties = new(); - private readonly List _rpms = new(); - private DateTime _chartAnchor = DateTime.UtcNow; - private bool _minimizeToTray = true; - private ScottPlot.IYAxis? _yRpmAxis; - - public MainForm() - { - _miPause = new ToolStripMenuItem(Resources.TrayPause); - Text = Resources.AppTitle; - Size = new Size(960, 700); - StartPosition = FormStartPosition.CenterScreen; - MinimumSize = new Size(800, 500); - - BuildUi(); - WireTray(); - - Load += (_, _) => - { - _config = AppConfig.LoadMerged(); - _chartAnchor = DateTime.UtcNow; - ApplyConfigToUi(); - _fan = new FanController(CloneConfigFromUi()); - _fan.Sample += FanOnSample; - _fan.Start(); - }; - - FormClosing += MainForm_FormClosing; - } - - private void BuildUi() - { - var tabs = new TabControl { Dock = DockStyle.Fill }; - - var tabMonitor = new TabPage(Resources.TabMonitor); - var tabCurve = new TabPage(Resources.TabCurve); - var tabAdv = new TabPage(Resources.TabAdvanced); - - tabs.TabPages.Add(tabMonitor); - tabs.TabPages.Add(tabCurve); - tabs.TabPages.Add(tabAdv); - - _lblStatus.AutoSize = false; - _lblStatus.Height = 28; - _lblStatus.Dock = DockStyle.Bottom; - _lblStatus.Padding = new Padding(8, 4, 8, 4); - _lblStatus.AutoEllipsis = true; - _lblStatus.Text = Resources.StatusStarting; - - var panelChart = new Panel { Dock = DockStyle.Fill }; - panelChart.Controls.Add(_chart); - panelChart.Controls.Add(_lblStatus); - - tabMonitor.Controls.Add(panelChart); - - _gridCurve.Dock = DockStyle.Top; - _gridCurve.Height = 260; - _gridCurve.AutoGenerateColumns = false; - _gridCurve.AllowUserToAddRows = false; - _gridCurve.RowHeadersVisible = false; - _gridCurve.Columns.Add(new DataGridViewTextBoxColumn - { - Name = "colTemp", - HeaderText = Resources.GridColTemp, - ReadOnly = true, - Width = 80 - }); - _gridCurve.Columns.Add(new DataGridViewTextBoxColumn - { - Name = "colDuty", - HeaderText = Resources.GridColDuty, - Width = 100 - }); - for (var i = 0; i < 14; i++) - _gridCurve.Rows.Add(FanCurve.TempBreakpoints[i].ToString(), "0"); - _gridCurve.CellValueChanged += (_, _) => RebuildCurveChart(); - - var panelCurveBtns = new FlowLayoutPanel - { - Dock = DockStyle.Bottom, - AutoSize = true, - FlowDirection = FlowDirection.LeftToRight, - Padding = new Padding(8) - }; - var btnSave = new Button { Text = Resources.BtnSaveApply, AutoSize = true }; - btnSave.Click += (_, _) => SaveFromUi(); - var btnDefault = new Button { Text = Resources.BtnRestoreDefault, AutoSize = true }; - btnDefault.Click += (_, _) => - { - var def = new AppConfig(); - for (var i = 0; i < 14; i++) - _gridCurve.Rows[i].Cells[1].Value = def.CurvePoints[i].ToString(); - RebuildCurveChart(); - }; - panelCurveBtns.Controls.Add(btnSave); - panelCurveBtns.Controls.Add(btnDefault); - - var panelCurveTop = new Panel { Dock = DockStyle.Top, Height = 360 }; - panelCurveTop.Controls.Add(_gridCurve); - panelCurveTop.Controls.Add(panelCurveBtns); - - var panelCurve = new Panel { Dock = DockStyle.Fill }; - panelCurve.Controls.Add(_curveChart); - panelCurve.Controls.Add(panelCurveTop); - tabCurve.Controls.Add(panelCurve); - - var flp = new TableLayoutPanel - { - Dock = DockStyle.Fill, - ColumnCount = 2, - RowCount = 15, - Padding = new Padding(12) - }; - flp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 35)); - flp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 65)); - for (var i = 0; i < 15; i++) - flp.RowStyles.Add(new RowStyle(SizeType.AutoSize)); - - var row = 0; - AddRow(flp, row++, Resources.LblLanguage, _cmbLanguage); - _cmbLanguage.DropDownStyle = ComboBoxStyle.DropDownList; - _cmbLanguage.Items.AddRange([Resources.LangAuto, Resources.LangEn, Resources.LangZhHans, Resources.LangZhHant]); - - AddRow(flp, row++, Resources.LblEctoolPath, _txtEctool); - AddRow(flp, row++, Resources.LblPollInterval, _nudPoll); - _nudPoll.Minimum = 250; - _nudPoll.Maximum = 60_000; - _nudPoll.Increment = 250; - - AddRow(flp, row++, Resources.LblRpmEvery, _nudRpmEvery); - _nudRpmEvery.Minimum = 1; - _nudRpmEvery.Maximum = 100; - - AddRow(flp, row++, Resources.LblFanRpmArgs, _txtFanRpmArgs); - AddRow(flp, row++, Resources.LblFanDutyArgs, _txtFanDutyArgs); - - AddRow(flp, row++, Resources.LblAutoFanArgs, _txtAutoFanArgs); - - AddRow(flp, row++, Resources.LblTempSource, _cmbTempSource); - _cmbTempSource.DropDownStyle = ComboBoxStyle.DropDownList; - _cmbTempSource.Items.AddRange(["AverageCore", "MaxCore"]); - - AddRow(flp, row++, Resources.LblFailCount, _nudFailCount); - _nudFailCount.Minimum = 1; - _nudFailCount.Maximum = 100; - - AddRow(flp, row++, Resources.LblFailPct, _nudFailPct); - _nudFailPct.Minimum = 0; - _nudFailPct.Maximum = 100; - - _chkFailAuto.Text = Resources.ChkFailAuto; - _chkFailAuto.AutoSize = true; - var failPanel = new FlowLayoutPanel - { - FlowDirection = FlowDirection.LeftToRight, - AutoSize = true, - Dock = DockStyle.Fill, - WrapContents = false - }; - failPanel.Controls.Add(_chkFailAuto); - flp.Controls.Add(new System.Windows.Forms.Label { Text = Resources.LblFailStrategy, AutoSize = true, Anchor = AnchorStyles.Left }, 0, row); - flp.Controls.Add(failPanel, 1, row++); - - AddRow(flp, row++, Resources.LblChartMin, _nudChartMin); - _nudChartMin.Minimum = 1; - _nudChartMin.Maximum = 120; - - AddRow(flp, row++, Resources.LblChartPoints, _nudChartPoints); - _nudChartPoints.Minimum = 100; - _nudChartPoints.Maximum = 10_000; - _nudChartPoints.Increment = 100; - - AddRow(flp, row++, Resources.LblRampUpSteps, _nudRampUpSteps); - _nudRampUpSteps.Minimum = 1; - _nudRampUpSteps.Maximum = 10; - - AddRow(flp, row++, Resources.LblRampUpMinDeltaPercent, _nudRampUpMinDelta); - _nudRampUpMinDelta.Minimum = 0; - _nudRampUpMinDelta.Maximum = 50; - - var btnSaveAdv = new Button { Text = Resources.BtnSaveAdvanced, AutoSize = true, Dock = DockStyle.Bottom }; - btnSaveAdv.Click += (_, _) => SaveFromUi(); - tabAdv.Controls.Add(btnSaveAdv); - tabAdv.Controls.Add(flp); - - Controls.Add(tabs); - } - - private static void AddRow(TableLayoutPanel t, int row, string label, Control editor) - { - t.Controls.Add(new System.Windows.Forms.Label { Text = label, AutoSize = true, Anchor = AnchorStyles.Left | AnchorStyles.Top }, 0, row); - editor.Dock = DockStyle.Fill; - t.Controls.Add(editor, 1, row); - } - - private void WireTray() - { - _tray.Text = Resources.AppTitle; - _tray.Visible = true; - _tray.Icon = SystemIcons.Application; - _tray.DoubleClick += (_, _) => ShowFromTray(); - _miPause.Click += (_, _) => - { - TogglePause(); - }; - _trayMenu.Items.Add(Resources.TrayOpen, null, (_, _) => ShowFromTray()); - _trayMenu.Items.Add(_miPause); - _trayMenu.Items.Add(Resources.TrayAbout, null, (_, _) => - MessageBox.Show(this, Resources.AboutMessage, Text, MessageBoxButtons.OK, MessageBoxIcon.Information)); - _trayMenu.Items.Add(Resources.TrayExit, null, (_, _) => Close()); - _tray.ContextMenuStrip = _trayMenu; - } - - private void ShowFromTray() - { - Show(); - WindowState = FormWindowState.Normal; - ShowInTaskbar = true; - Activate(); - } - - private void TogglePause() - { - if (_fan == null) - return; - _fan.Paused = !_fan.Paused; - _miPause.Text = _fan.Paused ? Resources.TrayResume : Resources.TrayPause; - } - - private static readonly string[] LanguageValues = ["auto", "en", "zh-Hans", "zh-Hant"]; - - private void ApplyConfigToUi() - { - var langIdx = Array.IndexOf(LanguageValues, _config.Language); - _cmbLanguage.SelectedIndex = langIdx >= 0 ? langIdx : 0; - - _txtEctool.Text = _config.EctoolPath; - _nudPoll.Value = _config.PollIntervalMs; - _nudRpmEvery.Value = _config.FanRpmPollEveryNCycles; - _txtFanRpmArgs.Text = string.Join(' ', _config.FanRpmArgs); - _txtFanDutyArgs.Text = _config.FanDutyArgs is { Length: > 0 } da ? string.Join(' ', da) : ""; - _txtAutoFanArgs.Text = string.Join(' ', _config.AutoFanCtrlArgs); - _cmbTempSource.SelectedItem = _config.TempSource; - if (_cmbTempSource.SelectedIndex < 0) - _cmbTempSource.SelectedIndex = 0; - _nudFailCount.Value = _config.FailSafeAfterConsecutiveErrors; - _nudFailPct.Value = _config.FailSafeFanPercent; - _chkFailAuto.Checked = _config.FailSafeRestoreAutoFan; - _nudChartMin.Value = _config.ChartHistoryMinutes; - _nudChartPoints.Value = _config.ChartMaxPoints; - _nudRampUpSteps.Value = _config.RampUpSteps; - _nudRampUpMinDelta.Value = _config.RampUpMinDeltaPercent; - for (var i = 0; i < 14; i++) - _gridCurve.Rows[i].Cells[1].Value = _config.CurvePoints[i].ToString(); - RebuildCurveChart(); - } - - private AppConfig CloneConfigFromUi() - { - var langIdx = _cmbLanguage.SelectedIndex; - var lang = langIdx >= 0 && langIdx < LanguageValues.Length ? LanguageValues[langIdx] : "auto"; - - var c = new AppConfig - { - Language = lang, - EctoolPath = _txtEctool.Text.Trim(), - PollIntervalMs = (int)_nudPoll.Value, - FanRpmPollEveryNCycles = (int)_nudRpmEvery.Value, - FanRpmArgs = SplitArgs(_txtFanRpmArgs.Text), - FanDutyArgs = SplitArgs(_txtFanDutyArgs.Text) is { Length: > 0 } fa ? fa : null, - AutoFanCtrlArgs = SplitArgs(_txtAutoFanArgs.Text), - TempSource = _cmbTempSource.SelectedItem?.ToString() ?? "AverageCore", - FailSafeAfterConsecutiveErrors = (int)_nudFailCount.Value, - FailSafeFanPercent = (int)_nudFailPct.Value, - FailSafeRestoreAutoFan = _chkFailAuto.Checked, - ChartHistoryMinutes = (int)_nudChartMin.Value, - ChartMaxPoints = (int)_nudChartPoints.Value, - RampUpSteps = (int)_nudRampUpSteps.Value, - RampUpMinDeltaPercent = (int)_nudRampUpMinDelta.Value, - CurvePoints = ReadCurveFromGrid() - }; - return c; - } - - private static string[] SplitArgs(string line) => - string.IsNullOrWhiteSpace(line) - ? [] - : line.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries); - - private int[] ReadCurveFromGrid() - { - var pts = new int[14]; - for (var i = 0; i < 14; i++) - { - var cell = _gridCurve.Rows[i].Cells[1].Value?.ToString(); - if (!int.TryParse(cell, out var v)) - v = 0; - pts[i] = Math.Clamp(v, 0, 100); - } - - return pts; - } - - private void RebuildCurveChart() - { - var temps = FanCurve.TempBreakpoints.Select(t => (double)t).ToArray(); - var duties = ReadCurveFromGrid().Select(d => (double)d).ToArray(); - - var plt = _curveChart.Plot; - plt.Clear(); - plt.Axes.Left.Label.Text = Resources.CurveChartDuty; - plt.Axes.Bottom.Label.Text = Resources.CurveChartTemp; - plt.Axes.SetLimitsX(0, 100); - plt.Axes.SetLimitsY(0, 100); - - if (temps.Length > 0 && duties.Length > 0) - { - var scatter = plt.Add.Scatter(temps, duties); - scatter.Color = Colors.SteelBlue; - scatter.LineWidth = 2; - scatter.MarkerSize = 6; - } - - plt.Font.Automatic(); - _curveChart.Refresh(); - } - - private void SaveFromUi() - { - try - { - var rpmArgs = SplitArgs(_txtFanRpmArgs.Text); - if (rpmArgs.Length == 0) - { - MessageBox.Show(this, Resources.ErrRpmArgsEmpty, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; - } - - var autoArgs = SplitArgs(_txtAutoFanArgs.Text); - if (autoArgs.Length == 0) - { - MessageBox.Show(this, Resources.ErrAutoFanArgsEmpty, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; - } - - var langBefore = _config.Language; - _config = CloneConfigFromUi(); - _config.SaveUser(); - _fan?.UpdateConfig(_config); - var msg = Resources.MsgSaved; - if (langBefore != _config.Language) - msg = Resources.MsgRestartToApplyLanguage; - MessageBox.Show(this, msg, Text, MessageBoxButtons.OK, MessageBoxIcon.Information); - } - catch (Exception ex) - { - MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); - } - } - - private void FanOnSample(object? sender, FanSampleEventArgs e) - { - if (IsDisposed) - return; - if (InvokeRequired) - BeginInvoke(new MethodInvoker(() => OnSampleUi(e))); - else - OnSampleUi(e); - } - - private void OnSampleUi(FanSampleEventArgs e) - { - var sec = (e.Time.ToUniversalTime() - _chartAnchor).TotalSeconds; - var temp = e.TempC ?? double.NaN; - var duty = (e.ActualDutyPercent ?? e.TargetDutyPercent) is { } d ? d : double.NaN; - var rpm = e.Rpm.HasValue ? e.Rpm.Value : double.NaN; - - lock (_seriesLock) - { - _tSec.Add(sec); - _temps.Add(temp); - _duties.Add(duty); - _rpms.Add(rpm); - TrimSeries(); - } - - var tStr = e.TempC.HasValue ? $"{e.TempC:F1} °C" : "—"; - var dVal = e.ActualDutyPercent ?? e.TargetDutyPercent; - var dStr = dVal.HasValue ? $"{dVal} %" : "—"; - var rStr = e.Rpm.HasValue ? $"{e.Rpm} RPM" : "—"; - var state = e.Paused ? Resources.StatusPaused : e.FailSafe ? Resources.StatusFailSafe : Resources.StatusRunning; - _lblStatus.Text = - $"{state} {Resources.LabelTemp} {tStr} {Resources.LabelTargetDuty} {dStr} {Resources.LabelRpm} {rStr} ectool: {e.LastEctoolMessage ?? "—"}"; - - _tray.Text = $"{Resources.AppTitle} {tStr} {dStr}"; - - RebuildChart(); - } - - private void TrimSeries() - { - var maxPts = _config.ChartMaxPoints; - var winSec = Math.Max(60, _config.ChartHistoryMinutes * 60.0); - while (_tSec.Count > 0 && _tSec.Count > maxPts) - { - _tSec.RemoveAt(0); - _temps.RemoveAt(0); - _duties.RemoveAt(0); - _rpms.RemoveAt(0); - } - - if (_tSec.Count == 0) - return; - var last = _tSec[^1]; - while (_tSec.Count > 0 && last - _tSec[0] > winSec) - { - _tSec.RemoveAt(0); - _temps.RemoveAt(0); - _duties.RemoveAt(0); - _rpms.RemoveAt(0); - } - } - - private void RebuildChart() - { - double[] x, t, d, r; - lock (_seriesLock) - { - x = _tSec.ToArray(); - t = _temps.ToArray(); - d = _duties.ToArray(); - r = _rpms.ToArray(); - } - - var plt = _chart.Plot; - plt.Clear(); - - plt.Axes.Left.Label.Text = Resources.ChartTemp; - plt.Axes.Bottom.Label.Text = Resources.ChartTime; - plt.Axes.Right.IsVisible = true; - plt.Axes.Right.Label.Text = Resources.ChartDuty; - - if (_yRpmAxis == null) - _yRpmAxis = plt.Axes.AddRightAxis(); - _yRpmAxis.Label.Text = Resources.ChartRpm; - - if (x.Length > 0) - { - var st = plt.Add.Scatter(x, t); - st.Axes.YAxis = plt.Axes.Left; - st.LegendText = string.Empty; - st.Color = Colors.Blue; - st.LineWidth = 2; - st.MarkerSize = 0; - - var sd = plt.Add.Scatter(x, d); - sd.Axes.YAxis = plt.Axes.Right; - sd.LegendText = string.Empty; - sd.Color = Colors.Orange; - sd.LineWidth = 2; - sd.MarkerSize = 0; - - var sr = plt.Add.Scatter(x, r); - sr.Axes.YAxis = _yRpmAxis; - sr.LegendText = string.Empty; - sr.Color = Colors.Green; - sr.LineWidth = 2; - sr.MarkerSize = 0; - - var tempValid = t.Where(v => !double.IsNaN(v)).ToArray(); - if (tempValid.Length > 0) - { - var tMin = tempValid.Min(); - var tMax = tempValid.Max(); - var pad = Math.Max(5, (tMax - tMin) * 0.1); - plt.Axes.SetLimitsY(Math.Max(0, tMin - pad), tMax + pad, plt.Axes.Left); - } - else - { - plt.Axes.SetLimitsY(0, 100, plt.Axes.Left); - } - - plt.Axes.SetLimitsY(0, 100, plt.Axes.Right); - - var rpmValid = r.Where(v => !double.IsNaN(v) && v > 0).ToArray(); - if (rpmValid.Length > 0) - { - var rMin = rpmValid.Min(); - var rMax = rpmValid.Max(); - var rpmRange = rMax - rMin; - var pad = Math.Max(500, Math.Max(rpmRange * 0.2, rMax * 0.15)); - plt.Axes.SetLimitsY(Math.Max(0, rMin - pad * 0.2), rMax + pad, _yRpmAxis); - } - else - { - plt.Axes.SetLimitsY(0, 6000, _yRpmAxis); - } - - var xMin = x[0]; - var xMax = x[^1]; - var range = xMax - xMin; - var xMargin = Math.Min(10, Math.Max(2, range * 0.03)); - plt.Axes.SetLimitsX(Math.Max(0, xMin - xMargin * 0.5), xMax + xMargin); - } - else - { - plt.Axes.SetLimitsY(0, 6000, _yRpmAxis); - } - - LegendItem[] leg = - [ - new() { LineColor = Colors.Blue, LineWidth = 2, LabelText = Resources.LegendCelsius }, - new() { LineColor = Colors.Orange, LineWidth = 2, LabelText = Resources.LegendDuty }, - new() { LineColor = Colors.Green, LineWidth = 2, LabelText = Resources.ChartRpm } - ]; - plt.ShowLegend(leg, Alignment.UpperRight); - plt.Legend.Orientation = ScottPlot.Orientation.Horizontal; - plt.Font.Automatic(); - _chart.Refresh(); - } - - private void MainForm_FormClosing(object? sender, FormClosingEventArgs e) - { - _tray.Visible = false; - _tray.Dispose(); - - try - { - _fan?.Stop(); - var cfg = _config; - if (File.Exists(cfg.EctoolPath)) - EctoolRunner.RunSync(cfg.EctoolPath, cfg.AutoFanCtrlArgs); - } - catch - { - /* best effort */ - } - - _fan?.Dispose(); - } - - protected override void OnResize(EventArgs e) - { - base.OnResize(e); - if (WindowState == FormWindowState.Minimized && _minimizeToTray) - { - Hide(); - ShowInTaskbar = false; - } - } -} +using ChromeboxFanControl.Properties; +using ScottPlot; +using ScottPlot.WinForms; + +namespace ChromeboxFanControl; + +public sealed class MainForm : Form +{ + private AppConfig _config = AppConfig.LoadMerged(); + private FanController _fan = null!; + private readonly FormsPlot _chart = new() { Dock = DockStyle.Fill }; + private readonly FormsPlot _curveChart = new() { Dock = DockStyle.Fill }; + private readonly System.Windows.Forms.Label _lblStatus = new(); + private readonly DataGridView _gridCurve = new(); + private readonly TextBox _txtEctool = new(); + private readonly NumericUpDown _nudPoll = new(); + private readonly NumericUpDown _nudRpmEvery = new(); + private readonly TextBox _txtFanRpmArgs = new(); + private readonly TextBox _txtFanDutyArgs = new(); + private readonly TextBox _txtAutoFanArgs = new(); + private readonly ComboBox _cmbTempSource = new(); + private readonly ComboBox _cmbLanguage = new(); + private readonly NumericUpDown _nudFailCount = new(); + private readonly NumericUpDown _nudFailPct = new(); + private readonly CheckBox _chkFailAuto = new(); + private readonly NumericUpDown _nudChartMin = new(); + private readonly NumericUpDown _nudChartPoints = new(); + private readonly NumericUpDown _nudRampUpSteps = new(); + private readonly NumericUpDown _nudRampUpMinDelta = new(); + private readonly NotifyIcon _tray = new(); + private readonly ContextMenuStrip _trayMenu = new(); + private readonly ToolStripMenuItem _miPause; + private readonly object _seriesLock = new(); + private readonly List _tSec = new(); + private readonly List _temps = new(); + private readonly List _duties = new(); + private readonly List _rpms = new(); + private DateTime _chartAnchor = DateTime.UtcNow; + private bool _minimizeToTray = true; + private ScottPlot.IYAxis? _yRpmAxis; + + public MainForm() + { + _miPause = new ToolStripMenuItem(Resources.TrayPause); + Text = Resources.AppTitle; + Size = new Size(960, 700); + StartPosition = FormStartPosition.CenterScreen; + MinimumSize = new Size(800, 500); + + BuildUi(); + WireTray(); + + Load += (_, _) => + { + _config = AppConfig.LoadMerged(); + _chartAnchor = DateTime.UtcNow; + ApplyConfigToUi(); + _fan = new FanController(CloneConfigFromUi()); + _fan.Sample += FanOnSample; + _fan.Start(); + }; + + FormClosing += MainForm_FormClosing; + } + + private void BuildUi() + { + var tabs = new TabControl { Dock = DockStyle.Fill }; + + var tabMonitor = new TabPage(Resources.TabMonitor); + var tabCurve = new TabPage(Resources.TabCurve); + var tabAdv = new TabPage(Resources.TabAdvanced); + + tabs.TabPages.Add(tabMonitor); + tabs.TabPages.Add(tabCurve); + tabs.TabPages.Add(tabAdv); + + _lblStatus.AutoSize = false; + _lblStatus.Height = 28; + _lblStatus.Dock = DockStyle.Bottom; + _lblStatus.Padding = new Padding(8, 4, 8, 4); + _lblStatus.AutoEllipsis = true; + _lblStatus.Text = Resources.StatusStarting; + + var panelChart = new Panel { Dock = DockStyle.Fill }; + panelChart.Controls.Add(_chart); + panelChart.Controls.Add(_lblStatus); + + tabMonitor.Controls.Add(panelChart); + + _gridCurve.Dock = DockStyle.Top; + _gridCurve.Height = 260; + _gridCurve.AutoGenerateColumns = false; + _gridCurve.AllowUserToAddRows = false; + _gridCurve.RowHeadersVisible = false; + _gridCurve.Columns.Add(new DataGridViewTextBoxColumn + { + Name = "colTemp", + HeaderText = Resources.GridColTemp, + ReadOnly = true, + Width = 80 + }); + _gridCurve.Columns.Add(new DataGridViewTextBoxColumn + { + Name = "colDuty", + HeaderText = Resources.GridColDuty, + Width = 100 + }); + for (var i = 0; i < 14; i++) + _gridCurve.Rows.Add(FanCurve.TempBreakpoints[i].ToString(), "0"); + _gridCurve.CellValueChanged += (_, _) => RebuildCurveChart(); + + var panelCurveBtns = new FlowLayoutPanel + { + Dock = DockStyle.Bottom, + AutoSize = true, + FlowDirection = FlowDirection.LeftToRight, + Padding = new Padding(8) + }; + var btnSave = new Button { Text = Resources.BtnSaveApply, AutoSize = true }; + btnSave.Click += (_, _) => SaveFromUi(); + var btnDefault = new Button { Text = Resources.BtnRestoreDefault, AutoSize = true }; + btnDefault.Click += (_, _) => + { + var def = new AppConfig(); + for (var i = 0; i < 14; i++) + _gridCurve.Rows[i].Cells[1].Value = def.CurvePoints[i].ToString(); + RebuildCurveChart(); + }; + panelCurveBtns.Controls.Add(btnSave); + panelCurveBtns.Controls.Add(btnDefault); + + var panelCurveTop = new Panel { Dock = DockStyle.Top, Height = 360 }; + panelCurveTop.Controls.Add(_gridCurve); + panelCurveTop.Controls.Add(panelCurveBtns); + + var panelCurve = new Panel { Dock = DockStyle.Fill }; + panelCurve.Controls.Add(_curveChart); + panelCurve.Controls.Add(panelCurveTop); + tabCurve.Controls.Add(panelCurve); + + var flp = new TableLayoutPanel + { + Dock = DockStyle.Fill, + ColumnCount = 2, + RowCount = 15, + Padding = new Padding(12) + }; + flp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 35)); + flp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 65)); + for (var i = 0; i < 15; i++) + flp.RowStyles.Add(new RowStyle(SizeType.AutoSize)); + + var row = 0; + AddRow(flp, row++, Resources.LblLanguage, _cmbLanguage); + _cmbLanguage.DropDownStyle = ComboBoxStyle.DropDownList; + _cmbLanguage.Items.AddRange([Resources.LangAuto, Resources.LangEn, Resources.LangZhHans, Resources.LangZhHant]); + + AddRow(flp, row++, Resources.LblEctoolPath, _txtEctool); + AddRow(flp, row++, Resources.LblPollInterval, _nudPoll); + _nudPoll.Minimum = 250; + _nudPoll.Maximum = 60_000; + _nudPoll.Increment = 250; + + AddRow(flp, row++, Resources.LblRpmEvery, _nudRpmEvery); + _nudRpmEvery.Minimum = 1; + _nudRpmEvery.Maximum = 100; + + AddRow(flp, row++, Resources.LblFanRpmArgs, _txtFanRpmArgs); + AddRow(flp, row++, Resources.LblFanDutyArgs, _txtFanDutyArgs); + + AddRow(flp, row++, Resources.LblAutoFanArgs, _txtAutoFanArgs); + + AddRow(flp, row++, Resources.LblTempSource, _cmbTempSource); + _cmbTempSource.DropDownStyle = ComboBoxStyle.DropDownList; + _cmbTempSource.Items.AddRange(["AverageCore", "MaxCore"]); + + AddRow(flp, row++, Resources.LblFailCount, _nudFailCount); + _nudFailCount.Minimum = 1; + _nudFailCount.Maximum = 100; + + AddRow(flp, row++, Resources.LblFailPct, _nudFailPct); + _nudFailPct.Minimum = 0; + _nudFailPct.Maximum = 100; + + _chkFailAuto.Text = Resources.ChkFailAuto; + _chkFailAuto.AutoSize = true; + var failPanel = new FlowLayoutPanel + { + FlowDirection = FlowDirection.LeftToRight, + AutoSize = true, + Dock = DockStyle.Fill, + WrapContents = false + }; + failPanel.Controls.Add(_chkFailAuto); + flp.Controls.Add(new System.Windows.Forms.Label { Text = Resources.LblFailStrategy, AutoSize = true, Anchor = AnchorStyles.Left }, 0, row); + flp.Controls.Add(failPanel, 1, row++); + + AddRow(flp, row++, Resources.LblChartMin, _nudChartMin); + _nudChartMin.Minimum = 1; + _nudChartMin.Maximum = 120; + + AddRow(flp, row++, Resources.LblChartPoints, _nudChartPoints); + _nudChartPoints.Minimum = 100; + _nudChartPoints.Maximum = 10_000; + _nudChartPoints.Increment = 100; + + AddRow(flp, row++, Resources.LblRampUpSteps, _nudRampUpSteps); + _nudRampUpSteps.Minimum = 1; + _nudRampUpSteps.Maximum = 10; + + AddRow(flp, row++, Resources.LblRampUpMinDeltaPercent, _nudRampUpMinDelta); + _nudRampUpMinDelta.Minimum = 0; + _nudRampUpMinDelta.Maximum = 50; + + var btnSaveAdv = new Button { Text = Resources.BtnSaveAdvanced, AutoSize = true, Dock = DockStyle.Bottom }; + btnSaveAdv.Click += (_, _) => SaveFromUi(); + tabAdv.Controls.Add(btnSaveAdv); + tabAdv.Controls.Add(flp); + + Controls.Add(tabs); + } + + private static void AddRow(TableLayoutPanel t, int row, string label, Control editor) + { + t.Controls.Add(new System.Windows.Forms.Label { Text = label, AutoSize = true, Anchor = AnchorStyles.Left | AnchorStyles.Top }, 0, row); + editor.Dock = DockStyle.Fill; + t.Controls.Add(editor, 1, row); + } + + private void WireTray() + { + _tray.Text = Resources.AppTitle; + _tray.Visible = true; + _tray.Icon = SystemIcons.Application; + _tray.DoubleClick += (_, _) => ShowFromTray(); + _miPause.Click += (_, _) => + { + TogglePause(); + }; + _trayMenu.Items.Add(Resources.TrayOpen, null, (_, _) => ShowFromTray()); + _trayMenu.Items.Add(_miPause); + _trayMenu.Items.Add(Resources.TrayAbout, null, (_, _) => + MessageBox.Show(this, Resources.AboutMessage, Text, MessageBoxButtons.OK, MessageBoxIcon.Information)); + _trayMenu.Items.Add(Resources.TrayExit, null, (_, _) => Close()); + _tray.ContextMenuStrip = _trayMenu; + } + + private void ShowFromTray() + { + Show(); + WindowState = FormWindowState.Normal; + ShowInTaskbar = true; + Activate(); + } + + private void TogglePause() + { + if (_fan == null) + return; + _fan.Paused = !_fan.Paused; + _miPause.Text = _fan.Paused ? Resources.TrayResume : Resources.TrayPause; + } + + private static readonly string[] LanguageValues = ["auto", "en", "zh-Hans", "zh-Hant"]; + + private void ApplyConfigToUi() + { + var langIdx = Array.IndexOf(LanguageValues, _config.Language); + _cmbLanguage.SelectedIndex = langIdx >= 0 ? langIdx : 0; + + _txtEctool.Text = _config.EctoolPath; + _nudPoll.Value = _config.PollIntervalMs; + _nudRpmEvery.Value = _config.FanRpmPollEveryNCycles; + _txtFanRpmArgs.Text = string.Join(' ', _config.FanRpmArgs); + _txtFanDutyArgs.Text = _config.FanDutyArgs is { Length: > 0 } da ? string.Join(' ', da) : ""; + _txtAutoFanArgs.Text = string.Join(' ', _config.AutoFanCtrlArgs); + _cmbTempSource.SelectedItem = _config.TempSource; + if (_cmbTempSource.SelectedIndex < 0) + _cmbTempSource.SelectedIndex = 0; + _nudFailCount.Value = _config.FailSafeAfterConsecutiveErrors; + _nudFailPct.Value = _config.FailSafeFanPercent; + _chkFailAuto.Checked = _config.FailSafeRestoreAutoFan; + _nudChartMin.Value = _config.ChartHistoryMinutes; + _nudChartPoints.Value = _config.ChartMaxPoints; + _nudRampUpSteps.Value = _config.RampUpSteps; + _nudRampUpMinDelta.Value = _config.RampUpMinDeltaPercent; + for (var i = 0; i < 14; i++) + _gridCurve.Rows[i].Cells[1].Value = _config.CurvePoints[i].ToString(); + RebuildCurveChart(); + } + + private AppConfig CloneConfigFromUi() + { + var langIdx = _cmbLanguage.SelectedIndex; + var lang = langIdx >= 0 && langIdx < LanguageValues.Length ? LanguageValues[langIdx] : "auto"; + + var c = new AppConfig + { + Language = lang, + EctoolPath = _txtEctool.Text.Trim(), + PollIntervalMs = (int)_nudPoll.Value, + FanRpmPollEveryNCycles = (int)_nudRpmEvery.Value, + FanRpmArgs = SplitArgs(_txtFanRpmArgs.Text), + FanDutyArgs = SplitArgs(_txtFanDutyArgs.Text) is { Length: > 0 } fa ? fa : null, + AutoFanCtrlArgs = SplitArgs(_txtAutoFanArgs.Text), + TempSource = _cmbTempSource.SelectedItem?.ToString() ?? "AverageCore", + FailSafeAfterConsecutiveErrors = (int)_nudFailCount.Value, + FailSafeFanPercent = (int)_nudFailPct.Value, + FailSafeRestoreAutoFan = _chkFailAuto.Checked, + ChartHistoryMinutes = (int)_nudChartMin.Value, + ChartMaxPoints = (int)_nudChartPoints.Value, + RampUpSteps = (int)_nudRampUpSteps.Value, + RampUpMinDeltaPercent = (int)_nudRampUpMinDelta.Value, + CurvePoints = ReadCurveFromGrid() + }; + return c; + } + + private static string[] SplitArgs(string line) => + string.IsNullOrWhiteSpace(line) + ? [] + : line.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries); + + private int[] ReadCurveFromGrid() + { + var pts = new int[14]; + for (var i = 0; i < 14; i++) + { + var cell = _gridCurve.Rows[i].Cells[1].Value?.ToString(); + if (!int.TryParse(cell, out var v)) + v = 0; + pts[i] = Math.Clamp(v, 0, 100); + } + + return pts; + } + + private void RebuildCurveChart() + { + var temps = FanCurve.TempBreakpoints.Select(t => (double)t).ToArray(); + var duties = ReadCurveFromGrid().Select(d => (double)d).ToArray(); + + var plt = _curveChart.Plot; + plt.Clear(); + plt.Axes.Left.Label.Text = Resources.CurveChartDuty; + plt.Axes.Bottom.Label.Text = Resources.CurveChartTemp; + plt.Axes.SetLimitsX(0, 100); + plt.Axes.SetLimitsY(0, 100); + + if (temps.Length > 0 && duties.Length > 0) + { + var scatter = plt.Add.Scatter(temps, duties); + scatter.Color = Colors.SteelBlue; + scatter.LineWidth = 2; + scatter.MarkerSize = 6; + } + + plt.Font.Automatic(); + _curveChart.Refresh(); + } + + private void SaveFromUi() + { + try + { + var rpmArgs = SplitArgs(_txtFanRpmArgs.Text); + if (rpmArgs.Length == 0) + { + MessageBox.Show(this, Resources.ErrRpmArgsEmpty, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + var autoArgs = SplitArgs(_txtAutoFanArgs.Text); + if (autoArgs.Length == 0) + { + MessageBox.Show(this, Resources.ErrAutoFanArgsEmpty, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + var langBefore = _config.Language; + _config = CloneConfigFromUi(); + _config.SaveUser(); + _fan?.UpdateConfig(_config); + var msg = Resources.MsgSaved; + if (langBefore != _config.Language) + msg = Resources.MsgRestartToApplyLanguage; + MessageBox.Show(this, msg, Text, MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + + private void FanOnSample(object? sender, FanSampleEventArgs e) + { + if (IsDisposed) + return; + if (InvokeRequired) + BeginInvoke(new MethodInvoker(() => OnSampleUi(e))); + else + OnSampleUi(e); + } + + private void OnSampleUi(FanSampleEventArgs e) + { + var sec = (e.Time.ToUniversalTime() - _chartAnchor).TotalSeconds; + var temp = e.TempC ?? double.NaN; + var duty = (e.ActualDutyPercent ?? e.TargetDutyPercent) is { } d ? d : double.NaN; + var rpm = e.Rpm.HasValue ? e.Rpm.Value : double.NaN; + + lock (_seriesLock) + { + _tSec.Add(sec); + _temps.Add(temp); + _duties.Add(duty); + _rpms.Add(rpm); + TrimSeries(); + } + + var tStr = e.TempC.HasValue ? $"{e.TempC:F1} °C" : "—"; + var dVal = e.ActualDutyPercent ?? e.TargetDutyPercent; + var dStr = dVal.HasValue ? $"{dVal} %" : "—"; + var rStr = e.Rpm.HasValue ? $"{e.Rpm} RPM" : "—"; + var state = e.Paused ? Resources.StatusPaused : e.FailSafe ? Resources.StatusFailSafe : Resources.StatusRunning; + _lblStatus.Text = + $"{state} {Resources.LabelTemp} {tStr} {Resources.LabelTargetDuty} {dStr} {Resources.LabelRpm} {rStr} ectool: {e.LastEctoolMessage ?? "—"}"; + + _tray.Text = $"{Resources.AppTitle} {tStr} {dStr}"; + + RebuildChart(); + } + + private void TrimSeries() + { + var maxPts = _config.ChartMaxPoints; + var winSec = Math.Max(60, _config.ChartHistoryMinutes * 60.0); + while (_tSec.Count > 0 && _tSec.Count > maxPts) + { + _tSec.RemoveAt(0); + _temps.RemoveAt(0); + _duties.RemoveAt(0); + _rpms.RemoveAt(0); + } + + if (_tSec.Count == 0) + return; + var last = _tSec[^1]; + while (_tSec.Count > 0 && last - _tSec[0] > winSec) + { + _tSec.RemoveAt(0); + _temps.RemoveAt(0); + _duties.RemoveAt(0); + _rpms.RemoveAt(0); + } + } + + private void RebuildChart() + { + double[] x, t, d, r; + lock (_seriesLock) + { + x = _tSec.ToArray(); + t = _temps.ToArray(); + d = _duties.ToArray(); + r = _rpms.ToArray(); + } + + var plt = _chart.Plot; + plt.Clear(); + + plt.Axes.Left.Label.Text = Resources.ChartTemp; + plt.Axes.Bottom.Label.Text = Resources.ChartTime; + plt.Axes.Right.IsVisible = true; + plt.Axes.Right.Label.Text = Resources.ChartDuty; + + if (_yRpmAxis == null) + _yRpmAxis = plt.Axes.AddRightAxis(); + _yRpmAxis.Label.Text = Resources.ChartRpm; + + if (x.Length > 0) + { + var st = plt.Add.Scatter(x, t); + st.Axes.YAxis = plt.Axes.Left; + st.LegendText = string.Empty; + st.Color = Colors.Blue; + st.LineWidth = 2; + st.MarkerSize = 0; + + var sd = plt.Add.Scatter(x, d); + sd.Axes.YAxis = plt.Axes.Right; + sd.LegendText = string.Empty; + sd.Color = Colors.Orange; + sd.LineWidth = 2; + sd.MarkerSize = 0; + + var sr = plt.Add.Scatter(x, r); + sr.Axes.YAxis = _yRpmAxis; + sr.LegendText = string.Empty; + sr.Color = Colors.Green; + sr.LineWidth = 2; + sr.MarkerSize = 0; + + var tempValid = t.Where(v => !double.IsNaN(v)).ToArray(); + if (tempValid.Length > 0) + { + var tMin = tempValid.Min(); + var tMax = tempValid.Max(); + var pad = Math.Max(5, (tMax - tMin) * 0.1); + plt.Axes.SetLimitsY(Math.Max(0, tMin - pad), tMax + pad, plt.Axes.Left); + } + else + { + plt.Axes.SetLimitsY(0, 100, plt.Axes.Left); + } + + plt.Axes.SetLimitsY(0, 100, plt.Axes.Right); + + var rpmValid = r.Where(v => !double.IsNaN(v) && v > 0).ToArray(); + if (rpmValid.Length > 0) + { + var rMin = rpmValid.Min(); + var rMax = rpmValid.Max(); + var rpmRange = rMax - rMin; + var pad = Math.Max(500, Math.Max(rpmRange * 0.2, rMax * 0.15)); + plt.Axes.SetLimitsY(Math.Max(0, rMin - pad * 0.2), rMax + pad, _yRpmAxis); + } + else + { + plt.Axes.SetLimitsY(0, 6000, _yRpmAxis); + } + + var xMin = x[0]; + var xMax = x[^1]; + var range = xMax - xMin; + var xMargin = Math.Min(10, Math.Max(2, range * 0.03)); + plt.Axes.SetLimitsX(Math.Max(0, xMin - xMargin * 0.5), xMax + xMargin); + } + else + { + plt.Axes.SetLimitsY(0, 6000, _yRpmAxis); + } + + LegendItem[] leg = + [ + new() { LineColor = Colors.Blue, LineWidth = 2, LabelText = Resources.LegendCelsius }, + new() { LineColor = Colors.Orange, LineWidth = 2, LabelText = Resources.LegendDuty }, + new() { LineColor = Colors.Green, LineWidth = 2, LabelText = Resources.ChartRpm } + ]; + plt.ShowLegend(leg, Alignment.UpperRight); + plt.Legend.Orientation = ScottPlot.Orientation.Horizontal; + plt.Font.Automatic(); + _chart.Refresh(); + } + + private void MainForm_FormClosing(object? sender, FormClosingEventArgs e) + { + _tray.Visible = false; + _tray.Dispose(); + + try + { + _fan?.Stop(); + var cfg = _config; + if (File.Exists(cfg.EctoolPath)) + EctoolRunner.RunSync(cfg.EctoolPath, cfg.AutoFanCtrlArgs); + } + catch + { + /* best effort */ + } + + _fan?.Dispose(); + } + + protected override void OnResize(EventArgs e) + { + base.OnResize(e); + if (WindowState == FormWindowState.Minimized && _minimizeToTray) + { + Hide(); + ShowInTaskbar = false; + } + } +} diff --git a/ChromeboxFanControl/Program.cs b/ChromeboxFanControl/Program.cs index 296d9c8..b321013 100644 --- a/ChromeboxFanControl/Program.cs +++ b/ChromeboxFanControl/Program.cs @@ -1,27 +1,27 @@ -namespace ChromeboxFanControl; - -internal static class Program -{ - [STAThread] - private static void Main() - { - ApplicationConfiguration.Initialize(); - - var config = AppConfig.LoadMerged(); - var lang = config.Language; - if (string.IsNullOrEmpty(lang) || lang.Equals("auto", StringComparison.OrdinalIgnoreCase)) - { - var culture = System.Globalization.CultureInfo.CurrentUICulture; - if (culture.Name is "zh-TW" or "zh-HK" or "zh-MO" or "zh-Hant") - lang = "zh-Hant"; - else if (culture.Name.StartsWith("zh", StringComparison.OrdinalIgnoreCase)) - lang = "zh-Hans"; - else - lang = "en"; - } - - System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); - - Application.Run(new MainForm()); - } -} +namespace ChromeboxFanControl; + +internal static class Program +{ + [STAThread] + private static void Main() + { + ApplicationConfiguration.Initialize(); + + var config = AppConfig.LoadMerged(); + var lang = config.Language; + if (string.IsNullOrEmpty(lang) || lang.Equals("auto", StringComparison.OrdinalIgnoreCase)) + { + var culture = System.Globalization.CultureInfo.CurrentUICulture; + if (culture.Name is "zh-TW" or "zh-HK" or "zh-MO" or "zh-Hant") + lang = "zh-Hant"; + else if (culture.Name.StartsWith("zh", StringComparison.OrdinalIgnoreCase)) + lang = "zh-Hans"; + else + lang = "en"; + } + + System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); + + Application.Run(new MainForm()); + } +} diff --git a/ChromeboxFanControl/Properties/Resources.Designer.cs b/ChromeboxFanControl/Properties/Resources.Designer.cs index 3cadd83..095195c 100644 --- a/ChromeboxFanControl/Properties/Resources.Designer.cs +++ b/ChromeboxFanControl/Properties/Resources.Designer.cs @@ -1,65 +1,65 @@ -using System.Resources; - -namespace ChromeboxFanControl.Properties; - -internal static class Resources -{ - private static readonly ResourceManager _rm = new( - "ChromeboxFanControl.Properties.Resources", - typeof(Resources).Assembly); - - public static string AppTitle => _rm.GetString("AppTitle") ?? "Chromebox Fan Control"; - public static string TabMonitor => _rm.GetString("TabMonitor") ?? "Monitoring"; - public static string TabCurve => _rm.GetString("TabCurve") ?? "Curve"; - public static string TabAdvanced => _rm.GetString("TabAdvanced") ?? "Advanced"; - public static string StatusStarting => _rm.GetString("StatusStarting") ?? "Starting…"; - public static string StatusRunning => _rm.GetString("StatusRunning") ?? "Running"; - public static string StatusPaused => _rm.GetString("StatusPaused") ?? "Paused"; - public static string StatusFailSafe => _rm.GetString("StatusFailSafe") ?? "Fail-safe mode"; - public static string GridColTemp => _rm.GetString("GridColTemp") ?? "Temp (°C)"; - public static string GridColDuty => _rm.GetString("GridColDuty") ?? "Duty 0–100"; - public static string BtnSaveApply => _rm.GetString("BtnSaveApply") ?? "Save & Apply"; - public static string BtnRestoreDefault => _rm.GetString("BtnRestoreDefault") ?? "Restore Default"; - public static string LblEctoolPath => _rm.GetString("LblEctoolPath") ?? "ectool path"; - public static string LblPollInterval => _rm.GetString("LblPollInterval") ?? "Poll interval (ms)"; - public static string LblRpmEvery => _rm.GetString("LblRpmEvery") ?? "Read RPM every N"; - public static string LblFanRpmArgs => _rm.GetString("LblFanRpmArgs") ?? "RPM args"; - public static string LblFanDutyArgs => _rm.GetString("LblFanDutyArgs") ?? "Duty args"; - public static string LblAutoFanArgs => _rm.GetString("LblAutoFanArgs") ?? "Auto fan args"; - public static string LblTempSource => _rm.GetString("LblTempSource") ?? "Temp source"; - public static string LblFailCount => _rm.GetString("LblFailCount") ?? "Fail count"; - public static string LblFailPct => _rm.GetString("LblFailPct") ?? "Fail-safe %"; - public static string ChkFailAuto => _rm.GetString("ChkFailAuto") ?? "Use autofanctrl"; - public static string LblFailStrategy => _rm.GetString("LblFailStrategy") ?? "Fail-safe strategy"; - public static string LblChartMin => _rm.GetString("LblChartMin") ?? "Chart minutes"; - public static string LblChartPoints => _rm.GetString("LblChartPoints") ?? "Chart points"; - public static string LblRampUpSteps => _rm.GetString("LblRampUpSteps") ?? "Ramp-up steps"; - public static string LblRampUpMinDeltaPercent => _rm.GetString("LblRampUpMinDeltaPercent") ?? "Min ramp delta (%)"; - public static string BtnSaveAdvanced => _rm.GetString("BtnSaveAdvanced") ?? "Save Advanced"; - public static string TrayOpen => _rm.GetString("TrayOpen") ?? "Show window"; - public static string TrayPause => _rm.GetString("TrayPause") ?? "Pause"; - public static string TrayResume => _rm.GetString("TrayResume") ?? "Resume"; - public static string TrayAbout => _rm.GetString("TrayAbout") ?? "About"; - public static string TrayExit => _rm.GetString("TrayExit") ?? "Exit"; - public static string AboutMessage => _rm.GetString("AboutMessage") ?? ""; - public static string ErrRpmArgsEmpty => _rm.GetString("ErrRpmArgsEmpty") ?? "RPM args required."; - public static string ErrAutoFanArgsEmpty => _rm.GetString("ErrAutoFanArgsEmpty") ?? "Auto fan args required."; - public static string MsgSaved => _rm.GetString("MsgSaved") ?? "Saved."; - public static string LabelTemp => _rm.GetString("LabelTemp") ?? "Temp:"; - public static string LabelTargetDuty => _rm.GetString("LabelTargetDuty") ?? "Target duty:"; - public static string LabelRpm => _rm.GetString("LabelRpm") ?? "RPM:"; - public static string ChartTemp => _rm.GetString("ChartTemp") ?? "Temperature (°C)"; - public static string ChartTime => _rm.GetString("ChartTime") ?? "Time (s)"; - public static string ChartDuty => _rm.GetString("ChartDuty") ?? "Duty (%)"; - public static string ChartRpm => _rm.GetString("ChartRpm") ?? "RPM"; - public static string LegendCelsius => _rm.GetString("LegendCelsius") ?? "°C"; - public static string LegendDuty => _rm.GetString("LegendDuty") ?? "Duty %"; - public static string CurveChartDuty => _rm.GetString("CurveChartDuty") ?? "Duty (%)"; - public static string CurveChartTemp => _rm.GetString("CurveChartTemp") ?? "Temp (°C)"; - public static string LblLanguage => _rm.GetString("LblLanguage") ?? "Language"; - public static string LangAuto => _rm.GetString("LangAuto") ?? "Auto"; - public static string LangEn => _rm.GetString("LangEn") ?? "English"; - public static string LangZhHans => _rm.GetString("LangZhHans") ?? "Simplified Chinese"; - public static string LangZhHant => _rm.GetString("LangZhHant") ?? "Traditional Chinese"; - public static string MsgRestartToApplyLanguage => _rm.GetString("MsgRestartToApplyLanguage") ?? "Restart to apply."; -} +using System.Resources; + +namespace ChromeboxFanControl.Properties; + +internal static class Resources +{ + private static readonly ResourceManager _rm = new( + "ChromeboxFanControl.Properties.Resources", + typeof(Resources).Assembly); + + public static string AppTitle => _rm.GetString("AppTitle") ?? "Chromebox Fan Control"; + public static string TabMonitor => _rm.GetString("TabMonitor") ?? "Monitoring"; + public static string TabCurve => _rm.GetString("TabCurve") ?? "Curve"; + public static string TabAdvanced => _rm.GetString("TabAdvanced") ?? "Advanced"; + public static string StatusStarting => _rm.GetString("StatusStarting") ?? "Starting…"; + public static string StatusRunning => _rm.GetString("StatusRunning") ?? "Running"; + public static string StatusPaused => _rm.GetString("StatusPaused") ?? "Paused"; + public static string StatusFailSafe => _rm.GetString("StatusFailSafe") ?? "Fail-safe mode"; + public static string GridColTemp => _rm.GetString("GridColTemp") ?? "Temp (°C)"; + public static string GridColDuty => _rm.GetString("GridColDuty") ?? "Duty 0–100"; + public static string BtnSaveApply => _rm.GetString("BtnSaveApply") ?? "Save & Apply"; + public static string BtnRestoreDefault => _rm.GetString("BtnRestoreDefault") ?? "Restore Default"; + public static string LblEctoolPath => _rm.GetString("LblEctoolPath") ?? "ectool path"; + public static string LblPollInterval => _rm.GetString("LblPollInterval") ?? "Poll interval (ms)"; + public static string LblRpmEvery => _rm.GetString("LblRpmEvery") ?? "Read RPM every N"; + public static string LblFanRpmArgs => _rm.GetString("LblFanRpmArgs") ?? "RPM args"; + public static string LblFanDutyArgs => _rm.GetString("LblFanDutyArgs") ?? "Duty args"; + public static string LblAutoFanArgs => _rm.GetString("LblAutoFanArgs") ?? "Auto fan args"; + public static string LblTempSource => _rm.GetString("LblTempSource") ?? "Temp source"; + public static string LblFailCount => _rm.GetString("LblFailCount") ?? "Fail count"; + public static string LblFailPct => _rm.GetString("LblFailPct") ?? "Fail-safe %"; + public static string ChkFailAuto => _rm.GetString("ChkFailAuto") ?? "Use autofanctrl"; + public static string LblFailStrategy => _rm.GetString("LblFailStrategy") ?? "Fail-safe strategy"; + public static string LblChartMin => _rm.GetString("LblChartMin") ?? "Chart minutes"; + public static string LblChartPoints => _rm.GetString("LblChartPoints") ?? "Chart points"; + public static string LblRampUpSteps => _rm.GetString("LblRampUpSteps") ?? "Ramp-up steps"; + public static string LblRampUpMinDeltaPercent => _rm.GetString("LblRampUpMinDeltaPercent") ?? "Min ramp delta (%)"; + public static string BtnSaveAdvanced => _rm.GetString("BtnSaveAdvanced") ?? "Save Advanced"; + public static string TrayOpen => _rm.GetString("TrayOpen") ?? "Show window"; + public static string TrayPause => _rm.GetString("TrayPause") ?? "Pause"; + public static string TrayResume => _rm.GetString("TrayResume") ?? "Resume"; + public static string TrayAbout => _rm.GetString("TrayAbout") ?? "About"; + public static string TrayExit => _rm.GetString("TrayExit") ?? "Exit"; + public static string AboutMessage => _rm.GetString("AboutMessage") ?? ""; + public static string ErrRpmArgsEmpty => _rm.GetString("ErrRpmArgsEmpty") ?? "RPM args required."; + public static string ErrAutoFanArgsEmpty => _rm.GetString("ErrAutoFanArgsEmpty") ?? "Auto fan args required."; + public static string MsgSaved => _rm.GetString("MsgSaved") ?? "Saved."; + public static string LabelTemp => _rm.GetString("LabelTemp") ?? "Temp:"; + public static string LabelTargetDuty => _rm.GetString("LabelTargetDuty") ?? "Target duty:"; + public static string LabelRpm => _rm.GetString("LabelRpm") ?? "RPM:"; + public static string ChartTemp => _rm.GetString("ChartTemp") ?? "Temperature (°C)"; + public static string ChartTime => _rm.GetString("ChartTime") ?? "Time (s)"; + public static string ChartDuty => _rm.GetString("ChartDuty") ?? "Duty (%)"; + public static string ChartRpm => _rm.GetString("ChartRpm") ?? "RPM"; + public static string LegendCelsius => _rm.GetString("LegendCelsius") ?? "°C"; + public static string LegendDuty => _rm.GetString("LegendDuty") ?? "Duty %"; + public static string CurveChartDuty => _rm.GetString("CurveChartDuty") ?? "Duty (%)"; + public static string CurveChartTemp => _rm.GetString("CurveChartTemp") ?? "Temp (°C)"; + public static string LblLanguage => _rm.GetString("LblLanguage") ?? "Language"; + public static string LangAuto => _rm.GetString("LangAuto") ?? "Auto"; + public static string LangEn => _rm.GetString("LangEn") ?? "English"; + public static string LangZhHans => _rm.GetString("LangZhHans") ?? "Simplified Chinese"; + public static string LangZhHant => _rm.GetString("LangZhHant") ?? "Traditional Chinese"; + public static string MsgRestartToApplyLanguage => _rm.GetString("MsgRestartToApplyLanguage") ?? "Restart to apply."; +} diff --git a/ChromeboxFanControl/Properties/Resources.resx b/ChromeboxFanControl/Properties/Resources.resx index f801ab0..c0547ad 100644 --- a/ChromeboxFanControl/Properties/Resources.resx +++ b/ChromeboxFanControl/Properties/Resources.resx @@ -1,119 +1,119 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - Chromebox Fan Control - Monitoring - Curve - Advanced - Starting… - Running - Paused - Fail-safe mode - Temp (°C) - Duty 0–100 - Save & Apply - Restore Default Curve - ectool path - Poll interval (ms) - Read RPM every N cycles - RPM args (space-separated) - Duty args (empty=skip) - Restore auto fan args - Temp source - Consecutive errors before fail-safe - Fail-safe fixed duty (%) - Use autofanctrl in fail-safe (not fixed duty) - Fail-safe strategy - Chart history (minutes) - Chart max points - Ramp-up steps - Min ramp delta (%) - Save Advanced Settings - Show window - Pause control - Resume control - About - Exit - Chromebox Fan Control - -Uses LibreHardwareMonitor to read CPU temperature, controls fan via crosec ectool. - -Run as Administrator. - RPM args cannot be empty. - Restore auto fan args cannot be empty. - Saved. - Temp: - Target duty: - RPM: - Temperature (°C) - Time (s) - Duty (%) - RPM - °C - Duty % - Duty (%) - Temp (°C) - Language - Auto (follow system) - English - Simplified Chinese - Traditional Chinese - Language changed. Restart the application to apply. - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Chromebox Fan Control + Monitoring + Curve + Advanced + Starting… + Running + Paused + Fail-safe mode + Temp (°C) + Duty 0–100 + Save & Apply + Restore Default Curve + ectool path + Poll interval (ms) + Read RPM every N cycles + RPM args (space-separated) + Duty args (empty=skip) + Restore auto fan args + Temp source + Consecutive errors before fail-safe + Fail-safe fixed duty (%) + Use autofanctrl in fail-safe (not fixed duty) + Fail-safe strategy + Chart history (minutes) + Chart max points + Ramp-up steps + Min ramp delta (%) + Save Advanced Settings + Show window + Pause control + Resume control + About + Exit + Chromebox Fan Control + +Uses LibreHardwareMonitor to read CPU temperature, controls fan via crosec ectool. + +Run as Administrator. + RPM args cannot be empty. + Restore auto fan args cannot be empty. + Saved. + Temp: + Target duty: + RPM: + Temperature (°C) + Time (s) + Duty (%) + RPM + °C + Duty % + Duty (%) + Temp (°C) + Language + Auto (follow system) + English + Simplified Chinese + Traditional Chinese + Language changed. Restart the application to apply. + diff --git a/ChromeboxFanControl/Properties/Resources.zh-Hans.resx b/ChromeboxFanControl/Properties/Resources.zh-Hans.resx index 7f9692c..1ce7757 100644 --- a/ChromeboxFanControl/Properties/Resources.zh-Hans.resx +++ b/ChromeboxFanControl/Properties/Resources.zh-Hans.resx @@ -1,119 +1,119 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - Chromebox 风扇温控 - 监控 - 曲线 - 高级 - 启动中… - 运行中 - 已暂停 - 安全模式 - 温度 (°C) - 占空比 0–100 - 保存并应用 - 恢复默认曲线 - ectool 路径 - 轮询间隔 (ms) - 每 N 轮读 RPM - 读转速参数 (空格分隔) - 读占空比参数 (空=跳过) - 恢复自动风扇参数 - 温度来源 - 连续失败进入安全模式 (次) - 安全模式固定占空比 (%) - 安全模式使用 autofanctrl(不用固定占空比) - 安全模式策略 - 图表保留时长 (分钟) - 图表最大点数 - 升速步数 - 最小 ramp 增幅 (%) - 保存高级设置 - 打开主窗口 - 暂停控制 - 恢复控制 - 关于 - 退出 - Chromebox 风扇温控 - -使用 LibreHardwareMonitor 读取 CPU 温度,通过 crosec ectool 控制风扇。 - -请以管理员身份运行。 - 读转速参数不能为空。 - 恢复自动风扇参数不能为空。 - 已保存。 - 温度: - 目标占空比: - 转速: - 温度 (°C) - 时间 (s) - 占空比 (%) - 转速 - °C - 占空比 % - 占空比 (%) - 温度 (°C) - 语言 - 跟随系统 - English - 简体中文 - 繁体中文 - 语言已更改,请重启应用以生效。 - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Chromebox 风扇温控 + 监控 + 曲线 + 高级 + 启动中… + 运行中 + 已暂停 + 安全模式 + 温度 (°C) + 占空比 0–100 + 保存并应用 + 恢复默认曲线 + ectool 路径 + 轮询间隔 (ms) + 每 N 轮读 RPM + 读转速参数 (空格分隔) + 读占空比参数 (空=跳过) + 恢复自动风扇参数 + 温度来源 + 连续失败进入安全模式 (次) + 安全模式固定占空比 (%) + 安全模式使用 autofanctrl(不用固定占空比) + 安全模式策略 + 图表保留时长 (分钟) + 图表最大点数 + 升速步数 + 最小 ramp 增幅 (%) + 保存高级设置 + 打开主窗口 + 暂停控制 + 恢复控制 + 关于 + 退出 + Chromebox 风扇温控 + +使用 LibreHardwareMonitor 读取 CPU 温度,通过 crosec ectool 控制风扇。 + +请以管理员身份运行。 + 读转速参数不能为空。 + 恢复自动风扇参数不能为空。 + 已保存。 + 温度: + 目标占空比: + 转速: + 温度 (°C) + 时间 (s) + 占空比 (%) + 转速 + °C + 占空比 % + 占空比 (%) + 温度 (°C) + 语言 + 跟随系统 + English + 简体中文 + 繁体中文 + 语言已更改,请重启应用以生效。 + diff --git a/ChromeboxFanControl/Properties/Resources.zh-Hant.resx b/ChromeboxFanControl/Properties/Resources.zh-Hant.resx index 44777f8..0906c16 100644 --- a/ChromeboxFanControl/Properties/Resources.zh-Hant.resx +++ b/ChromeboxFanControl/Properties/Resources.zh-Hant.resx @@ -1,119 +1,119 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - Chromebox 風扇溫控 - 監控 - 曲線 - 進階 - 啟動中… - 運行中 - 已暫停 - 安全模式 - 溫度 (°C) - 占空比 0–100 - 儲存並套用 - 恢復預設曲線 - ectool 路徑 - 輪詢間隔 (ms) - 每 N 輪讀 RPM - 讀轉速參數 (空格分隔) - 讀占空比參數 (空=略過) - 恢復自動風扇參數 - 溫度來源 - 連續失敗進入安全模式 (次) - 安全模式固定占空比 (%) - 安全模式使用 autofanctrl(不用固定占空比) - 安全模式策略 - 圖表保留時長 (分鐘) - 圖表最大點數 - 升速步數 - 最小 ramp 增幅 (%) - 儲存進階設定 - 開啟主視窗 - 暫停控制 - 恢復控制 - 關於 - 結束 - Chromebox 風扇溫控 - -使用 LibreHardwareMonitor 讀取 CPU 溫度,透過 crosec ectool 控制風扇。 - -請以系統管理員身分執行。 - 讀轉速參數不能為空。 - 恢復自動風扇參數不能為空。 - 已儲存。 - 溫度: - 目標占空比: - 轉速: - 溫度 (°C) - 時間 (s) - 占空比 (%) - 轉速 - °C - 占空比 % - 占空比 (%) - 溫度 (°C) - 語言 - 跟隨系統 - English - 簡體中文 - 繁體中文 - 語言已變更,請重新啟動應用程式以生效。 - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Chromebox 風扇溫控 + 監控 + 曲線 + 進階 + 啟動中… + 運行中 + 已暫停 + 安全模式 + 溫度 (°C) + 占空比 0–100 + 儲存並套用 + 恢復預設曲線 + ectool 路徑 + 輪詢間隔 (ms) + 每 N 輪讀 RPM + 讀轉速參數 (空格分隔) + 讀占空比參數 (空=略過) + 恢復自動風扇參數 + 溫度來源 + 連續失敗進入安全模式 (次) + 安全模式固定占空比 (%) + 安全模式使用 autofanctrl(不用固定占空比) + 安全模式策略 + 圖表保留時長 (分鐘) + 圖表最大點數 + 升速步數 + 最小 ramp 增幅 (%) + 儲存進階設定 + 開啟主視窗 + 暫停控制 + 恢復控制 + 關於 + 結束 + Chromebox 風扇溫控 + +使用 LibreHardwareMonitor 讀取 CPU 溫度,透過 crosec ectool 控制風扇。 + +請以系統管理員身分執行。 + 讀轉速參數不能為空。 + 恢復自動風扇參數不能為空。 + 已儲存。 + 溫度: + 目標占空比: + 轉速: + 溫度 (°C) + 時間 (s) + 占空比 (%) + 轉速 + °C + 占空比 % + 占空比 (%) + 溫度 (°C) + 語言 + 跟隨系統 + English + 簡體中文 + 繁體中文 + 語言已變更,請重新啟動應用程式以生效。 + diff --git a/ChromeboxFanControl/app.manifest b/ChromeboxFanControl/app.manifest index 9b248c5..f884719 100644 --- a/ChromeboxFanControl/app.manifest +++ b/ChromeboxFanControl/app.manifest @@ -1,16 +1,16 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/ChromeboxFanControl/appsettings.json b/ChromeboxFanControl/appsettings.json index bfb5ee3..c39b067 100644 --- a/ChromeboxFanControl/appsettings.json +++ b/ChromeboxFanControl/appsettings.json @@ -1,17 +1,17 @@ -{ - "EctoolPath": "C:\\Program Files\\crosec\\ectool.exe", - "PollIntervalMs": 1500, - "FanRpmPollEveryNCycles": 1, - "FanRpmArgs": [ "pwmgetfanrpm", "0" ], - "FanDutyArgs": null, - "AutoFanCtrlArgs": [ "autofanctrl" ], - "TempSource": "AverageCore", - "RampUpSteps": 3, - "RampUpMinDeltaPercent": 20, - "FailSafeAfterConsecutiveErrors": 5, - "FailSafeFanPercent": 100, - "FailSafeRestoreAutoFan": false, - "ChartHistoryMinutes": 15, - "ChartMaxPoints": 900, - "CurvePoints": [ 0, 0, 10, 25, 40, 55, 70, 80, 90, 95, 97, 98, 99, 100 ] -} +{ + "EctoolPath": "C:\\Program Files\\crosec\\ectool.exe", + "PollIntervalMs": 1500, + "FanRpmPollEveryNCycles": 1, + "FanRpmArgs": [ "pwmgetfanrpm", "0" ], + "FanDutyArgs": null, + "AutoFanCtrlArgs": [ "autofanctrl" ], + "TempSource": "AverageCore", + "RampUpSteps": 3, + "RampUpMinDeltaPercent": 20, + "FailSafeAfterConsecutiveErrors": 5, + "FailSafeFanPercent": 100, + "FailSafeRestoreAutoFan": false, + "ChartHistoryMinutes": 15, + "ChartMaxPoints": 900, + "CurvePoints": [ 0, 0, 10, 25, 40, 55, 70, 80, 90, 95, 97, 98, 99, 100 ] +} diff --git a/ChromeboxFanControlService/ChromeboxFanControlService.csproj b/ChromeboxFanControlService/ChromeboxFanControlService.csproj index d56dcac..c60cc83 100644 --- a/ChromeboxFanControlService/ChromeboxFanControlService.csproj +++ b/ChromeboxFanControlService/ChromeboxFanControlService.csproj @@ -1,27 +1,27 @@ - - - - $(NoWarn);NU1701 - net8.0-windows - enable - enable - ChromeboxFanControlService - ChromeboxFanControlService - Exe - - - - - - - - - - - - - - - - - + + + + $(NoWarn);NU1701 + net8.0-windows + enable + enable + ChromeboxFanControlService + ChromeboxFanControlService + Exe + + + + + + + + + + + + + + + + + diff --git a/ChromeboxFanControlService/FanControlWorker.cs b/ChromeboxFanControlService/FanControlWorker.cs index b9f9619..e17cf89 100644 --- a/ChromeboxFanControlService/FanControlWorker.cs +++ b/ChromeboxFanControlService/FanControlWorker.cs @@ -1,43 +1,43 @@ -using ChromeboxFanControl; - -namespace ChromeboxFanControlService; - -public sealed class FanControlWorker : BackgroundService -{ - private FanController? _fan; - private AppConfig? _config; - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - _config = AppConfig.LoadMerged(); - _fan = new FanController(_config); - _fan.Start(); - - try - { - await Task.Delay(Timeout.Infinite, stoppingToken); - } - catch (OperationCanceledException) - { - /* shutdown */ - } - } - - public override async Task StopAsync(CancellationToken cancellationToken) - { - _fan?.Stop(); - _fan?.Dispose(); - if (_config != null) - { - try - { - EctoolRunner.RunSync(_config.EctoolPath, _config.AutoFanCtrlArgs); - } - catch - { - /* best effort restore autofan on shutdown */ - } - } - await base.StopAsync(cancellationToken); - } -} +using ChromeboxFanControl; + +namespace ChromeboxFanControlService; + +public sealed class FanControlWorker : BackgroundService +{ + private FanController? _fan; + private AppConfig? _config; + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + _config = AppConfig.LoadMerged(); + _fan = new FanController(_config); + _fan.Start(); + + try + { + await Task.Delay(Timeout.Infinite, stoppingToken); + } + catch (OperationCanceledException) + { + /* shutdown */ + } + } + + public override async Task StopAsync(CancellationToken cancellationToken) + { + _fan?.Stop(); + _fan?.Dispose(); + if (_config != null) + { + try + { + EctoolRunner.RunSync(_config.EctoolPath, _config.AutoFanCtrlArgs); + } + catch + { + /* best effort restore autofan on shutdown */ + } + } + await base.StopAsync(cancellationToken); + } +} diff --git a/ChromeboxFanControlService/Program.cs b/ChromeboxFanControlService/Program.cs index ce209b5..f480e19 100644 --- a/ChromeboxFanControlService/Program.cs +++ b/ChromeboxFanControlService/Program.cs @@ -1,16 +1,16 @@ -using ChromeboxFanControlService; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -var host = Host.CreateDefaultBuilder(args) - .UseWindowsService(options => - { - options.ServiceName = "ChromeboxFanControl"; - }) - .ConfigureServices(services => - { - services.AddHostedService(); - }) - .Build(); - -await host.RunAsync(); +using ChromeboxFanControlService; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +var host = Host.CreateDefaultBuilder(args) + .UseWindowsService(options => + { + options.ServiceName = "ChromeboxFanControl"; + }) + .ConfigureServices(services => + { + services.AddHostedService(); + }) + .Build(); + +await host.RunAsync(); diff --git a/ChromeboxFanControlService/appsettings.json b/ChromeboxFanControlService/appsettings.json index 2996c3f..9851a56 100644 --- a/ChromeboxFanControlService/appsettings.json +++ b/ChromeboxFanControlService/appsettings.json @@ -1,15 +1,15 @@ -{ - "EctoolPath": "C:\\Program Files\\crosec\\ectool.exe", - "PollIntervalMs": 1500, - "FanRpmPollEveryNCycles": 1, - "FanRpmArgs": [ "pwmgetfanrpm", "0" ], - "FanDutyArgs": null, - "AutoFanCtrlArgs": [ "autofanctrl" ], - "TempSource": "AverageCore", - "FailSafeAfterConsecutiveErrors": 5, - "FailSafeFanPercent": 100, - "FailSafeRestoreAutoFan": false, - "ChartHistoryMinutes": 15, - "ChartMaxPoints": 900, - "CurvePoints": [ 0, 0, 10, 25, 40, 55, 70, 80, 90, 95, 97, 98, 99, 100 ] -} +{ + "EctoolPath": "C:\\Program Files\\crosec\\ectool.exe", + "PollIntervalMs": 1500, + "FanRpmPollEveryNCycles": 1, + "FanRpmArgs": [ "pwmgetfanrpm", "0" ], + "FanDutyArgs": null, + "AutoFanCtrlArgs": [ "autofanctrl" ], + "TempSource": "AverageCore", + "FailSafeAfterConsecutiveErrors": 5, + "FailSafeFanPercent": 100, + "FailSafeRestoreAutoFan": false, + "ChartHistoryMinutes": 15, + "ChartMaxPoints": 900, + "CurvePoints": [ 0, 0, 10, 25, 40, 55, 70, 80, 90, 95, 97, 98, 99, 100 ] +} diff --git a/FanControl.ChromeboxEC/ChromeboxECControlSensor.cs b/FanControl.ChromeboxEC/ChromeboxECControlSensor.cs index 8e3a13d..6cae651 100644 --- a/FanControl.ChromeboxEC/ChromeboxECControlSensor.cs +++ b/FanControl.ChromeboxEC/ChromeboxECControlSensor.cs @@ -1,41 +1,41 @@ -using FanControl.Plugins; - -namespace FanControl.ChromeboxEC; - -/// 风扇控制:ectool fanduty <percent> -public sealed class ChromeboxECControlSensor : IPluginControlSensor2 -{ - private readonly string _ectoolPath; - private readonly string[] _autoFanArgs; - private float? _value; - - public ChromeboxECControlSensor(string ectoolPath, string[] autoFanArgs) - { - _ectoolPath = ectoolPath; - _autoFanArgs = autoFanArgs; - } - - public string Id => "ChromeboxEC_Control"; - public string Name => "Chromebox EC Fan"; - public string Origin => "ectool fanduty"; - public float? Value => _value; - public string? PairedFanSensorId => "ChromeboxEC_Fan"; - - /// 设置风扇占空比 0–100,调用 ectool fanduty - public void Set(float val) - { - _value = val; - var duty = (int)Math.Clamp(Math.Round(val), 0, 100); - EctoolRunner.Run(_ectoolPath, ["fanduty", duty.ToString()]); - } - - /// 禁用控制时恢复 EC 自动风扇,调用 ectool autofanctrl - public void Reset() - { - _value = null; - if (_autoFanArgs is { Length: > 0 }) - EctoolRunner.Run(_ectoolPath, _autoFanArgs); - } - - public void Update() { } -} +using FanControl.Plugins; + +namespace FanControl.ChromeboxEC; + +/// 风扇控制:ectool fanduty <percent> +public sealed class ChromeboxECControlSensor : IPluginControlSensor2 +{ + private readonly string _ectoolPath; + private readonly string[] _autoFanArgs; + private float? _value; + + public ChromeboxECControlSensor(string ectoolPath, string[] autoFanArgs) + { + _ectoolPath = ectoolPath; + _autoFanArgs = autoFanArgs; + } + + public string Id => "ChromeboxEC_Control"; + public string Name => "Chromebox EC Fan"; + public string Origin => "ectool fanduty"; + public float? Value => _value; + public string? PairedFanSensorId => "ChromeboxEC_Fan"; + + /// 设置风扇占空比 0–100,调用 ectool fanduty + public void Set(float val) + { + _value = val; + var duty = (int)Math.Clamp(Math.Round(val), 0, 100); + EctoolRunner.Run(_ectoolPath, ["fanduty", duty.ToString()]); + } + + /// 禁用控制时恢复 EC 自动风扇,调用 ectool autofanctrl + public void Reset() + { + _value = null; + if (_autoFanArgs is { Length: > 0 }) + EctoolRunner.Run(_ectoolPath, _autoFanArgs); + } + + public void Update() { } +} diff --git a/FanControl.ChromeboxEC/ChromeboxECPlugin.cs b/FanControl.ChromeboxEC/ChromeboxECPlugin.cs index 4e6f5ba..cc215b2 100644 --- a/FanControl.ChromeboxEC/ChromeboxECPlugin.cs +++ b/FanControl.ChromeboxEC/ChromeboxECPlugin.cs @@ -1,41 +1,41 @@ -using FanControl.Plugins; - -namespace FanControl.ChromeboxEC; - -/// -/// Fan Control 插件:通过 ectool 读取 Chromebox EC 温度、控制风扇。 -/// 命令参见 docs/ectool-commands-zh.md -/// -public sealed class ChromeboxECPlugin : IPlugin -{ - private PluginConfig? _config; - private bool _available; - - public string Name => "Chromebox EC"; - - public void Initialize() - { - _config = PluginConfig.Load(); - _available = !string.IsNullOrWhiteSpace(_config.EctoolPath) && - File.Exists(_config.EctoolPath); - } - - public void Load(IPluginSensorsContainer container) - { - if (!_available || _config == null) - return; - - var tempArgs = _config.TempArgs is { Length: > 0 } ta ? ta : ["temps", "0"]; - var rpmArgs = _config.FanRpmArgs is { Length: > 0 } ra ? ra : ["pwmgetfanrpm", "0"]; - - container.TempSensors.Add(new ChromeboxECTempSensor(_config.EctoolPath, tempArgs)); - container.FanSensors.Add(new ChromeboxECFanSensor(_config.EctoolPath, rpmArgs)); - container.ControlSensors.Add(new ChromeboxECControlSensor(_config.EctoolPath, _config.AutoFanCtrlArgs)); - } - - public void Close() - { - _config = null; - _available = false; - } -} +using FanControl.Plugins; + +namespace FanControl.ChromeboxEC; + +/// +/// Fan Control 插件:通过 ectool 读取 Chromebox EC 温度、控制风扇。 +/// 命令参见 docs/ectool-commands-zh.md +/// +public sealed class ChromeboxECPlugin : IPlugin +{ + private PluginConfig? _config; + private bool _available; + + public string Name => "Chromebox EC"; + + public void Initialize() + { + _config = PluginConfig.Load(); + _available = !string.IsNullOrWhiteSpace(_config.EctoolPath) && + File.Exists(_config.EctoolPath); + } + + public void Load(IPluginSensorsContainer container) + { + if (!_available || _config == null) + return; + + var tempArgs = _config.TempArgs is { Length: > 0 } ta ? ta : ["temps", "0"]; + var rpmArgs = _config.FanRpmArgs is { Length: > 0 } ra ? ra : ["pwmgetfanrpm", "0"]; + + container.TempSensors.Add(new ChromeboxECTempSensor(_config.EctoolPath, tempArgs)); + container.FanSensors.Add(new ChromeboxECFanSensor(_config.EctoolPath, rpmArgs)); + container.ControlSensors.Add(new ChromeboxECControlSensor(_config.EctoolPath, _config.AutoFanCtrlArgs)); + } + + public void Close() + { + _config = null; + _available = false; + } +} diff --git a/FanControl.ChromeboxEC/ChromeboxECTempSensor.cs b/FanControl.ChromeboxEC/ChromeboxECTempSensor.cs index 95ec6e4..f2993eb 100644 --- a/FanControl.ChromeboxEC/ChromeboxECTempSensor.cs +++ b/FanControl.ChromeboxEC/ChromeboxECTempSensor.cs @@ -1,27 +1,27 @@ -using FanControl.Plugins; - -namespace FanControl.ChromeboxEC; - -/// 温度:ectool temps <sensorid> -public sealed class ChromeboxECTempSensor : IPluginSensor -{ - private readonly string _ectoolPath; - private readonly string[] _tempArgs; - - public ChromeboxECTempSensor(string ectoolPath, string[] tempArgs) - { - _ectoolPath = ectoolPath; - _tempArgs = tempArgs; - } - - public string Id => "ChromeboxEC_Temp"; - public string Name => "Chromebox EC Temperature"; - public string Origin => "ectool temps"; - public float? Value { get; private set; } - - public void Update() - { - var (ok, stdout, _) = EctoolRunner.Run(_ectoolPath, _tempArgs); - Value = ok && EctoolRunner.TryParseTemp(stdout) is { } temp ? temp : null; - } -} +using FanControl.Plugins; + +namespace FanControl.ChromeboxEC; + +/// 温度:ectool temps <sensorid> +public sealed class ChromeboxECTempSensor : IPluginSensor +{ + private readonly string _ectoolPath; + private readonly string[] _tempArgs; + + public ChromeboxECTempSensor(string ectoolPath, string[] tempArgs) + { + _ectoolPath = ectoolPath; + _tempArgs = tempArgs; + } + + public string Id => "ChromeboxEC_Temp"; + public string Name => "Chromebox EC Temperature"; + public string Origin => "ectool temps"; + public float? Value { get; private set; } + + public void Update() + { + var (ok, stdout, _) = EctoolRunner.Run(_ectoolPath, _tempArgs); + Value = ok && EctoolRunner.TryParseTemp(stdout) is { } temp ? temp : null; + } +} diff --git a/FanControl.ChromeboxEC/EctoolRunner.cs b/FanControl.ChromeboxEC/EctoolRunner.cs index c7f1e7e..6f77def 100644 --- a/FanControl.ChromeboxEC/EctoolRunner.cs +++ b/FanControl.ChromeboxEC/EctoolRunner.cs @@ -76,15 +76,27 @@ internal static class EctoolRunner return null; } - /// 解析 temps 输出为摄氏温度。支持 "315 K (= 42 C)" 等格式,多传感器时取最高值。 + /// 解析 temps 输出为摄氏温度。支持 "329 K (= 56 C)" 等格式,多传感器时取最高值。排除 ratio 行中的 (313 K and 333 K) 等阈值。 public static float? TryParseTemp(string stdout) { if (string.IsNullOrWhiteSpace(stdout)) return null; float? best = null; - // 匹配 "315 K" 或 "315 K (= 42 C)" - foreach (Match m in Regex.Matches(stdout, @"(\d{2,3})\s*K\b", RegexOptions.IgnoreCase)) + // 匹配 "= 56 C":最可靠,且不会被 ratio 行的 (313 K and 333 K) 干扰 + foreach (Match m in Regex.Matches(stdout, @"=\s*(\d{1,3})\s*[Cc]\b")) + { + if (int.TryParse(m.Groups[1].Value, out var c) && c is >= 0 and <= 120) + { + if (best == null || c > best) + best = c; + } + } + if (best != null) + return best; + + // 若无 "= X C",再匹配 "329 K",排除 ratio 中的 "313 K and" 与 "and 333 K" + foreach (Match m in Regex.Matches(stdout, @"(?= 250 and <= 400) { @@ -93,15 +105,6 @@ internal static class EctoolRunner best = c; } } - // 匹配 "= 42 C" 或 "42 C" - foreach (Match m in Regex.Matches(stdout, @"(?:=\s*)?(\d{1,3})\s*[Cc]\b")) - { - if (int.TryParse(m.Groups[1].Value, out var c) && c is >= 0 and <= 120) - { - if (best == null || c > best) - best = c; - } - } return best; } } diff --git a/FanControl.ChromeboxEC/FanControl.ChromeboxEC.csproj b/FanControl.ChromeboxEC/FanControl.ChromeboxEC.csproj index aab1cd4..220b805 100644 --- a/FanControl.ChromeboxEC/FanControl.ChromeboxEC.csproj +++ b/FanControl.ChromeboxEC/FanControl.ChromeboxEC.csproj @@ -1,18 +1,18 @@ - - - - net8.0 - enable - enable - FanControl.ChromeboxEC - FanControl.ChromeboxEC - true - - - - - lib\FanControl.Plugins.dll - - - - + + + + net8.0 + enable + enable + FanControl.ChromeboxEC + FanControl.ChromeboxEC + true + + + + + lib\FanControl.Plugins.dll + + + + diff --git a/FanControl.ChromeboxEC/FanControl.ChromeboxEC.json.example b/FanControl.ChromeboxEC/FanControl.ChromeboxEC.json.example index 1cd1a4a..4949eec 100644 --- a/FanControl.ChromeboxEC/FanControl.ChromeboxEC.json.example +++ b/FanControl.ChromeboxEC/FanControl.ChromeboxEC.json.example @@ -1,6 +1,6 @@ -{ - "EctoolPath": "C:\\Program Files\\crosec\\ectool.exe", - "TempArgs": ["temps", "0"], - "FanRpmArgs": ["pwmgetfanrpm", "0"], - "AutoFanCtrlArgs": ["autofanctrl"] -} +{ + "EctoolPath": "C:\\Program Files\\crosec\\ectool.exe", + "TempArgs": ["temps", "0"], + "FanRpmArgs": ["pwmgetfanrpm", "0"], + "AutoFanCtrlArgs": ["autofanctrl"] +} diff --git a/README.md b/README.md index 3d9efe3..dbc0cc2 100644 --- a/README.md +++ b/README.md @@ -1,191 +1,191 @@ -# Chromebox Fan Control (Windows) - -Read **CPU core temperature** via **LibreHardwareMonitor**, set **`fanduty`** through Coolstar **crosec**'s **ectool**, and read **fan RPM** from ectool. The UI shows three real-time charts: temperature, target duty, and RPM. On Chromebox with coreboot. - -### Background - -My Acer Chromebox CXI4 has only a board-mounted temperature sensor; there is no sensor near the CPU. On Chrome OS, the kernel uses CPU core temperature for fan control and thermal management works well. After flashing coreboot, Linux also uses CPU core temp for fan control. On Windows, however, the system relies on the motherboard temperature sensor for fan control, not CPU temperature. During sudden high load, heat has not yet reached the board sensor when the CPU already overheats, causing Windows to shut down before the fan can respond. I tried configuring coreboot, recompiling the EC firmware, and other approaches—none succeeded in connecting CPU core temperature to Chromebox EC fan control. Hence this project. - -**Also included:** [FanControl.ChromeboxEC](FanControl.ChromeboxEC/) — a plugin for [Fan Control](https://github.com/Rem0o/FanControl.Releases) (Rémi Mercier) that adds Chromebox EC fan control. Use this if you prefer Fan Control’s UI and curve editor. - -### Fan Control setup - -1. **Install plugin**: Copy `FanControl.ChromeboxEC.dll` to Fan Control's `Plugins` folder; start Fan Control as Administrator. -2. **Recommended config**: CPU core temp + board temp (EC Temp) dual curves, take max to set fan. - - Create curve **CPU Package**: temp source = CPU Package, output 0–100% duty. - - Create curve **Board Temp**: temp source = Chromebox EC Temperature, output 0–100% duty. - - Create **Mix** curve: function = Max, inputs = both curves above. - - Set **Chromebox EC Fan** control to use **Mix** curve. - -See [FanControl.ChromeboxEC README](FanControl.ChromeboxEC/README.md) for details. - -## Dependencies - -1. Install [Coolstar CROS-EC driver](https://github.com/coolstar/crwindows) (or your crosec package). The default executable is: - `C:\Program Files\crosec\ectool.exe` - Change it in the program’s **Advanced** tab if needed. -2. **Run as Administrator** (manifest requests elevation; LHM and EC access usually need admin). -3. **Deployment**: `.\build.ps1 -publish` (and with `-msi`) defaults to **self-contained single-file**, **including .NET 8 runtime**. `dist\` has only a few exes. Use `-multiFile` or `-frameworkDependent` to avoid bundling the runtime. - -## Build - -```powershell -.\build.ps1 -``` - -### Publish MSI installer (recommended) - -```powershell -.\build.ps1 -publish -msi -``` - -`-publish` produces `dist\`; **`-msi`** uses WiX to build installers (requires WiX). Two MSIs are produced: - -| File | Description | -|------|-------------| -| `dist-installer\ChromeboxFanControl-Setup.msi` | **Desktop**: GUI + optional Windows service (checkbox in feature tree) | -| `dist-installer\ChromeboxFanControlService-Setup.msi` | **Service only**: background service for headless deployment | - -Build machine needs [.NET 8 SDK](https://dotnet.microsoft.com/download) and [WiX Toolset 3](https://wixtoolset.org/docs/wix3/). **Close ChromeboxFanControl.exe before building.** - -Run the MSI **as Administrator**. If ectool is not installed yet, see [Chrultrabook ectool installation](https://docs.chrultrabook.com/docs/installing/ectool.html). See also [ectool 命令参考(中文)](docs/ectool-commands-zh.md). In service mode, config is read from `%ProgramData%\ChromeboxFanControl\config.json`. - -### Publish to dist\ only (debugging or manual packaging) - -| Command | Description | -|---------|-------------| -| `.\build.ps1 -publish` | Publish both desktop and service to `dist\` (single-file by default) | -| `.\build.ps1 -publishGui` | Desktop only | -| `.\build.ps1 -publishService` | Service only, merged into `dist\` | -| `.\build.ps1 -publish -multiFile` | Keep multiple DLLs (instead of single exe) | -| `.\build.ps1 -publish -frameworkDependent` | Rely on system .NET 8 (no bundled runtime) | - -## Configuration - -- **`appsettings.json`** beside the install directory: default options. -- **`%AppData%\ChromeboxFanControl\config.json`**: user config; saved from GUI in desktop mode. -- **`%ProgramData%\ChromeboxFanControl\config.json`**: used in Windows service mode; can share config with GUI. - -Important fields: - -| Field | Description | -|-------|-------------| -| `Language` | UI language: `auto` (system), `en`, `zh-Hans`, `zh-Hant`. Restart to apply. | -| `RampUpSteps` | Steps for gradual fan speed change (1–10, default 3). | -| `RampUpMinDeltaPercent` | Change below this % is applied in one step (0–50, default 20). | -| `FanRpmArgs` | ectool args to read RPM (default `pwmgetfanrpm` `0`). Check with `ectool help` for your board. | -| `FanDutyArgs` | ectool args to read duty (e.g. `pwmget` `0`); empty = skip, show target duty. | -| `AutoFanCtrlArgs` | Command to restore EC auto fan on exit (default `autofanctrl`). | -| `TempSource` | `AverageCore` or `MaxCore`. | -| `CurvePoints` | 14 duty values 0–100 for temp breakpoints 0,40,45,…,100°C (linear interpolation). | - -## Usage - -- Tray icon after startup. **Pause control** to stop sending `fanduty`; EC keeps the last state. -- On **exit**, the program tries to run the **auto fan restore** command (see config). -- After several consecutive CPU temp read failures, **fail-safe** mode activates (fixed duty or `autofanctrl`, see UI options). - -## Disclaimer - -Software is provided “as is”. Incorrect fan settings may cause overheating or hardware damage; use at your own risk. - -## License - -Original code written for this project. Fan curve logic follows the public description in Chrultrabook-Tools; no GPL source code was copied. - ---- - -# 中文 / Chinese - -在刷了 coreboot 的 Chromebox 上,用 **LibreHardwareMonitor** 读取 **CPU 核心温度**,通过 Coolstar **crosec** 自带的 **`ectool`** 设置 **`fanduty`**,并从 **ectool** 读取 **风扇转速(RPM)**。主界面提供三条实时曲线:温度、目标占空比、转速。 - -### 项目缘由 - -Acer Chromebox CXI4 仅配有板载温度传感器,CPU 附近没有独立测温点。Chrome OS 和刷 coreboot 后的 Linux 都能用 CPU 核心温度驱动风扇,温控正常;而 Windows 只认主板传感器,高负载下热量未传到主板时 CPU 就已过热,系统直接关机,风扇反应不及。我试过改 coreboot、重编 EC 固件等办法,仍无法让 CPU 温度直接参与 EC 控扇。加之本人曾重编 EC 解除长期 25W 功耗墙以跑高性能,发热压力更大,传统控扇更显不足。于是有了这个项目。 - -**另含** [FanControl.ChromeboxEC](FanControl.ChromeboxEC/) 插件,用于 [Fan Control](https://github.com/Rem0o/FanControl.Releases),可在 Fan Control 中控制 Chromebox EC 风扇。 - -### Fan Control 配置说明 - -1. **安装插件**:将 `FanControl.ChromeboxEC.dll` 复制到 Fan Control 的 `Plugins` 文件夹,以管理员身份启动 Fan Control。 -2. **推荐配置**:CPU 核心温度 + 板载温度(EC Temp)双曲线,取最大值设置风扇。 - - 新建曲线 **CPU Package**:温度源选「CPU Package」,输出 0–100% 占空比。 - - 新建曲线 **Board Temp**:温度源选「Chromebox EC Temperature」,输出 0–100% 占空比。 - - 新建 **Mix** 曲线:函数选「最大」,输入上述两条曲线。 - - 将 **Chromebox EC Fan** 控制的曲线设为 **Mix**。 - -详见 [FanControl.ChromeboxEC 说明](FanControl.ChromeboxEC/README.md)。 - -## 依赖 - -1. 安装 [Coolstar CROS-EC 驱动](https://github.com/coolstar/crwindows)(或您当前使用的 crosec 安装包),确保存在默认可执行文件: - `C:\Program Files\crosec\ectool.exe` - 若路径不同,在程序「高级」选项卡中修改。 -2. **以管理员身份运行**本程序(清单已要求提升权限;LHM 与 EC 通信通常需要管理员)。 -3. **客户机**:`.\build.ps1 -publish`(及带 `-msi` 时)默认**自包含单文件**发布,**已内含 .NET 8 运行时**,`dist\` 仅含数个 exe,无需再装运行时。若需多 DLL 或依赖系统 .NET 8 以减小体积,可加 `-multiFile` 或 `-frameworkDependent`。 - -## 编译 - -```powershell -.\build.ps1 -``` - -### 发布 MSI 安装包(推荐) - -```powershell -.\build.ps1 -publish -msi -``` - -`-publish` 只生成 `dist\`;加上 **`-msi`** 才用 WiX 生成安装包(需已安装 WiX)。输出两个 MSI: - -| 文件 | 说明 | -|------|------| -| `dist-installer\ChromeboxFanControl-Setup.msi` | **桌面版**:GUI + 可选 Windows 服务(功能树中可勾选) | -| `dist-installer\ChromeboxFanControlService-Setup.msi` | **服务版**:仅后台服务,适合无头部署 | - -**构建机**需 [.NET 8 SDK](https://dotnet.microsoft.com/download) 与 [WiX Toolset 3](https://wixtoolset.org/docs/wix3/)。**构建前请关闭正在运行的 ChromeboxFanControl.exe**。 - -以**管理员身份**运行 MSI。若尚未安装 ectool,可访问 [Chrultrabook 的 ectool 安装说明](https://docs.chrultrabook.com/docs/installing/ectool.html)。另见 [ectool 命令参考](docs/ectool-commands-zh.md)。服务模式下配置从 `%ProgramData%\ChromeboxFanControl\config.json` 读取。 - -### 仅发布到 dist\(用于调试或手动打包) - -| 命令 | 说明 | -|------|------| -| `.\build.ps1 -publish` | 同时发布桌面版与服务版到 `dist\`(默认单文件) | -| `.\build.ps1 -publishGui` | 仅发布 GUI | -| `.\build.ps1 -publishService` | 仅发布服务并合并进 `dist\` | -| `.\build.ps1 -publish -multiFile` | 保留多 DLL 输出(默认单 exe) | -| `.\build.ps1 -publish -frameworkDependent` | 依赖系统 .NET 8(无 bundled 运行时) | - -## 配置 - -- 安装目录旁的 **`appsettings.json`**:默认选项。 -- **`%AppData%\ChromeboxFanControl\config.json`**:GUI 模式下保存后覆盖(用户配置)。 -- **`%ProgramData%\ChromeboxFanControl\config.json`**:Windows 服务模式下读取的配置;可与 GUI 共用同一份配置逻辑。 - -重要字段: - -| 项 | 说明 | -|----|------| -| `Language` | 界面语言:`auto`(跟随系统)、`en`、`zh-Hans`、`zh-Hant`。修改后需重启生效。 | -| `RampUpSteps` | 升/降速分步数(1–10,默认 3)。 | -| `RampUpMinDeltaPercent` | 变化幅度低于此百分比则一步到位(0–50,默认 20)。 | -| `FanRpmArgs` | 读取转速时传给 ectool 的参数(默认 `pwmgetfanrpm` `0`),请用本机 `ectool help` 核对。 | -| `FanDutyArgs` | 读取占空比时传给 ectool 的参数(如 `pwmget` `0`);空则跳过,显示目标占空比。 | -| `AutoFanCtrlArgs` | 退出程序时恢复 EC 自动风扇(默认 `autofanctrl`),若命令名不同请修改。 | -| `TempSource` | `AverageCore` 或 `MaxCore`。 | -| `CurvePoints` | 14 个 0–100 的占空比控制点(对应温度断点 0,40,45,…,100°C,线性插值)。 | - -## 使用说明 - -- 启动后任务栏托盘有图标;可「暂停控制」(不再下发 `fanduty`,EC 保持上次状态)。 -- **退出**或关闭前会尝试执行 **自动风扇恢复**命令(见配置)。 -- 若连续多次读不到 CPU 温度,将进入**安全模式**(固定占空比或 `autofanctrl`,见界面选项)。 - -## 免责声明 - -软件按「原样」提供。错误的风扇策略可能导致过热或硬件损坏;请自行承担使用风险。 - -## 许可 - -本项目为实现计划而编写的原创代码;风扇曲线算法行为参考 Chrultrabook-Tools 的公开逻辑描述,未复制其 GPL 源码。 +# Chromebox Fan Control (Windows) + +Read **CPU core temperature** via **LibreHardwareMonitor**, set **`fanduty`** through Coolstar **crosec**'s **ectool**, and read **fan RPM** from ectool. The UI shows three real-time charts: temperature, target duty, and RPM. On Chromebox with coreboot. + +### Background + +My Acer Chromebox CXI4 has only a board-mounted temperature sensor; there is no sensor near the CPU. On Chrome OS, the kernel uses CPU core temperature for fan control and thermal management works well. After flashing coreboot, Linux also uses CPU core temp for fan control. On Windows, however, the system relies on the motherboard temperature sensor for fan control, not CPU temperature. During sudden high load, heat has not yet reached the board sensor when the CPU already overheats, causing Windows to shut down before the fan can respond. I tried configuring coreboot, recompiling the EC firmware, and other approaches—none succeeded in connecting CPU core temperature to Chromebox EC fan control. Hence this project. + +**Also included:** [FanControl.ChromeboxEC](FanControl.ChromeboxEC/) — a plugin for [Fan Control](https://github.com/Rem0o/FanControl.Releases) (Rémi Mercier) that adds Chromebox EC fan control. Use this if you prefer Fan Control’s UI and curve editor. + +### Fan Control setup + +1. **Install plugin**: Copy `FanControl.ChromeboxEC.dll` to Fan Control's `Plugins` folder; start Fan Control as Administrator. +2. **Recommended config**: CPU core temp + board temp (EC Temp) dual curves, take max to set fan. + - Create curve **CPU Package**: temp source = CPU Package, output 0–100% duty. + - Create curve **Board Temp**: temp source = Chromebox EC Temperature, output 0–100% duty. + - Create **Mix** curve: function = Max, inputs = both curves above. + - Set **Chromebox EC Fan** control to use **Mix** curve. + +See [FanControl.ChromeboxEC README](FanControl.ChromeboxEC/README.md) for details. + +## Dependencies + +1. Install [Coolstar CROS-EC driver](https://github.com/coolstar/crwindows) (or your crosec package). The default executable is: + `C:\Program Files\crosec\ectool.exe` + Change it in the program’s **Advanced** tab if needed. +2. **Run as Administrator** (manifest requests elevation; LHM and EC access usually need admin). +3. **Deployment**: `.\build.ps1 -publish` (and with `-msi`) defaults to **self-contained single-file**, **including .NET 8 runtime**. `dist\` has only a few exes. Use `-multiFile` or `-frameworkDependent` to avoid bundling the runtime. + +## Build + +```powershell +.\build.ps1 +``` + +### Publish MSI installer (recommended) + +```powershell +.\build.ps1 -publish -msi +``` + +`-publish` produces `dist\`; **`-msi`** uses WiX to build installers (requires WiX). Two MSIs are produced: + +| File | Description | +|------|-------------| +| `dist-installer\ChromeboxFanControl-Setup.msi` | **Desktop**: GUI + optional Windows service (checkbox in feature tree) | +| `dist-installer\ChromeboxFanControlService-Setup.msi` | **Service only**: background service for headless deployment | + +Build machine needs [.NET 8 SDK](https://dotnet.microsoft.com/download) and [WiX Toolset 3](https://wixtoolset.org/docs/wix3/). **Close ChromeboxFanControl.exe before building.** + +Run the MSI **as Administrator**. If ectool is not installed yet, see [Chrultrabook ectool installation](https://docs.chrultrabook.com/docs/installing/ectool.html). See also [ectool 命令参考(中文)](docs/ectool-commands-zh.md). In service mode, config is read from `%ProgramData%\ChromeboxFanControl\config.json`. + +### Publish to dist\ only (debugging or manual packaging) + +| Command | Description | +|---------|-------------| +| `.\build.ps1 -publish` | Publish both desktop and service to `dist\` (single-file by default) | +| `.\build.ps1 -publishGui` | Desktop only | +| `.\build.ps1 -publishService` | Service only, merged into `dist\` | +| `.\build.ps1 -publish -multiFile` | Keep multiple DLLs (instead of single exe) | +| `.\build.ps1 -publish -frameworkDependent` | Rely on system .NET 8 (no bundled runtime) | + +## Configuration + +- **`appsettings.json`** beside the install directory: default options. +- **`%AppData%\ChromeboxFanControl\config.json`**: user config; saved from GUI in desktop mode. +- **`%ProgramData%\ChromeboxFanControl\config.json`**: used in Windows service mode; can share config with GUI. + +Important fields: + +| Field | Description | +|-------|-------------| +| `Language` | UI language: `auto` (system), `en`, `zh-Hans`, `zh-Hant`. Restart to apply. | +| `RampUpSteps` | Steps for gradual fan speed change (1–10, default 3). | +| `RampUpMinDeltaPercent` | Change below this % is applied in one step (0–50, default 20). | +| `FanRpmArgs` | ectool args to read RPM (default `pwmgetfanrpm` `0`). Check with `ectool help` for your board. | +| `FanDutyArgs` | ectool args to read duty (e.g. `pwmget` `0`); empty = skip, show target duty. | +| `AutoFanCtrlArgs` | Command to restore EC auto fan on exit (default `autofanctrl`). | +| `TempSource` | `AverageCore` or `MaxCore`. | +| `CurvePoints` | 14 duty values 0–100 for temp breakpoints 0,40,45,…,100°C (linear interpolation). | + +## Usage + +- Tray icon after startup. **Pause control** to stop sending `fanduty`; EC keeps the last state. +- On **exit**, the program tries to run the **auto fan restore** command (see config). +- After several consecutive CPU temp read failures, **fail-safe** mode activates (fixed duty or `autofanctrl`, see UI options). + +## Disclaimer + +Software is provided “as is”. Incorrect fan settings may cause overheating or hardware damage; use at your own risk. + +## License + +Original code written for this project. Fan curve logic follows the public description in Chrultrabook-Tools; no GPL source code was copied. + +--- + +# 中文 / Chinese + +在刷了 coreboot 的 Chromebox 上,用 **LibreHardwareMonitor** 读取 **CPU 核心温度**,通过 Coolstar **crosec** 自带的 **`ectool`** 设置 **`fanduty`**,并从 **ectool** 读取 **风扇转速(RPM)**。主界面提供三条实时曲线:温度、目标占空比、转速。 + +### 项目缘由 + +Acer Chromebox CXI4 仅配有板载温度传感器,CPU 附近没有独立测温点。Chrome OS 和刷 coreboot 后的 Linux 都能用 CPU 核心温度驱动风扇,温控正常;而 Windows 只认主板传感器,高负载下热量未传到主板时 CPU 就已过热,系统直接关机,风扇反应不及。我试过改 coreboot、重编 EC 固件等办法,仍无法让 CPU 温度直接参与 EC 控扇。加之本人曾重编 EC 解除长期 25W 功耗墙以跑高性能,发热压力更大,传统控扇更显不足。于是有了这个项目。 + +**另含** [FanControl.ChromeboxEC](FanControl.ChromeboxEC/) 插件,用于 [Fan Control](https://github.com/Rem0o/FanControl.Releases),可在 Fan Control 中控制 Chromebox EC 风扇。 + +### Fan Control 配置说明 + +1. **安装插件**:将 `FanControl.ChromeboxEC.dll` 复制到 Fan Control 的 `Plugins` 文件夹,以管理员身份启动 Fan Control。 +2. **推荐配置**:CPU 核心温度 + 板载温度(EC Temp)双曲线,取最大值设置风扇。 + - 新建曲线 **CPU Package**:温度源选「CPU Package」,输出 0–100% 占空比。 + - 新建曲线 **Board Temp**:温度源选「Chromebox EC Temperature」,输出 0–100% 占空比。 + - 新建 **Mix** 曲线:函数选「最大」,输入上述两条曲线。 + - 将 **Chromebox EC Fan** 控制的曲线设为 **Mix**。 + +详见 [FanControl.ChromeboxEC 说明](FanControl.ChromeboxEC/README.md)。 + +## 依赖 + +1. 安装 [Coolstar CROS-EC 驱动](https://github.com/coolstar/crwindows)(或您当前使用的 crosec 安装包),确保存在默认可执行文件: + `C:\Program Files\crosec\ectool.exe` + 若路径不同,在程序「高级」选项卡中修改。 +2. **以管理员身份运行**本程序(清单已要求提升权限;LHM 与 EC 通信通常需要管理员)。 +3. **客户机**:`.\build.ps1 -publish`(及带 `-msi` 时)默认**自包含单文件**发布,**已内含 .NET 8 运行时**,`dist\` 仅含数个 exe,无需再装运行时。若需多 DLL 或依赖系统 .NET 8 以减小体积,可加 `-multiFile` 或 `-frameworkDependent`。 + +## 编译 + +```powershell +.\build.ps1 +``` + +### 发布 MSI 安装包(推荐) + +```powershell +.\build.ps1 -publish -msi +``` + +`-publish` 只生成 `dist\`;加上 **`-msi`** 才用 WiX 生成安装包(需已安装 WiX)。输出两个 MSI: + +| 文件 | 说明 | +|------|------| +| `dist-installer\ChromeboxFanControl-Setup.msi` | **桌面版**:GUI + 可选 Windows 服务(功能树中可勾选) | +| `dist-installer\ChromeboxFanControlService-Setup.msi` | **服务版**:仅后台服务,适合无头部署 | + +**构建机**需 [.NET 8 SDK](https://dotnet.microsoft.com/download) 与 [WiX Toolset 3](https://wixtoolset.org/docs/wix3/)。**构建前请关闭正在运行的 ChromeboxFanControl.exe**。 + +以**管理员身份**运行 MSI。若尚未安装 ectool,可访问 [Chrultrabook 的 ectool 安装说明](https://docs.chrultrabook.com/docs/installing/ectool.html)。另见 [ectool 命令参考](docs/ectool-commands-zh.md)。服务模式下配置从 `%ProgramData%\ChromeboxFanControl\config.json` 读取。 + +### 仅发布到 dist\(用于调试或手动打包) + +| 命令 | 说明 | +|------|------| +| `.\build.ps1 -publish` | 同时发布桌面版与服务版到 `dist\`(默认单文件) | +| `.\build.ps1 -publishGui` | 仅发布 GUI | +| `.\build.ps1 -publishService` | 仅发布服务并合并进 `dist\` | +| `.\build.ps1 -publish -multiFile` | 保留多 DLL 输出(默认单 exe) | +| `.\build.ps1 -publish -frameworkDependent` | 依赖系统 .NET 8(无 bundled 运行时) | + +## 配置 + +- 安装目录旁的 **`appsettings.json`**:默认选项。 +- **`%AppData%\ChromeboxFanControl\config.json`**:GUI 模式下保存后覆盖(用户配置)。 +- **`%ProgramData%\ChromeboxFanControl\config.json`**:Windows 服务模式下读取的配置;可与 GUI 共用同一份配置逻辑。 + +重要字段: + +| 项 | 说明 | +|----|------| +| `Language` | 界面语言:`auto`(跟随系统)、`en`、`zh-Hans`、`zh-Hant`。修改后需重启生效。 | +| `RampUpSteps` | 升/降速分步数(1–10,默认 3)。 | +| `RampUpMinDeltaPercent` | 变化幅度低于此百分比则一步到位(0–50,默认 20)。 | +| `FanRpmArgs` | 读取转速时传给 ectool 的参数(默认 `pwmgetfanrpm` `0`),请用本机 `ectool help` 核对。 | +| `FanDutyArgs` | 读取占空比时传给 ectool 的参数(如 `pwmget` `0`);空则跳过,显示目标占空比。 | +| `AutoFanCtrlArgs` | 退出程序时恢复 EC 自动风扇(默认 `autofanctrl`),若命令名不同请修改。 | +| `TempSource` | `AverageCore` 或 `MaxCore`。 | +| `CurvePoints` | 14 个 0–100 的占空比控制点(对应温度断点 0,40,45,…,100°C,线性插值)。 | + +## 使用说明 + +- 启动后任务栏托盘有图标;可「暂停控制」(不再下发 `fanduty`,EC 保持上次状态)。 +- **退出**或关闭前会尝试执行 **自动风扇恢复**命令(见配置)。 +- 若连续多次读不到 CPU 温度,将进入**安全模式**(固定占空比或 `autofanctrl`,见界面选项)。 + +## 免责声明 + +软件按「原样」提供。错误的风扇策略可能导致过热或硬件损坏;请自行承担使用风险。 + +## 许可 + +本项目为实现计划而编写的原创代码;风扇曲线算法行为参考 Chrultrabook-Tools 的公开逻辑描述,未复制其 GPL 源码。 diff --git a/docs/ectool-commands-zh.md b/docs/ectool-commands-zh.md index 4a763a1..a8706af 100644 --- a/docs/ectool-commands-zh.md +++ b/docs/ectool-commands-zh.md @@ -1,333 +1,333 @@ -# ectool 命令参考(中文) - -`ectool` 是 Chrome OS 嵌入式控制器(EC)的命令行工具,用于从用户空间与 EC 通信。在刷了 coreboot 的 Chromebox/Chromebook 上,可通过 Coolstar CROS-EC 驱动的 `ectool.exe` 使用。 - -## 基本用法 - -```text -ectool [选项] <命令> [参数] -``` - -### 常用选项 - -| 选项 | 说明 | -|------|------| -| `--dev=n` | 指定设备号 | -| `--interface=dev\|i2c\|lpc` | 指定接口类型 | -| `--i2c_bus=n` | 指定 I2C 总线号(如 `--i2c_bus=7` 使用 /dev/i2c-7),隐含 `--interface=i2c` | -| `--device=vid:pid` | 指定 USB 端点(如 `18d1:5022`) | -| `--name=cros_ec\|cros_fp\|cros_pd\|cros_scp\|cros_ish` | 指定 EC 类型 | -| `--ascii` | 以 ASCII 格式输出 | - ---- - -## 风扇相关命令 - -### fanduty [idx] \ - -强制将风扇 PWM 设为固定占空比(0–100)。 - -```text -ectool fanduty 75 # 所有风扇 75% -ectool fanduty 0 50 # 风扇 0 设为 50% -``` - -### autofanctrl \ - -开启 EC 自动风扇转速控制。取消手动 `fanduty` 后,应调用此命令恢复 EC 自动控速。 - -```text -ectool autofanctrl -``` - -### pwmgetfanrpm [\ | all] - -读取风扇转速(RPM)。 - -```text -ectool pwmgetfanrpm 0 # 读取第 0 号风扇,输出如 "Fan 0 RPM: 2621" -ectool pwmgetfanrpm all # 读取所有风扇 -``` - -### pwmgetnumfans - -显示风扇数量。 - -### pwmsetfanrpm \ - -设定目标风扇转速(RPM)。部分 EC 支持基于目标转速的控制。 - -### pwmgetduty \ | kb | disp - -读取当前 PWM 占空比(16 位,0–65535)。 - -```text -ectool pwmgetduty 0 # 风扇 0,输出如 "Current PWM duty: 50462"(约 77%) -``` -百分比 = 数值 / 65535 × 100 - -### pwmsetduty - -设置 PWM 占空比(16 位)。需配合具体参数使用。 - ---- - -## 温度相关命令 - -### temps \ - -读取指定温度传感器的温度,以及与 `fan_off` / `fan_max` 的比值。 - -```text -ectool temps 0 # 输出如 "Core 336 K (= 63 C) 100% (313 K and 333 K)" -``` - -输出为开尔文(K)及摄氏(C)。传感器 ID 因主板而异,用 `tempsinfo` 查看。 - -### tempsinfo \ - -显示温度传感器信息,用于确认本机支持的传感器 ID 和名称。 - -```text -ectool tempsinfo 0 -``` - ---- - -## 电源与电池 - -### battery - -显示电池信息。 - -### powerinfo - -显示电源相关信息。 - ---- - -## 系统与固件 - -### chipinfo - -显示 EC 芯片信息。 - -### version - -显示 EC 固件版本。 - -### hello - -检测与 EC 的基本通信是否正常。 - -### console - -显示 EC 调试控制台最新输出。 - -### sysinfo [flags\|reset_flags\|firmware_copy] - -显示系统信息。 - ---- - -## 其他常用命令 - -### switches - -显示 EC 开关状态(如盖板、电源等)。 - -### boardversion - -显示板级版本。 - -### flashinfo - -显示 EC Flash 信息。 - -### rtcget / rtcset - -读取/设置 EC 内部 RTC。 - -### led \ \ - -控制 LED 颜色或查询亮度范围。 - ---- - -## 与 Chromebox 风扇温控的对应关系 - -本仓库中的 **ChromeboxFanControl** 和 **FanControl.ChromeboxEC** 插件主要使用以下命令: - -| 功能 | 命令 | 说明 | -|------|------|------| -| 设定风扇占空比 | `ectool fanduty <0-100>` | 手动控速 | -| 恢复自动控速 | `ectool autofanctrl` | 退出时恢复 | -| 读取转速 | `ectool pwmgetfanrpm 0` | 显示 RPM | -| 读取温度 | `ectool temps` | 从 EC 读取温度 | - -不同主板的 `pwmgetfanrpm`、`temps` 参数可能不同,请用 `ectool help` 和 `ectool tempsinfo` 确认本机用法。 - ---- - -## 完整命令列表(英文字母序) - -| 命令 | 说明 | -|------|------| -| adcread \ | 读取 ADC 通道 | -| addentropy [reset] | 向设备秘密添加熵 | -| apreset | 发起 AP 复位 | -| autofanctrl \ | 开启自动风扇控制 | -| backlight \ | 启用/禁用 LCD 背光 | -| basestate [attach\|detach\|reset] | 强制底座状态 | -| battery | 电池信息 | -| batterycutoff [at-shutdown] | 切断电池输出 | -| batteryparam | 读写板级电池参数 | -| boardversion | 板级版本 | -| button [vup\|vdown\|rec] \ | 模拟按键 | -| cbi | 读写 Cros Board Info | -| chargecurrentlimit | 设置最大充电电流 | -| chargecontrol | 强制停止充电或放电 | -| chargeoverride | 覆盖充电口选择逻辑 | -| chargesplash | 充电动画相关 | -| chargestate | 充电状态 v2+ | -| chipinfo | 芯片信息 | -| cmdversions \ | 命令版本掩码 | -| console | EC 调试控制台输出 | -| cec | CEC 消息读写 | -| echash [CMDS] | EC hash 相关 | -| eventclear \ | 清除 EC 主机事件 | -| eventclearb \ | 清除 EC 主机事件副本 B | -| eventget | 原始 EC 主机事件标志 | -| eventgetb | 原始 EC 主机事件标志副本 B | -| eventgetscimask | SCI 掩码 | -| eventgetsmimask | SMI 掩码 | -| eventgetwakemask | 唤醒掩码 | -| eventsetscimask \ | 设置 SCI 掩码 | -| eventsetsmimask \ | 设置 SMI 掩码 | -| eventsetwakemask \ | 设置唤醒掩码 | -| extpwrlimit | 外部功率限制 | -| fanduty \ | 固定风扇 PWM 占空比 | -| flasherase \ \ | 擦除 EC Flash | -| flasheraseasync \ \ | 异步擦除 EC Flash | -| flashinfo | EC Flash 信息 | -| flashspiinfo | EC SPI Flash 信息 | -| flashpd \ \ \ | 通过 PD 刷写 | -| flashprotect [now] [enable\|disable] | Flash 写保护 | -| flashread \ \ \ | 从 EC Flash 读取 | -| flashwrite \ \ | 写入 EC Flash | -| forcelidopen \ | 强制盖板为打开 | -| fpcontext | 指纹传感器上下文 | -| fpencstatus | 指纹加密引擎状态 | -| fpframe | 获取指纹图像 | -| fpinfo | 指纹传感器信息 | -| fpmode [mode...] | 指纹传感器模式 | -| fpseed | 设置 TPM seed | -| fpstats | 指纹匹配时序统计 | -| fptemplate [\\|\] | 添加/导出指纹模板 | -| gpioget \ | 读取 GPIO | -| gpioset \ | 设置 GPIO | -| hangdetect | 挂起检测定时器 | -| hello | 检测 EC 通信 | -| hibdelay [sec] | 休眠前延时 | -| hostsleepstate | 主机睡眠状态 | -| hostevent | 主机事件掩码 | -| i2cprotect \ | I2C 总线保护 | -| i2cread | I2C 读取 | -| i2cspeed \ [speed] | I2C 总线速率 | -| i2cwrite | I2C 写入 | -| i2cxfer \ \ \ [write bytes...] | I2C 传输 | -| infopddev \ | USB-C 配件信息 | -| inventory | 支持功能列表 | -| kbfactorytest | 键盘工厂测试 | -| kbid | 键盘 ID | -| kbinfo | 键盘矩阵信息 | -| kbpress | 模拟按键 | -| keyscan \ \ | 按键扫描测试 | -| led \ \ | LED 控制 | -| lightbar [CMDS] | 灯条控制 | -| locatechip \ \ | 查找芯片地址 | -| mkbpget \ | MKBP 按键/开关 | -| mkbpwakemask | MKBP 唤醒掩码 | -| motionsense [CMDS] | 运动传感器 | -| panicinfo | 崩溃信息 | -| pause_in_s5 [on\|off] | S5 关机时是否暂停 | -| pchg [\] | 外设充电口 | -| pdcontrol [suspend\|resume\|reset\|disable\|on] | PD 芯片控制 | -| pdchipinfo \ | PD 芯片信息 | -| pdlog | PD 事件日志 | -| pdwritelog \ \ | 写入 PD 日志 | -| pdgetmode \ | 获取 USB-PD 模式 | -| pdsetmode \ \ \ | 设置 USB-PD 模式 | -| port80flood | 快速写 port 80 | -| port80read | port 80 历史 | -| powerinfo | 电源信息 | -| protoinfo | EC 主机协议信息 | -| pse | PoE PSE 端口功率 | -| pstoreinfo | 持久存储信息 | -| pstoreread \ \ \ | 读取持久存储 | -| pstorewrite \ \ | 写入持久存储 | -| pwmgetfanrpm [\\|all] | 风扇转速 | -| pwmgetkblight | 键盘背光百分比 | -| pwmgetnumfans | 风扇数量 | -| pwmgetduty | 当前 PWM 占空比 | -| pwmsetfanrpm \ | 设定目标风扇转速 | -| pwmsetkblight \ | 键盘背光百分比 | -| pwmsetduty | 设置 PWM 占空比 | -| rand \ | 生成随机数 | -| readtest | EC 读取测试 | -| reboot_ec \ | 重启 EC | -| reboot_ap_on_g3 [\] | G3 后自动重启 AP | -| rgbkbd ... | RGB 键盘 | -| rollbackinfo | 回滚块信息 | -| rtcget | 读取 RTC | -| rtcgetalarm | RTC 闹钟剩余秒数 | -| rtcset \ | 设置 RTC | -| rtcsetalarm \ | 设置 RTC 闹钟 | -| rwhashpd | PD MCU rw_hash | -| rwsig \ | RW 签名相关 | -| sertest | 串口输出测试 | -| smartdischarge | 智能放电参数 | -| stress [reboot] [help] | 压力测试 | -| sysinfo [flags\|reset_flags\|firmware_copy] | 系统信息 | -| switches | EC 开关状态 | -| temps \ | 温度传感器读数 | -| tempsinfo \ | 温度传感器信息 | -| thermalget | 读取热阈温 | -| thermalset | 设置热阈温 | -| tpselftest | 触摸板自检 | -| tpframeget | 触摸板帧数据 | -| tmp006cal | TMP006 校准 | -| tmp006raw | TMP006 原始数据 | -| typeccontrol \ \ | USB PD 策略 | -| typecdiscovery \ \ | USB-C 发现信息 | -| typecstatus \ | USB-C 状态 | -| uptimeinfo | EC 运行时长与 AP 复位 | -| usbchargemode \ \ | USB 充电模式 | -| usbmux \ | USB Mux 状态 | -| usbpd | USB PD 控制(已弃用) | -| usbpddps [enable\|disable] | 动态 PDO 选择 | -| usbpdmuxinfo [tsv] | USB-C SS Mux 信息 | -| usbpdpower [port] | USB PD 功率 | -| version | EC 版本 | -| waitevent \ [\] | 等待 MKBP 事件 | -| wireless \ | WLAN/蓝牙无线控制 | - ---- - -## 常用命令速查(实测 Coolstar ectool) - -| 操作 | 命令 | -|------|------| -| 读取温度 | `ectool temps 0` | -| 读取风扇转速 (RPM) | `ectool pwmgetfanrpm 0` 或 `ectool pwmgetfanrpm all` | -| 读取占空比 (16 位) | `ectool pwmgetduty 0`,百分比 = 数值/65535×100 | -| 设置风扇占空比 | `ectool fanduty 75`(所有风扇)或 `ectool fanduty 0 50`(风扇 0) | -| 设置目标 RPM | `ectool pwmsetfanrpm 2000` | -| 恢复 EC 自动控速 | `ectool autofanctrl` | - ---- - -## 参考 - -- [Chrultrabook ectool 安装说明](https://docs.chrultrabook.com/docs/installing/ectool.html) -- [Coolstar CROS-EC 驱动](https://github.com/coolstar/crwindows) +# ectool 命令参考(中文) + +`ectool` 是 Chrome OS 嵌入式控制器(EC)的命令行工具,用于从用户空间与 EC 通信。在刷了 coreboot 的 Chromebox/Chromebook 上,可通过 Coolstar CROS-EC 驱动的 `ectool.exe` 使用。 + +## 基本用法 + +```text +ectool [选项] <命令> [参数] +``` + +### 常用选项 + +| 选项 | 说明 | +|------|------| +| `--dev=n` | 指定设备号 | +| `--interface=dev\|i2c\|lpc` | 指定接口类型 | +| `--i2c_bus=n` | 指定 I2C 总线号(如 `--i2c_bus=7` 使用 /dev/i2c-7),隐含 `--interface=i2c` | +| `--device=vid:pid` | 指定 USB 端点(如 `18d1:5022`) | +| `--name=cros_ec\|cros_fp\|cros_pd\|cros_scp\|cros_ish` | 指定 EC 类型 | +| `--ascii` | 以 ASCII 格式输出 | + +--- + +## 风扇相关命令 + +### fanduty [idx] \ + +强制将风扇 PWM 设为固定占空比(0–100)。 + +```text +ectool fanduty 75 # 所有风扇 75% +ectool fanduty 0 50 # 风扇 0 设为 50% +``` + +### autofanctrl \ + +开启 EC 自动风扇转速控制。取消手动 `fanduty` 后,应调用此命令恢复 EC 自动控速。 + +```text +ectool autofanctrl +``` + +### pwmgetfanrpm [\ | all] + +读取风扇转速(RPM)。 + +```text +ectool pwmgetfanrpm 0 # 读取第 0 号风扇,输出如 "Fan 0 RPM: 2621" +ectool pwmgetfanrpm all # 读取所有风扇 +``` + +### pwmgetnumfans + +显示风扇数量。 + +### pwmsetfanrpm \ + +设定目标风扇转速(RPM)。部分 EC 支持基于目标转速的控制。 + +### pwmgetduty \ | kb | disp + +读取当前 PWM 占空比(16 位,0–65535)。 + +```text +ectool pwmgetduty 0 # 风扇 0,输出如 "Current PWM duty: 50462"(约 77%) +``` +百分比 = 数值 / 65535 × 100 + +### pwmsetduty + +设置 PWM 占空比(16 位)。需配合具体参数使用。 + +--- + +## 温度相关命令 + +### temps \ + +读取指定温度传感器的温度,以及与 `fan_off` / `fan_max` 的比值。 + +```text +ectool temps 0 # 输出如 "Core 336 K (= 63 C) 100% (313 K and 333 K)" +``` + +输出为开尔文(K)及摄氏(C)。传感器 ID 因主板而异,用 `tempsinfo` 查看。 + +### tempsinfo \ + +显示温度传感器信息,用于确认本机支持的传感器 ID 和名称。 + +```text +ectool tempsinfo 0 +``` + +--- + +## 电源与电池 + +### battery + +显示电池信息。 + +### powerinfo + +显示电源相关信息。 + +--- + +## 系统与固件 + +### chipinfo + +显示 EC 芯片信息。 + +### version + +显示 EC 固件版本。 + +### hello + +检测与 EC 的基本通信是否正常。 + +### console + +显示 EC 调试控制台最新输出。 + +### sysinfo [flags\|reset_flags\|firmware_copy] + +显示系统信息。 + +--- + +## 其他常用命令 + +### switches + +显示 EC 开关状态(如盖板、电源等)。 + +### boardversion + +显示板级版本。 + +### flashinfo + +显示 EC Flash 信息。 + +### rtcget / rtcset + +读取/设置 EC 内部 RTC。 + +### led \ \ + +控制 LED 颜色或查询亮度范围。 + +--- + +## 与 Chromebox 风扇温控的对应关系 + +本仓库中的 **ChromeboxFanControl** 和 **FanControl.ChromeboxEC** 插件主要使用以下命令: + +| 功能 | 命令 | 说明 | +|------|------|------| +| 设定风扇占空比 | `ectool fanduty <0-100>` | 手动控速 | +| 恢复自动控速 | `ectool autofanctrl` | 退出时恢复 | +| 读取转速 | `ectool pwmgetfanrpm 0` | 显示 RPM | +| 读取温度 | `ectool temps` | 从 EC 读取温度 | + +不同主板的 `pwmgetfanrpm`、`temps` 参数可能不同,请用 `ectool help` 和 `ectool tempsinfo` 确认本机用法。 + +--- + +## 完整命令列表(英文字母序) + +| 命令 | 说明 | +|------|------| +| adcread \ | 读取 ADC 通道 | +| addentropy [reset] | 向设备秘密添加熵 | +| apreset | 发起 AP 复位 | +| autofanctrl \ | 开启自动风扇控制 | +| backlight \ | 启用/禁用 LCD 背光 | +| basestate [attach\|detach\|reset] | 强制底座状态 | +| battery | 电池信息 | +| batterycutoff [at-shutdown] | 切断电池输出 | +| batteryparam | 读写板级电池参数 | +| boardversion | 板级版本 | +| button [vup\|vdown\|rec] \ | 模拟按键 | +| cbi | 读写 Cros Board Info | +| chargecurrentlimit | 设置最大充电电流 | +| chargecontrol | 强制停止充电或放电 | +| chargeoverride | 覆盖充电口选择逻辑 | +| chargesplash | 充电动画相关 | +| chargestate | 充电状态 v2+ | +| chipinfo | 芯片信息 | +| cmdversions \ | 命令版本掩码 | +| console | EC 调试控制台输出 | +| cec | CEC 消息读写 | +| echash [CMDS] | EC hash 相关 | +| eventclear \ | 清除 EC 主机事件 | +| eventclearb \ | 清除 EC 主机事件副本 B | +| eventget | 原始 EC 主机事件标志 | +| eventgetb | 原始 EC 主机事件标志副本 B | +| eventgetscimask | SCI 掩码 | +| eventgetsmimask | SMI 掩码 | +| eventgetwakemask | 唤醒掩码 | +| eventsetscimask \ | 设置 SCI 掩码 | +| eventsetsmimask \ | 设置 SMI 掩码 | +| eventsetwakemask \ | 设置唤醒掩码 | +| extpwrlimit | 外部功率限制 | +| fanduty \ | 固定风扇 PWM 占空比 | +| flasherase \ \ | 擦除 EC Flash | +| flasheraseasync \ \ | 异步擦除 EC Flash | +| flashinfo | EC Flash 信息 | +| flashspiinfo | EC SPI Flash 信息 | +| flashpd \ \ \ | 通过 PD 刷写 | +| flashprotect [now] [enable\|disable] | Flash 写保护 | +| flashread \ \ \ | 从 EC Flash 读取 | +| flashwrite \ \ | 写入 EC Flash | +| forcelidopen \ | 强制盖板为打开 | +| fpcontext | 指纹传感器上下文 | +| fpencstatus | 指纹加密引擎状态 | +| fpframe | 获取指纹图像 | +| fpinfo | 指纹传感器信息 | +| fpmode [mode...] | 指纹传感器模式 | +| fpseed | 设置 TPM seed | +| fpstats | 指纹匹配时序统计 | +| fptemplate [\\|\] | 添加/导出指纹模板 | +| gpioget \ | 读取 GPIO | +| gpioset \ | 设置 GPIO | +| hangdetect | 挂起检测定时器 | +| hello | 检测 EC 通信 | +| hibdelay [sec] | 休眠前延时 | +| hostsleepstate | 主机睡眠状态 | +| hostevent | 主机事件掩码 | +| i2cprotect \ | I2C 总线保护 | +| i2cread | I2C 读取 | +| i2cspeed \ [speed] | I2C 总线速率 | +| i2cwrite | I2C 写入 | +| i2cxfer \ \ \ [write bytes...] | I2C 传输 | +| infopddev \ | USB-C 配件信息 | +| inventory | 支持功能列表 | +| kbfactorytest | 键盘工厂测试 | +| kbid | 键盘 ID | +| kbinfo | 键盘矩阵信息 | +| kbpress | 模拟按键 | +| keyscan \ \ | 按键扫描测试 | +| led \ \ | LED 控制 | +| lightbar [CMDS] | 灯条控制 | +| locatechip \ \ | 查找芯片地址 | +| mkbpget \ | MKBP 按键/开关 | +| mkbpwakemask | MKBP 唤醒掩码 | +| motionsense [CMDS] | 运动传感器 | +| panicinfo | 崩溃信息 | +| pause_in_s5 [on\|off] | S5 关机时是否暂停 | +| pchg [\] | 外设充电口 | +| pdcontrol [suspend\|resume\|reset\|disable\|on] | PD 芯片控制 | +| pdchipinfo \ | PD 芯片信息 | +| pdlog | PD 事件日志 | +| pdwritelog \ \ | 写入 PD 日志 | +| pdgetmode \ | 获取 USB-PD 模式 | +| pdsetmode \ \ \ | 设置 USB-PD 模式 | +| port80flood | 快速写 port 80 | +| port80read | port 80 历史 | +| powerinfo | 电源信息 | +| protoinfo | EC 主机协议信息 | +| pse | PoE PSE 端口功率 | +| pstoreinfo | 持久存储信息 | +| pstoreread \ \ \ | 读取持久存储 | +| pstorewrite \ \ | 写入持久存储 | +| pwmgetfanrpm [\\|all] | 风扇转速 | +| pwmgetkblight | 键盘背光百分比 | +| pwmgetnumfans | 风扇数量 | +| pwmgetduty | 当前 PWM 占空比 | +| pwmsetfanrpm \ | 设定目标风扇转速 | +| pwmsetkblight \ | 键盘背光百分比 | +| pwmsetduty | 设置 PWM 占空比 | +| rand \ | 生成随机数 | +| readtest | EC 读取测试 | +| reboot_ec \ | 重启 EC | +| reboot_ap_on_g3 [\] | G3 后自动重启 AP | +| rgbkbd ... | RGB 键盘 | +| rollbackinfo | 回滚块信息 | +| rtcget | 读取 RTC | +| rtcgetalarm | RTC 闹钟剩余秒数 | +| rtcset \ | 设置 RTC | +| rtcsetalarm \ | 设置 RTC 闹钟 | +| rwhashpd | PD MCU rw_hash | +| rwsig \ | RW 签名相关 | +| sertest | 串口输出测试 | +| smartdischarge | 智能放电参数 | +| stress [reboot] [help] | 压力测试 | +| sysinfo [flags\|reset_flags\|firmware_copy] | 系统信息 | +| switches | EC 开关状态 | +| temps \ | 温度传感器读数 | +| tempsinfo \ | 温度传感器信息 | +| thermalget | 读取热阈温 | +| thermalset | 设置热阈温 | +| tpselftest | 触摸板自检 | +| tpframeget | 触摸板帧数据 | +| tmp006cal | TMP006 校准 | +| tmp006raw | TMP006 原始数据 | +| typeccontrol \ \ | USB PD 策略 | +| typecdiscovery \ \ | USB-C 发现信息 | +| typecstatus \ | USB-C 状态 | +| uptimeinfo | EC 运行时长与 AP 复位 | +| usbchargemode \ \ | USB 充电模式 | +| usbmux \ | USB Mux 状态 | +| usbpd | USB PD 控制(已弃用) | +| usbpddps [enable\|disable] | 动态 PDO 选择 | +| usbpdmuxinfo [tsv] | USB-C SS Mux 信息 | +| usbpdpower [port] | USB PD 功率 | +| version | EC 版本 | +| waitevent \ [\] | 等待 MKBP 事件 | +| wireless \ | WLAN/蓝牙无线控制 | + +--- + +## 常用命令速查(实测 Coolstar ectool) + +| 操作 | 命令 | +|------|------| +| 读取温度 | `ectool temps 0` | +| 读取风扇转速 (RPM) | `ectool pwmgetfanrpm 0` 或 `ectool pwmgetfanrpm all` | +| 读取占空比 (16 位) | `ectool pwmgetduty 0`,百分比 = 数值/65535×100 | +| 设置风扇占空比 | `ectool fanduty 75`(所有风扇)或 `ectool fanduty 0 50`(风扇 0) | +| 设置目标 RPM | `ectool pwmsetfanrpm 2000` | +| 恢复 EC 自动控速 | `ectool autofanctrl` | + +--- + +## 参考 + +- [Chrultrabook ectool 安装说明](https://docs.chrultrabook.com/docs/installing/ectool.html) +- [Coolstar CROS-EC 驱动](https://github.com/coolstar/crwindows) diff --git a/wix/Product-Service.wxs b/wix/Product-Service.wxs index 7ce7bf4..0b4c940 100644 --- a/wix/Product-Service.wxs +++ b/wix/Product-Service.wxs @@ -1,48 +1,48 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/wix/Product.wxs b/wix/Product.wxs index cd727da..ef4c212 100644 --- a/wix/Product.wxs +++ b/wix/Product.wxs @@ -1,66 +1,66 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/wix/exclude-service.xsl b/wix/exclude-service.xsl index 3b71a9f..36b24b9 100644 --- a/wix/exclude-service.xsl +++ b/wix/exclude-service.xsl @@ -1,11 +1,11 @@ - - - - - - - - - - - + + + + + + + + + + +