public static bool FindWiimotes() { IntPtr ptr = HIDapi.hid_enumerate(vid, pid); if (ptr == IntPtr.Zero) { ptr = HIDapi.hid_enumerate(vid, pid2); if (ptr == IntPtr.Zero) { return(false); } } IntPtr cur_ptr = ptr; hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); bool found = false; while (cur_ptr != IntPtr.Zero) { DS4 remote = null; bool fin = false; foreach (DS4 r in Controllers) { if (fin) { continue; } if (r.hidapi_path.Equals(enumerate.path)) { remote = r; fin = true; } } if (remote == null) { IntPtr handle = HIDapi.hid_open_path(enumerate.path); remote = new DS4(handle, enumerate.path); Debug.Log("Found New Remote: " + remote.hidapi_path); Controllers.Add(remote); // TODO: Initialization (?) } cur_ptr = enumerate.next; if (cur_ptr != IntPtr.Zero) { enumerate = (hid_device_info)Marshal.PtrToStructure(cur_ptr, typeof(hid_device_info)); } } HIDapi.hid_free_enumeration(ptr); return(found); }
private static bool _FindWiimotes(WiimoteType a_MoteType) { ushort uVendor = 0; ushort uProduct = 0; if(a_MoteType == WiimoteType.WIIMOTE) { uVendor = c_uVendorIDWiiMote; uProduct = c_uProductIDWiiMote; } IntPtr ptr = CS_HIDapi.hid_enumerate(uVendor, uProduct); IntPtr cur_ptr = ptr; if (ptr == IntPtr.Zero) return false; hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); bool bHasFound = false; while(cur_ptr != IntPtr.Zero) { CS_WiiMote Remote = null; bool bEnd = false; foreach (CS_WiiMote r in Wiimotes) { if (bEnd) continue; if (r.hidapi_path.Equals(enumerate.path)) { Remote = r; bEnd = true; } } if (Remote == null) { IntPtr handle = CS_HIDapi.hid_open_path(enumerate.path); Remote = new CS_WiiMote(handle, enumerate.path, a_MoteType); if (bDebugMessages) Debug.Log("Found New Remote: " + Remote.hidapi_path); Wiimotes.Add(Remote); Remote.SendStatusInfoRequest(); } cur_ptr = enumerate.next; if(cur_ptr != IntPtr.Zero) enumerate = (hid_device_info)Marshal.PtrToStructure(cur_ptr, typeof(hid_device_info)); } CS_HIDapi.hid_free_enumeration(ptr); return bHasFound; }
public bool FindOmni() { Debug.Log(System.DateTime.Now.ToLongTimeString() + ": OmniManager(FindOmni) - Trying to find the Omni"); ushort vendor = 0; ushort product = 0; vendor = vendor_id_omni; product = product_id_omni; IntPtr ptr = HIDapi.hid_enumerate(vendor, product); IntPtr cur_ptr = ptr; if (ptr == IntPtr.Zero) { return(false); } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); bool found = false; while (cur_ptr != IntPtr.Zero) { IntPtr handle = HIDapi.hid_open_path(enumerate.path); if (enumerate.path.Contains("mi_04")) { hidapi_handle = handle; HIDapi.hid_set_nonblocking(hidapi_handle, 1); found = true; omniDisconnected = false; break; } if (enumerate.path.Contains("mi_00")) { hidapi_handle = handle; HIDapi.hid_set_nonblocking(hidapi_handle, 1); found = true; omniDisconnected = false; break; } cur_ptr = enumerate.next; if (cur_ptr != IntPtr.Zero) { enumerate = (hid_device_info)Marshal.PtrToStructure(cur_ptr, typeof(hid_device_info)); } } HIDapi.hid_free_enumeration(ptr); Debug.Log(System.DateTime.Now.ToLongTimeString() + ": OmniManager(FindOmni) - Result of trying to find the Omni = " + found); return(found); }
public bool FindOmni() { Debug.Log("finding omni"); ushort vendor = 0; ushort product = 0; vendor = vendor_id_omni; product = product_id_omni; IntPtr ptr = HIDapi.hid_enumerate(vendor, product); IntPtr cur_ptr = ptr; if (ptr == IntPtr.Zero) { return(false); } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); bool found = false; while (cur_ptr != IntPtr.Zero) { IntPtr handle = HIDapi.hid_open_path(enumerate.path); if (enumerate.path.Contains("mi_04")) { hidapi_handle = handle; HIDapi.hid_set_nonblocking(hidapi_handle, 1); found = true; break; } if (enumerate.path.Contains("mi_00")) { hidapi_handle = handle; HIDapi.hid_set_nonblocking(hidapi_handle, 1); found = true; break; } cur_ptr = enumerate.next; if (cur_ptr != IntPtr.Zero) { enumerate = (hid_device_info)Marshal.PtrToStructure(cur_ptr, typeof(hid_device_info)); } } HIDapi.hid_free_enumeration(ptr); Debug.Log(found); return(found); }
static void PrintEnumeration(IntPtr phid_device_info) { if (!phid_device_info.Equals(IntPtr.Zero)) { hid_device_info hdev = (hid_device_info)Marshal.PtrToStructure(phid_device_info, typeof(hid_device_info)); Console.WriteLine(string.Format("path: {0}", hdev.path)); Console.WriteLine(string.Format("vendor id: {0:X}", hdev.vendor_id)); Console.WriteLine(string.Format("product id: {0:X}", hdev.product_id)); Console.WriteLine(string.Format("usage page: {0:X}", hdev.usage_page)); Console.WriteLine(string.Format("usage: {0:X}", hdev.usage)); Console.WriteLine(""); PrintEnumeration(hdev.next); } }
static string _getDevicePath(IntPtr phid_device_info, ushort usagePage, ushort usage) { if (!phid_device_info.Equals(IntPtr.Zero)) { hid_device_info hdev = (hid_device_info)Marshal.PtrToStructure(phid_device_info, typeof(hid_device_info)); if (usagePage == hdev.usage_page && usage == hdev.usage) { return(hdev.path); } else { return(_getDevicePath(hdev.next, usagePage, usage)); } } return(null); }
bool TryConnect() { IntPtr ptr = HIDapi.hid_enumerate(USB_VID, USB_PID); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); DebugPrint("USB receiver not found."); return(false); } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); HIDapi.hid_free_enumeration(ptr); addReport(setup_buffer); return(true); }
public int Attach(byte leds_ = 0x0, bool imu = true, float alpha = 0.01f, bool localize = false) { imu_enabled = imu; do_localize = localize & imu; filterweight = alpha; state = state_.NOT_ATTACHED; HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0); if (ptr == IntPtr.Zero) { ptr = HIDapi.hid_enumerate(vendor_id_, 0x0); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); DebugPrint("No Joy-Cons found.", DebugType.ALL); state = state_.NO_JOYCONS; return(-1); } } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); if (enumerate.product_id == product_l) { isLeft = true; DebugPrint("Left Joy-Con connected.", DebugType.ALL); } else if (enumerate.product_id == product_r) { DebugPrint("Right Joy-Con connected.", DebugType.ALL); } else { HIDapi.hid_free_enumeration(ptr); DebugPrint("No Joy-Cons found.", DebugType.ALL); state = state_.NO_JOYCONS; return(-1); } handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); HIDapi.hid_free_enumeration(ptr); state = state_.ATTACHED; byte[] a = { 0x0 }; // Input report mode Subcommand(0x3, new byte[] { 0x3f }, 1, false); a[0] = 0x1; dump_calibration_data(); // Connect a[0] = 0x01; Subcommand(0x1, a, 1); a[0] = 0x02; Subcommand(0x1, a, 1); a[0] = 0x03; Subcommand(0x1, a, 1); a[0] = leds_; Subcommand(0x30, a, 1); Subcommand(0x40, new byte[] { (imu_enabled ? (byte)0x1 : (byte)0x0) }, 1, true); Subcommand(0x3, new byte[] { 0x30 }, 1, true); Subcommand(0x48, new byte[] { 0x1 }, 1, true); DebugPrint("Done with init.", DebugType.COMMS); return(0); }
private static int rw(byte start_page, int bytes, byte[] in_buf, bool read = true) { bool connect_success = true; IntPtr ptr = HIDapi.hid_enumerate(USB_VID, USB_PID); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); connect_success = false; return(-4); } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); HIDapi.hid_free_enumeration(ptr); if (!connect_success) { HIDapi.hid_close(handle); return(-4); } int numPages = bytes / pagelen; if (bytes % pagelen != 0) { ++numPages; } int in_buf_ind = 0; byte[] r = new byte[recvlen]; byte[] s = new byte[sendlen]; for (int i = start_page; i < start_page + numPages; ++i) { byte cmd = (byte)(read ? i & ~0x80 : i | 0x80); if (!read) { for (int j = 2; j < sendlen; ++j) { if (in_buf_ind < bytes) { s[j] = in_buf[in_buf_ind]; ++in_buf_ind; } } } s[0] = 0; s[1] = cmd; int res = 0; int err = 0; res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen)); if (res < 1) { err = -1; } else { res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000); if (res < 1) { err = -2; } if (r[1] != s[1]) { err = -3; } } if (err == 0) { if (read) { for (int j = 2; j < recvlen; ++j) { if (in_buf_ind < bytes) { in_buf[in_buf_ind] = r[j]; ++in_buf_ind; } } } } else { HIDapi.hid_close(handle); return(err); } string formatstr = string.Concat(String.Format("page {0:D}, read: {1:B}, data: ", i, read), "{0:S}"); PrintArray(r, format: formatstr); } HIDapi.hid_close(handle); return(0); }
private static bool _FindWiimotes(WiimoteType type) { //if (hidapi_wiimote != IntPtr.Zero) // HIDapi.hid_close(hidapi_wiimote); ushort vendor = 0; ushort product = 0; if (type == WiimoteType.WIIMOTE) { vendor = vendor_id_wiimote; product = product_id_wiimote; } else if (type == WiimoteType.WIIMOTEPLUS || type == WiimoteType.PROCONTROLLER) { vendor = vendor_id_wiimote; product = product_id_wiimoteplus; } IntPtr ptr = HIDapi.hid_enumerate(vendor, product); IntPtr cur_ptr = ptr; if (ptr == IntPtr.Zero) return false; hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); bool found = false; while (cur_ptr != IntPtr.Zero) { Wiimote remote = null; bool fin = false; foreach (Wiimote r in Wiimotes) { if (fin) continue; if (r.hidapi_path.Equals(enumerate.path)) { remote = r; fin = true; } } if (remote == null) { IntPtr handle = HIDapi.hid_open_path(enumerate.path); WiimoteType trueType = type; // Wii U Pro Controllers have the same identifiers as the newer Wii Remote Plus except for product // string (WHY nintendo...) if (enumerate.product_string.EndsWith("UC")) trueType = WiimoteType.PROCONTROLLER; remote = new Wiimote(handle, enumerate.path, trueType); if (Debug_Messages) Debug.Log("Found New Remote: " + remote.hidapi_path); Wiimotes.Add(remote); remote.SendDataReportMode(InputDataType.REPORT_BUTTONS); remote.SendStatusInfoRequest(); } cur_ptr = enumerate.next; if (cur_ptr != IntPtr.Zero) enumerate = (hid_device_info)Marshal.PtrToStructure(cur_ptr, typeof(hid_device_info)); } HIDapi.hid_free_enumeration(ptr); return found; }
private static int rw(uint start_page, int bytes, byte[] in_buf, bool read = true, bool eeprom_write = false) { if (eeprom_write) { read = true; } bool connect_success = true; HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(USB_VID, USB_PID); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); HIDapi.hid_exit(); connect_success = false; return(-4); } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); HIDapi.hid_free_enumeration(ptr); if (!connect_success) { HIDapi.hid_close(handle); HIDapi.hid_exit(); return(-4); } int numPages = bytes / pagelen; if (bytes % pagelen != 0) { ++numPages; } int in_buf_ind = 0; byte[] r = new byte[recvlen]; byte[] s = new byte[sendlen]; uint i = 0; for (i = start_page; i < start_page + numPages; ++i) { s[0] = 0; // pack address bytes in big endian s[1] = 0; s[2] = (byte)((i >> 8) & 0xff); s[3] = (byte)(i & 0xff); byte crc = 0; string formatstr; if (!read | eeprom_write) { for (int j = addrlen + 1; j < sendlen; ++j) { if (in_buf_ind < bytes) { s[j] = in_buf[in_buf_ind]; ++in_buf_ind; } crc = crc7_table[crc ^ s[j]]; } crc >>= 1; s[1] = (byte)0x80; s[1] |= crc; } if (eeprom_write) { s[1] = (byte)0x20; } if (debugdump) { formatstr = "sent page: {0:S}"; PrintArray(s, format: formatstr); } int res = 0; int err = 0; res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen)); if (res < 1) { err = -1; } else { res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000); if (res < 1) { err = -2; } if (r[1] != s[1] || r[2] != s[2] || r[3] != s[3]) { err = -3; } } if (err == 0) { if (read) { for (int j = addrlen + 1; j < recvlen; ++j) { if (in_buf_ind < bytes) { in_buf[in_buf_ind] = r[j]; ++in_buf_ind; } } } if (debugdump) { formatstr = string.Concat(String.Format("page {0:D}, read: {1:B}, err: {2:D}, data: ", i, read, err), "{0:S}"); PrintArray(r, format: formatstr); } } else { if (debugdump) { print("error reading"); print(err.ToString()); } HIDapi.hid_close(handle); HIDapi.hid_exit(); return(err); } formatstr = string.Concat(String.Format("page {0:D}, read: {1:B}, data: ", i, read), "{0:S}"); PrintArray(r, format: formatstr); } // tell device to copy flash memory out of buffer if (!read && !eeprom_write) { s[1] = 0x40; s[2] = (byte)((start_page >> 8) & 0xff); s[3] = (byte)(start_page & 0xff); s[4] = (byte)((i >> 8) & 0xff); s[5] = (byte)(i & 0xff); int res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen)); if (res < 1) { HIDapi.hid_close(handle); HIDapi.hid_exit(); return(-2); } res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000); if (res < 1) { HIDapi.hid_close(handle); HIDapi.hid_exit(); return(-2); } if (r[1] != s[1] || r[2] != s[2] || r[3] != s[3]) { HIDapi.hid_close(handle); HIDapi.hid_exit(); return(-3); } } HIDapi.hid_close(handle); HIDapi.hid_exit(); return(0); }
public static int SetLEDBrightness(float brightness_) { if (led_brightness_locked) { return(-4); } if (brightness_ > 1f) { return(-5); } HIDapi.hid_init(); byte brightness = (byte)(brightness_ * 255); IntPtr ptr = HIDapi.hid_enumerate(USB_VID, USB_PID); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); HIDapi.hid_exit(); return(-4); } hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); HIDapi.hid_free_enumeration(ptr); byte[] r = new byte[recvlen]; byte[] s = new byte[sendlen]; for (int i = 0; i < sendlen; ++i) { s[i] = 0; } s[1] = (byte)0x10; s[2] = brightness; int res = 0; int err = 0; res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen)); if (res < 1) { err = -1; } else { res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000); if (res < 1) { err = -2; } if (r[1] != s[1] || r[2] != s[2] || r[3] != s[3]) { led_brightness_locked = true; err = -3; } if (r[4] == 0xff) { led_brightness_locked = true; err = -4; } } HIDapi.hid_close(handle); HIDapi.hid_exit(); return(err); }