This commit is contained in:
Serge
2023-11-19 15:15:19 +01:00
parent ccf2ae9eed
commit f2085b836b
2 changed files with 36 additions and 17 deletions

View File

@@ -14,21 +14,18 @@ public static class AsusHid
static HidStream? auraStream; static HidStream? auraStream;
public static IEnumerable<HidDevice>? FindDevices(byte reportId, int minFeatureLength = 1) public static IEnumerable<HidDevice>? FindDevices(byte reportId)
{ {
HidDeviceLoader loader = new HidDeviceLoader(); HidDeviceLoader loader = new HidDeviceLoader();
IEnumerable<HidDevice> deviceList; IEnumerable<HidDevice> deviceList;
try try
{ {
deviceList = loader.GetDevices(ASUS_ID).Where( deviceList = loader.GetDevices(ASUS_ID).Where(device => deviceIds.Contains(device.ProductID) && device.CanOpen && device.GetMaxFeatureReportLength() > 0);
device => deviceIds.Contains(device.ProductID) &&
device.CanOpen &&
device.GetMaxFeatureReportLength() >= minFeatureLength);
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"Error enumerating HID devices: {ex.Message}"); Logger.WriteLine($"Error enumerating HID devices: {ex.Message}");
yield break; yield break;
} }
@@ -37,28 +34,50 @@ public static class AsusHid
yield return device; yield return device;
} }
public static HidStream? FindHidStream(byte reportId, int minFeatureLength = 1) public static HidStream? FindHidStream(byte reportId)
{ {
try try
{ {
return FindDevices(reportId, minFeatureLength)?.FirstOrDefault()?.Open(); return FindDevices(reportId)?.FirstOrDefault()?.Open();
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"Error accessing HID device: {ex.Message}"); Logger.WriteLine($"Error accessing HID device: {ex.Message}");
} }
return null; return null;
} }
public static void Write(byte[] data, byte reportId = AURA_ID, string log = "USB") public static void WriteInput(byte[] data, string log = "USB")
{ {
Write(new List<byte[]> { data }, reportId, log); foreach (var device in FindDevices(INPUT_ID))
{
try
{
using (var stream = device.Open())
{
var payload = new byte[device.GetMaxFeatureReportLength()];
Array.Copy(data, payload, data.Length);
stream.SetFeature(payload);
Logger.WriteLine($"{log} Feature {device.ProductID.ToString("X")}|{device.GetMaxFeatureReportLength()}: {BitConverter.ToString(data)}");
}
}
catch (Exception ex)
{
Logger.WriteLine($"Error setting feature {device.GetMaxFeatureReportLength()} {device.DevicePath}: {BitConverter.ToString(data)} {ex.Message}");
}
}
} }
public static void Write(List<byte[]> dataList, byte reportId = AURA_ID, string log = "USB") public static void Write(byte[] data, string log = "USB")
{ {
var devices = FindDevices(reportId); Write(new List<byte[]> { data }, log);
}
public static void Write(List<byte[]> dataList, string log = "USB")
{
var devices = FindDevices(AURA_ID);
if (devices is null) return; if (devices is null) return;
try try
@@ -68,12 +87,12 @@ public static class AsusHid
foreach (var data in dataList) foreach (var data in dataList)
{ {
stream.Write(data); stream.Write(data);
Logger.WriteLine($"{log} " + device.ProductID.ToString("X") + ": " + BitConverter.ToString(data)); Logger.WriteLine($"{log} {device.ProductID.ToString("X")}: {BitConverter.ToString(data)}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"Error writing {log} to HID device: {ex.Message}"); Logger.WriteLine($"Error writing {log}: {ex.Message}");
} }
} }

View File

@@ -263,9 +263,9 @@ namespace GHelper.USB
if (delay) await Task.Delay(TimeSpan.FromSeconds(1)); if (delay) await Task.Delay(TimeSpan.FromSeconds(1));
if (isACPI) Program.acpi.TUFKeyboardBrightness(brightness); if (isACPI) Program.acpi.TUFKeyboardBrightness(brightness);
AsusHid.Write(new byte[] { AsusHid.AURA_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.AURA_ID, log); AsusHid.Write(new byte[] { AsusHid.AURA_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, log);
if (AppConfig.ContainsModel("GA503")) if (AppConfig.ContainsModel("GA503"))
AsusHid.Write(new byte[] { AsusHid.INPUT_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.INPUT_ID, log); AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, log);
}); });