mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
Logitech - Added retry mechanism to Lightspeed detector
This commit is contained in:
parent
35d61e1496
commit
1f3cef94c8
@ -1,4 +1,4 @@
|
|||||||
using HidSharp;
|
using HidSharp;
|
||||||
using RGB.NET.Core;
|
using RGB.NET.Core;
|
||||||
using RGB.NET.HID;
|
using RGB.NET.HID;
|
||||||
using System;
|
using System;
|
||||||
@ -18,6 +18,7 @@ public class LightspeedHIDLoader<TLed, TData> : IEnumerable<HIDDeviceDefinition<
|
|||||||
{
|
{
|
||||||
#region Constants
|
#region Constants
|
||||||
|
|
||||||
|
private const int LOGITECH_PROTOCOL_TIMEOUT = 300;
|
||||||
private const int VENDOR_ID = 0x046D;
|
private const int VENDOR_ID = 0x046D;
|
||||||
|
|
||||||
// ReSharper disable once StaticMemberInGenericType - This is used like a const
|
// ReSharper disable once StaticMemberInGenericType - This is used like a const
|
||||||
@ -115,49 +116,57 @@ public class LightspeedHIDLoader<TLed, TData> : IEnumerable<HIDDeviceDefinition<
|
|||||||
|
|
||||||
Dictionary<int, byte> map = new();
|
Dictionary<int, byte> map = new();
|
||||||
|
|
||||||
if (deviceUsages.TryGetValue(1, out HidDevice? device))
|
if (!deviceUsages.TryGetValue(1, out HidDevice? device) || !device.TryOpen(out HidStream stream))
|
||||||
|
return map;
|
||||||
|
|
||||||
|
int tries = 0;
|
||||||
|
const int maxTries = 5;
|
||||||
|
while (tries < maxTries)
|
||||||
{
|
{
|
||||||
HidStream? stream = device.Open();
|
try
|
||||||
|
|
||||||
FapResponse response = new();
|
|
||||||
|
|
||||||
FapShortRequest getConnectedDevices = new();
|
|
||||||
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
|
|
||||||
|
|
||||||
stream.Write(getConnectedDevices.AsSpan());
|
|
||||||
stream.Read(response.AsSpan());
|
|
||||||
|
|
||||||
bool wirelessNotifications = (response.Data01 & 1) == 1;
|
|
||||||
if (!wirelessNotifications)
|
|
||||||
{
|
{
|
||||||
response = new FapResponse();
|
stream.ReadTimeout = LOGITECH_PROTOCOL_TIMEOUT;
|
||||||
|
stream.WriteTimeout = LOGITECH_PROTOCOL_TIMEOUT;
|
||||||
|
|
||||||
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
|
FapResponse response = new();
|
||||||
getConnectedDevices.Data1 = 1;
|
|
||||||
|
FapShortRequest getConnectedDevices = new();
|
||||||
|
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
|
||||||
|
|
||||||
stream.Write(getConnectedDevices.AsSpan());
|
stream.Write(getConnectedDevices.AsSpan());
|
||||||
stream.Read(response.AsSpan());
|
stream.Read(response.AsSpan());
|
||||||
|
|
||||||
if (getConnectedDevices.FeatureIndex == 0x8f)
|
bool wirelessNotifications = (response.Data01 & 1) == 1;
|
||||||
|
if (!wirelessNotifications)
|
||||||
{
|
{
|
||||||
//error??
|
response = new FapResponse();
|
||||||
|
|
||||||
|
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
|
||||||
|
getConnectedDevices.Data1 = 1;
|
||||||
|
|
||||||
|
stream.Write(getConnectedDevices.AsSpan());
|
||||||
|
stream.Read(response.AsSpan());
|
||||||
|
|
||||||
|
if (getConnectedDevices.FeatureIndex == 0x8f)
|
||||||
|
{
|
||||||
|
//error??
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
response = new FapResponse();
|
response = new FapResponse();
|
||||||
|
|
||||||
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
|
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
|
||||||
getConnectedDevices.FeatureCommand = 0x02;
|
getConnectedDevices.FeatureCommand = 0x02;
|
||||||
|
|
||||||
stream.Write(getConnectedDevices.AsSpan());
|
stream.Write(getConnectedDevices.AsSpan());
|
||||||
stream.Read(response.AsSpan());
|
stream.Read(response.AsSpan());
|
||||||
|
int deviceCount = response.Data01;
|
||||||
|
if (deviceCount <= 0)
|
||||||
|
return map;
|
||||||
|
|
||||||
int deviceCount = response.Data01;
|
//Add 1 to the device_count to include the receiver
|
||||||
if (deviceCount > 0)
|
|
||||||
{
|
|
||||||
//log "Faking a reconnect to get device list"
|
|
||||||
deviceCount++;
|
deviceCount++;
|
||||||
|
|
||||||
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
|
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
|
||||||
getConnectedDevices.FeatureCommand = 0x02;
|
getConnectedDevices.FeatureCommand = 0x02;
|
||||||
getConnectedDevices.Data0 = 0x02;
|
getConnectedDevices.Data0 = 0x02;
|
||||||
@ -171,6 +180,14 @@ public class LightspeedHIDLoader<TLed, TData> : IEnumerable<HIDDeviceDefinition<
|
|||||||
if (devices.DeviceIndex != 0xff)
|
if (devices.DeviceIndex != 0xff)
|
||||||
map.Add(wirelessPid, devices.DeviceIndex);
|
map.Add(wirelessPid, devices.DeviceIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
tries++;
|
||||||
|
//This might timeout if LGS or GHUB interfere.
|
||||||
|
//Retry.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user