/// <summary> /// Searches for JoyCon and initalizes ConnectedJoyCons /// </summary> /// <returns>Number of connected joycons</returns> public int ConnectJoyCons() { HIDapi.InitializeDll(); ConnectedJoyCons = new List <JoyCon>(); HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0); IntPtr top_ptr = ptr; while (ptr != IntPtr.Zero) { var hidDeviceInfo = (HIDapi.hid_device_info)Marshal.PtrToStructure(ptr, typeof(HIDapi.hid_device_info)); if (IsJoyCon(hidDeviceInfo.product_id)) { bool isLeft = hidDeviceInfo.product_id == product_l; Log.Debug(hidDeviceInfo.product_id); Log.Debug(isLeft ? "Left Joy-Con connected." : "Right Joy-Con connected."); IntPtr hid_device = HIDapi.hid_open_path(hidDeviceInfo.path); HIDapi.hid_set_nonblocking(hid_device, 1); ConnectedJoyCons.Add(new JoyCon(hid_device, EnableIMU, EnableLocalize & EnableIMU, 0.04f, isLeft)); } ptr = hidDeviceInfo.next; } HIDapi.hid_free_enumeration(top_ptr); if (ConnectedJoyCons.Count == 0) { Log.Debug("No Joy-Cons found!"); } return(ConnectedJoyCons.Count); }
private int ReceiveRaw() { if (handle == IntPtr.Zero) { return(-2); } HIDapi.hid_set_nonblocking(handle, 0); byte[] buffer = new byte[report_len]; int bytesRead = HIDapi.hid_read(handle, buffer, report_len); if (bytesRead > 0) { lock (reports) { reports.Enqueue(new Report(buffer)); } if (ts_en == buffer[1]) { DebugPrint(string.Format("Duplicate timestamp enqueued. TS: {0:X2}", ts_en)); } ts_en = buffer[1]; DebugPrint(string.Format("Enqueue. Bytes read: {0:D}. Timestamp: {1:X2}", bytesRead, buffer[1])); } return(bytesRead); }