diff --git a/AppConfig.cs b/AppConfig.cs new file mode 100644 index 00000000..db27733c --- /dev/null +++ b/AppConfig.cs @@ -0,0 +1,144 @@ +using System.Text.Json; + +public class AppConfig +{ + + string appPath; + string configFile; + + public Dictionary config = new Dictionary(); + + public AppConfig() + { + + appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GHelper"; + configFile = appPath + "\\config.json"; + + if (!System.IO.Directory.Exists(appPath)) + System.IO.Directory.CreateDirectory(appPath); + + if (File.Exists(configFile)) + { + string text = File.ReadAllText(configFile); + try + { + config = JsonSerializer.Deserialize>(text); + } + catch + { + initConfig(); + } + } + else + { + initConfig(); + } + + } + + private void initConfig() + { + config = new Dictionary(); + config["performance_mode"] = 0; + string jsonString = JsonSerializer.Serialize(config); + File.WriteAllText(configFile, jsonString); + } + + public int getConfig(string name) + { + if (config.ContainsKey(name)) + return int.Parse(config[name].ToString()); + else return -1; + } + + public string getConfigString(string name) + { + if (config.ContainsKey(name)) + return config[name].ToString(); + else return null; + } + + public void setConfig(string name, int value) + { + config[name] = value; + string jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(configFile, jsonString); + } + + public void setConfig(string name, string value) + { + config[name] = value; + string jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(configFile, jsonString); + } + + public string getParamName(int device, string paramName = "fan_profile") + { + int mode = getConfig("performance_mode"); + string name; + + if (device == 1) + name = "gpu"; + else + name = "cpu"; + + return paramName+"_" + name + "_" + mode; + } + + public byte[] getFanConfig(int device) + { + string curveString = getConfigString(getParamName(device)); + byte[] curve = { }; + + if (curveString is not null) + curve = StringToBytes(curveString); + + return curve; + } + + public void setFanConfig(int device, byte[] curve) + { + string bitCurve = BitConverter.ToString(curve); + setConfig(getParamName(device), bitCurve); + } + + + public static byte[] StringToBytes(string str) + { + String[] arr = str.Split('-'); + byte[] array = new byte[arr.Length]; + for (int i = 0; i < arr.Length; i++) array[i] = Convert.ToByte(arr[i], 16); + return array; + } + + public byte[] getDefaultCurve(int device) + { + int mode = getConfig("performance_mode"); + byte[] curve; + + switch (mode) + { + case 1: + if (device == 1) + curve = StringToBytes("14-3F-44-48-4C-50-54-62-16-1F-26-2D-39-47-55-5F"); + else + curve = StringToBytes("14-3F-44-48-4C-50-54-62-11-1A-22-29-34-43-51-5A"); + break; + case 2: + if (device == 1) + curve = StringToBytes("3C-41-42-46-47-4B-4C-62-08-11-11-1D-1D-26-26-2D"); + else + curve = StringToBytes("3C-41-42-46-47-4B-4C-62-03-0C-0C-16-16-22-22-29"); + break; + default: + if (device == 1) + curve = StringToBytes("3A-3D-40-44-48-4D-51-62-0C-16-1D-1F-26-2D-34-4A"); + else + curve = StringToBytes("3A-3D-40-44-48-4D-51-62-08-11-16-1A-22-29-30-45"); + break; + } + + return curve; + } + +} diff --git a/GHelper.csproj b/GHelper.csproj index 2e857890..0f3ed78a 100644 --- a/GHelper.csproj +++ b/GHelper.csproj @@ -15,7 +15,7 @@ GHelper x64 False - 0.11.0 + 0.12.0 diff --git a/Program.cs b/Program.cs index adf68f9a..e4665f5a 100644 --- a/Program.cs +++ b/Program.cs @@ -1,150 +1,6 @@ using Microsoft.Win32; using System.Diagnostics; using System.Management; -using System.Text.Json; - -public class AppConfig -{ - - string appPath; - string configFile; - - public Dictionary config = new Dictionary(); - - public AppConfig() - { - - appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GHelper"; - configFile = appPath + "\\config.json"; - - if (!System.IO.Directory.Exists(appPath)) - System.IO.Directory.CreateDirectory(appPath); - - if (File.Exists(configFile)) - { - string text = File.ReadAllText(configFile); - try - { - config = JsonSerializer.Deserialize>(text); - } - catch - { - initConfig(); - } - } - else - { - initConfig(); - } - - } - - private void initConfig() - { - config = new Dictionary(); - config["performance_mode"] = 0; - string jsonString = JsonSerializer.Serialize(config); - File.WriteAllText(configFile, jsonString); - } - - public int getConfig(string name) - { - if (config.ContainsKey(name)) - return int.Parse(config[name].ToString()); - else return -1; - } - - public string getConfigString(string name) - { - if (config.ContainsKey(name)) - return config[name].ToString(); - else return null; - } - - public void setConfig(string name, int value) - { - config[name] = value; - string jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); - File.WriteAllText(configFile, jsonString); - } - - public void setConfig(string name, string value) - { - config[name] = value; - string jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); - File.WriteAllText(configFile, jsonString); - } - - public string getFanName(int device) - { - int mode = getConfig("performance_mode"); - string name; - - if (device == 1) - name = "gpu"; - else - name = "cpu"; - - return "fan_profile_" + name + "_" + mode; - } - - public byte[] getFanConfig(int device) - { - string curveString = getConfigString(getFanName(device)); - byte[] curve = { }; - - if (curveString is not null) - curve = StringToBytes(curveString); - - return curve; - } - - public void setFanConfig(int device, byte[] curve) - { - string bitCurve = BitConverter.ToString(curve); - setConfig(getFanName(device), bitCurve); - } - - - public static byte[] StringToBytes(string str) - { - String[] arr = str.Split('-'); - byte[] array = new byte[arr.Length]; - for (int i = 0; i < arr.Length; i++) array[i] = Convert.ToByte(arr[i], 16); - return array; - } - - public byte[] getDefaultCurve(int device) - { - int mode = getConfig("performance_mode"); - byte[] curve; - - switch (mode) - { - case 1: - if (device == 1) - curve = StringToBytes("14-3F-44-48-4C-50-54-62-16-1F-26-2D-39-47-55-5F"); - else - curve = StringToBytes("14-3F-44-48-4C-50-54-62-11-1A-22-29-34-43-51-5A"); - break; - case 2: - if (device == 1) - curve = StringToBytes("3C-41-42-46-47-4B-4C-62-08-11-11-1D-1D-26-26-2D"); - else - curve = StringToBytes("3C-41-42-46-47-4B-4C-62-03-0C-0C-16-16-22-22-29"); - break; - default: - if (device == 1) - curve = StringToBytes("3A-3D-40-44-48-4D-51-62-0C-16-1D-1F-26-2D-34-4A"); - else - curve = StringToBytes("3A-3D-40-44-48-4D-51-62-08-11-16-1A-22-29-30-45"); - break; - } - - return curve; - } - -} public class HardwareMonitor { diff --git a/Settings.Designer.cs b/Settings.Designer.cs index d2191369..b3ae5f6b 100644 --- a/Settings.Designer.cs +++ b/Settings.Designer.cs @@ -132,12 +132,12 @@ // labelGPUFan // labelGPUFan.Anchor = AnchorStyles.Top | AnchorStyles.Right; - labelGPUFan.Location = new Point(410, 262); + labelGPUFan.Location = new Point(338, 262); labelGPUFan.Margin = new Padding(4, 0, 4, 0); labelGPUFan.Name = "labelGPUFan"; - labelGPUFan.Size = new Size(276, 32); + labelGPUFan.Size = new Size(348, 32); labelGPUFan.TabIndex = 8; - labelGPUFan.Text = "GPU Fan : 0%"; + labelGPUFan.Text = "GPU Fan"; labelGPUFan.TextAlign = ContentAlignment.TopRight; // // tableGPU @@ -226,12 +226,12 @@ // labelCPUFan // labelCPUFan.Anchor = AnchorStyles.Top | AnchorStyles.Right; - labelCPUFan.Location = new Point(410, 38); + labelCPUFan.Location = new Point(320, 38); labelCPUFan.Margin = new Padding(4, 0, 4, 0); labelCPUFan.Name = "labelCPUFan"; - labelCPUFan.Size = new Size(276, 32); + labelCPUFan.Size = new Size(366, 32); labelCPUFan.TabIndex = 12; - labelCPUFan.Text = "CPU Fan : 0%"; + labelCPUFan.Text = "CPU Fan"; labelCPUFan.TextAlign = ContentAlignment.TopRight; // // tablePerf diff --git a/Settings.cs b/Settings.cs index 69708ef9..08c06e94 100644 --- a/Settings.cs +++ b/Settings.cs @@ -75,12 +75,21 @@ namespace GHelper labelVersion.Text = "Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString(); labelVersion.Click += LabelVersion_Click; + labelCPUFan.Click += LabelCPUFan_Click; + labelGPUFan.Click += LabelCPUFan_Click; + SetTimer(); } + private void LabelCPUFan_Click(object? sender, EventArgs e) + { + Program.config.setConfig("fan_rpm", (Program.config.getConfig("fan_rpm") == 1) ? 0 : 1); + RefreshSensors(); + } + private void LabelVersion_Click(object? sender, EventArgs e) { Process.Start(new ProcessStartInfo("http://github.com/seerge/g-helper/releases") { UseShellExecute = true }); @@ -128,7 +137,8 @@ namespace GHelper if (fans.Visible) { fans.Hide(); - } else + } + else { fans.Show(); } @@ -402,10 +412,20 @@ namespace GHelper aTimer.Enabled = false; } + + private static string FormatFan(int fan) + { + if (Program.config.getConfig("fan_rpm") == 1) + return " Fan: " + (fan * 100).ToString() + "RPM"; + else + return " Fan: " + Math.Round(fan / 0.6).ToString() + "%"; // relatively to 6000 rpm + } + private static void RefreshSensors() { - string cpuFan = " Fan: " + Math.Round(Program.wmi.DeviceGet(ASUSWmi.CPU_Fan) / 0.6).ToString() + "%"; - string gpuFan = " Fan: " + Math.Round(Program.wmi.DeviceGet(ASUSWmi.GPU_Fan) / 0.6).ToString() + "%"; + + string cpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.CPU_Fan)); + string gpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.GPU_Fan)); string cpuTemp = ""; string gpuTemp = ""; @@ -443,7 +463,7 @@ namespace GHelper this.Top = Screen.FromControl(this).WorkingArea.Height - 10 - this.Height; this.Activate(); - aTimer.Interval = 500; + aTimer.Interval = 100; aTimer.Enabled = true; }