예제 #1
0
        private void EnumerateHidDevices(bool connected)
        {
            var enumeratedDevices = HidDevices.Enumerate()
                                    .Where(d => d.IsConnected)
                                    .Where(d => d.Capabilities.InputReportByteLength > 0)
                                    .Where(d => (ushort)d.Capabilities.UsagePage == ConsoleUsagePage)
                                    .Where(d => (ushort)d.Capabilities.Usage == ConsoleUsage);

            if (connected)
            {
                foreach (var device in enumeratedDevices)
                {
                    var listed = Devices.Aggregate(false, (curr, d) => curr | d.HidDevice.DevicePath.Equals(device.DevicePath));

                    if (device != null && !listed)
                    {
                        HidConsoleDevice consoleDevice = new HidConsoleDevice(device)
                        {
                            consoleReportReceived = HidConsoleReportReceived
                        };
                        Devices.Add(consoleDevice);
                        consoleDeviceConnected?.Invoke(consoleDevice);
                    }
                }
            }
            else
            {
                foreach (var device in Devices.ToList())
                {
                    var listed = enumeratedDevices.Aggregate(false, (curr, d) => curr | device.HidDevice.DevicePath.Equals(d.DevicePath));

                    if (!listed)
                    {
                        if (device.HidDevice.IsOpen)
                        {
                            device.HidDevice.CloseDevice();
                        }
                        Devices.Remove(device);
                        device.consoleReportReceived = null;
                        consoleDeviceDisconnected?.Invoke(device);
                    }
                }
            }
        }
예제 #2
0
 public void HidConsoleReportReceived(HidConsoleDevice device, string data)
 {
     consoleReportReceived?.Invoke(device, data);
 }