diff --git a/app/ASUSWmi.cs b/app/ASUSWmi.cs index ddb6e883..9dae0361 100644 --- a/app/ASUSWmi.cs +++ b/app/ASUSWmi.cs @@ -1,4 +1,5 @@ -using GHelper; +using System.Globalization; +using System.IO.Pipes; using System.Management; using System.Runtime.InteropServices; @@ -205,7 +206,7 @@ public class ASUSWmi byte[] args = new byte[8]; BitConverter.GetBytes((uint)DeviceID).CopyTo(args, 0); byte[] status = CallMethod(DSTS, args); - + return BitConverter.ToInt32(status, 0) - 65536; } @@ -215,7 +216,7 @@ public class ASUSWmi byte[] args = new byte[8]; BitConverter.GetBytes((uint)DeviceID).CopyTo(args, 0); BitConverter.GetBytes((uint)Status).CopyTo(args, 4); - + return CallMethod(DSTS, args); } @@ -242,8 +243,8 @@ public class ASUSWmi int result; - //for (int i = 8; i < curve.Length; i++) - // curve[i] = Math.Max((byte)0, Math.Min((byte)99, curve[i])); // it seems to be a bug, when some old model's bios can go nuts if fan is set to 100% + for (int i = 8; i < curve.Length; i++) + curve[i] = Math.Max((byte)0, Math.Min((byte)99, curve[i])); // it seems to be a bug, when some old model's bios can go nuts if fan is set to 100% switch (device) { @@ -285,6 +286,47 @@ public class ASUSWmi } + + public static bool IsEmptyCurve(byte[] curve) { + return curve.Length != 16 || curve.All(singleByte => singleByte == 0); + } + + public static byte[] FixFanCurve(byte[] curve) + { + if (curve.Length != 16) throw new Exception("Incorrect curve"); + + var points = new Dictionary(); + for (int i = 0; i < 8; i++) points[curve[i]] = curve[i+8]; + + var pointsFixed = new Dictionary(); + bool fix = false; + + int count = 0; + foreach (var pair in points.OrderBy(x => x.Key)) + { + if (count == 0 && pair.Key >= 40) + { + fix = true; + pointsFixed.Add(20, 0); + } + + if (count != 3 || !fix) + pointsFixed.Add(pair.Key, pair.Value); + count++; + } + + count = 0; + foreach (var pair in pointsFixed.OrderBy(x => x.Key)) + { + curve[count] =pair.Key; + curve[count+8] = pair.Value; + count++; + } + + return curve; + + } + public void TUFKeyboardBrightness(int brightness) { int param = 0x80 | (brightness & 0x7F); @@ -334,7 +376,8 @@ public class ASUSWmi watcher.Scope = new ManagementScope("root\\wmi"); watcher.Query = new WqlEventQuery("SELECT * FROM AsusAtkWmiEvent"); watcher.Start(); - } catch + } + catch { Logger.WriteLine("Can't connect to ASUS WMI events"); } diff --git a/app/Aura.cs b/app/Aura.cs index 9a617c78..3739c5f3 100644 --- a/app/Aura.cs +++ b/app/Aura.cs @@ -1,8 +1,6 @@ using HidLibrary; using Microsoft.Win32; -using OSD; using System.Diagnostics; -using System.Windows.Forms; namespace GHelper { @@ -167,7 +165,9 @@ namespace GHelper { HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); foreach (HidDevice device in HidDeviceList) - if (device.IsConnected && device.Description.ToLower().Contains("hid") && device.Capabilities.FeatureReportByteLength >= 64) + if (device.IsConnected + && device.Capabilities.FeatureReportByteLength >= 64 + && device.Capabilities.InputReportByteLength >= 12) // yield return device; } @@ -196,12 +196,13 @@ namespace GHelper byte[] msg = { 0x5d, 0xba, 0xc5, 0xc4, (byte)brightness }; var devices = GetHidDevices(deviceIds); - if (devices.Count() > 0) Logger.WriteLine("USB-KB = " + BitConverter.ToString(msg)); + //Logger.WriteLine("USB-KB = " + BitConverter.ToString(msg)); foreach (HidDevice device in devices) { device.OpenDevice(); device.Write(msg); + Logger.WriteLine("USB-KB " + device.Attributes.ProductHexId + ":" + BitConverter.ToString(msg)); device.CloseDevice(); } @@ -217,27 +218,28 @@ namespace GHelper var devices = GetHidDevices(deviceIds); - if (devices.Count() > 0) Logger.WriteLine("USB-KB = " + BitConverter.ToString(msg)); + //Logger.WriteLine("USB-KB = " + BitConverter.ToString(msg)); foreach (HidDevice device in devices) { device.OpenDevice(); device.Write(msg); + Logger.WriteLine("USB-KB " + device.Attributes.ProductHexId + ":" + BitConverter.ToString(msg)); device.CloseDevice(); } if (Program.config.ContainsModel("TUF")) Program.wmi.TUFKeyboardPower( - flags.Contains(AuraDev19b6.AwakeKeyb), - flags.Contains(AuraDev19b6.BootKeyb), - flags.Contains(AuraDev19b6.SleepKeyb), + flags.Contains(AuraDev19b6.AwakeKeyb), + flags.Contains(AuraDev19b6.BootKeyb), + flags.Contains(AuraDev19b6.SleepKeyb), flags.Contains(AuraDev19b6.ShutdownKeyb)); } public static void ApplyXGMLight(bool status) { - byte value = status? (byte)0x50:(byte)0; + byte value = status ? (byte)0x50 : (byte)0; var msg = new byte[] { 0x5e, 0xc5, value }; foreach (HidDevice device in GetHidDevices(new int[] { 0x1970 })) @@ -273,7 +275,7 @@ namespace GHelper byte[] msg = AuraMessage(Mode, Color1, Color2, _speed); var devices = GetHidDevices(deviceIds); - if (devices.Count() > 0) Logger.WriteLine("USB-KB = " + BitConverter.ToString(msg)); + //if (devices.Count() > 0) Logger.WriteLine("USB-KB = " + BitConverter.ToString(msg)); foreach (HidDevice device in devices) { @@ -282,6 +284,7 @@ namespace GHelper device.Write(MESSAGE_SET); device.Write(MESSAGE_APPLY); device.CloseDevice(); + Logger.WriteLine("USB-KB " + device.Capabilities.FeatureReportByteLength + "|" + device.Capabilities.InputReportByteLength + device.Description + device.DevicePath + ":" + BitConverter.ToString(msg)); } if (Program.config.ContainsModel("TUF")) @@ -299,7 +302,8 @@ namespace GHelper myKey.SetValue("TurnOffKeybdLight", value, RegistryValueKind.DWord); myKey.Close(); } - } catch (Exception ex) + } + catch (Exception ex) { Logger.WriteLine(ex.Message); } diff --git a/app/Fans.cs b/app/Fans.cs index dc55fefc..b6cf81e7 100644 --- a/app/Fans.cs +++ b/app/Fans.cs @@ -462,11 +462,16 @@ namespace GHelper int mode = Program.config.getConfig("performance_mode"); byte[] curve = Program.config.getFanConfig(device); - if (def == 1 || curve.Length != 16) + if (def == 1 || ASUSWmi.IsEmptyCurve(curve)) + { curve = Program.wmi.GetFanCurve(device, mode); + + if (ASUSWmi.IsEmptyCurve(curve)) + curve = Program.config.getDefaultCurve(device); - if (curve.Length != 16 || curve.All(singleByte => singleByte == 0)) - curve = Program.config.getDefaultCurve(device); + curve = ASUSWmi.FixFanCurve(curve); + + } //Debug.WriteLine(BitConverter.ToString(curve)); diff --git a/app/GHelper.csproj b/app/GHelper.csproj index a2e501eb..ca4b7386 100644 --- a/app/GHelper.csproj +++ b/app/GHelper.csproj @@ -16,7 +16,7 @@ AnyCPU False True - 0.61 + 0.62