예제 #1
0
        // Get a list of connected Pinscape Controller devices
        public static List <PinscapeDev> FindDevices()
        {
            // set up an empty return list
            var devices = new List <PinscapeDev>();

            // get the list of devices matching the HID class GUID
            Guid guid = Guid.Empty;

            HIDImports.HidD_GetHidGuid(out guid);
            IntPtr hdev = HIDImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HIDImports.DIGCF_DEVICEINTERFACE);

            // set up the attribute structure buffer
            HIDImports.SP_DEVICE_INTERFACE_DATA diData = new HIDImports.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            // read the devices in our list
            for (uint i = 0;
                 HIDImports.SetupDiEnumDeviceInterfaces(hdev, IntPtr.Zero, ref guid, i, ref diData);
                 ++i)
            {
                // get the size of the detail data structure
                UInt32 size = 0;
                HIDImports.SetupDiGetDeviceInterfaceDetail(hdev, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                // now actually read the detail data structure
                HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA();
                diDetail.cbSize = (IntPtr.Size == 8) ? (uint)8 : (uint)5;
                HIDImports.SP_DEVINFO_DATA devInfoData = new HIDImports.SP_DEVINFO_DATA();
                devInfoData.cbSize = Marshal.SizeOf(devInfoData);
                if (HIDImports.SetupDiGetDeviceInterfaceDetail(hdev, ref diData, ref diDetail, size, out size, out devInfoData))
                {
                    // create a file handle to access the device
                    IntPtr fp = HIDImports.CreateFile(
                        diDetail.DevicePath, HIDImports.GENERIC_READ_WRITE, HIDImports.SHARE_READ_WRITE,
                        IntPtr.Zero, HIDImports.OPEN_EXISTING, 0, IntPtr.Zero);

                    // read the attributes
                    HIDImports.HIDD_ATTRIBUTES attrs = new HIDImports.HIDD_ATTRIBUTES();
                    attrs.Size = Marshal.SizeOf(attrs);
                    if (HIDImports.HidD_GetAttributes(fp, ref attrs))
                    {
                        // read the product name string
                        String name    = "<unknown>";
                        byte[] nameBuf = new byte[128];
                        if (HIDImports.HidD_GetProductString(fp, nameBuf, 128))
                        {
                            name = System.Text.Encoding.Unicode.GetString(nameBuf).TrimEnd('\0');
                        }

                        // if the vendor and product ID match an LedWiz, and the
                        // product name contains "pinscape", it's one of ours
                        PinscapeDev di;
                        if (CheckIDMatch(attrs) &&
                            Regex.IsMatch(name, @"\b(?i)pinscape controller\b") &&
                            (di = PinscapeDev.Create(
                                 diDetail.DevicePath, name, attrs.VendorID, attrs.ProductID, attrs.VersionNumber)) != null)
                        {
                            // add the device to our list
                            devices.Add(di);
                        }

                        // done with the file handle
                        if (fp.ToInt32() != 0 && fp.ToInt32() != -1)
                        {
                            HIDImports.CloseHandle(fp);
                        }
                    }
                }
            }

            // done with the device info list
            HIDImports.SetupDiDestroyDeviceInfoList(hdev);

            // return the device list
            return(devices);
        }
예제 #2
0
        private static PinscapeDev Create(string path, string name, ushort vendorID, ushort productID, ushort version)
        {
            var di = new PinscapeDev(path, name, vendorID, productID, version);

            return(di.isValid ? di : null);
        }