void Awake() { if (instance != null) { Destroy(gameObject); } instance = this; DontDestroyOnLoad(this.gameObject); int i = 0; j = new List <Joycon>(); bool isLeft = false; HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0); IntPtr top_ptr = ptr; if (ptr == IntPtr.Zero) { ptr = HIDapi.hid_enumerate(vendor_id_, 0x0); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); //Debug.Log ("No Joy-Cons found!"); } } hid_device_info enumerate; while (ptr != IntPtr.Zero) { enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); //Debug.Log (enumerate.product_id); if (enumerate.product_id == product_l || enumerate.product_id == product_r) { if (enumerate.product_id == product_l) { isLeft = true; //Debug.Log ("Left Joy-Con connected."); } else if (enumerate.product_id == product_r) { isLeft = false; //Debug.Log ("Right Joy-Con connected."); } else { //Debug.Log ("Non Joy-Con input device skipped."); } IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft)); ++i; } ptr = enumerate.next; } HIDapi.hid_free_enumeration(top_ptr); }
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); }
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); }
void Start() { joycons = new List <JoyCon>(); HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(VENDOR_TOP, 0x0); IntPtr top_ptr = ptr; if (ptr == IntPtr.Zero) { ptr = HIDapi.hid_enumerate(VENDOR_ID, 0x0); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); Debug.Log("No Joy-Cons found!"); } } while (ptr != IntPtr.Zero) { var enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); Debug.Log(enumerate.product_id); if (enumerate.product_id == PRODUCT_L_ID || enumerate.product_id == PRODUCT_R_ID) { var type = JoyConType.NONE; if (enumerate.product_id == PRODUCT_L_ID) { type = JoyConType.LEFT; Debug.Log("Left Joy-Con connected."); } else if (enumerate.product_id == PRODUCT_R_ID) { type = JoyConType.RIGHT; Debug.Log("Right Joy-Con connected."); } IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); joycons.Add(new JoyCon(type, handle, true)); } ptr = enumerate.next; } HIDapi.hid_free_enumeration(top_ptr); //TODO: Move to other place Attach(); }
public void Awake() { instance = this; int i = 0; j = new List <Joycon>(); bool isLeft = false; HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0); IntPtr top_ptr = ptr; if (ptr == IntPtr.Zero) { ptr = HIDapi.hid_enumerate(vendor_id_, 0x0); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); } } hid_device_info enumerate; while (ptr != IntPtr.Zero) { enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); if (enumerate.product_id == product_l || enumerate.product_id == product_r) { if (enumerate.product_id == product_l) { isLeft = true; } else if (enumerate.product_id == product_r) { isLeft = false; } else { } IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft)); ++i; } ptr = enumerate.next; } HIDapi.hid_free_enumeration(top_ptr); }
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; }
internal void Scan() { if (instance != null) { ; } instance = this; int i = 0; bool isLeft = false; bool isvalid = true; HIDapi.hid_init(); IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0); IntPtr top_ptr = ptr; if (ptr == IntPtr.Zero) { ptr = HIDapi.hid_enumerate(vendor_id_, 0x0); if (ptr == IntPtr.Zero) { HIDapi.hid_free_enumeration(ptr); Console.WriteLine("No Joy-Cons found!"); } } hid_device_info enumerate; while (ptr != IntPtr.Zero) { enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); isvalid = false; //Console.WriteLine(enumerate.product_id); if (enumerate.product_id == product_l || enumerate.product_id == product_r) { if (enumerate.product_id == product_l) { isvalid = true; isLeft = true; Console.WriteLine("Left Joy-Con connected."); } else if (enumerate.product_id == product_r) { isvalid = true; isLeft = false; Console.WriteLine("Right Joy-Con connected."); } else { //Console.WriteLine("Non Joy-Con input device skipped."); } if (isvalid) { IntPtr handle = HIDapi.hid_open_path(enumerate.path); HIDapi.hid_set_nonblocking(handle, 1); j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.04f, isLeft)); } ++i; } ptr = enumerate.next; } HIDapi.hid_free_enumeration(top_ptr); }
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); }