/// \brief Disables the given \c Wiimote by closing its bluetooth HID connection. Also removes the remote from Wiimotes /// \param remote The remote to cleanup public static void Cleanup(Wiimote remote) { if (remote.hidapi_handle != IntPtr.Zero) HIDapi.hid_close(remote.hidapi_handle); Wiimotes.Remove(remote); }
public void Cleanup() { if (hidapi_handle != IntPtr.Zero) { HIDapi.hid_close(hidapi_handle); } }
public static void Cleanup(DS4 remote) { if (remote != null) { if (remote.hidapi_handle != IntPtr.Zero) { HIDapi.hid_close(remote.hidapi_handle); } Controllers.Remove(remote); } }
public void Detach() { stop_polling = true; PrintArray(max, format: "Max {0:S}", d: DebugType.IMU); PrintArray(sum, format: "Sum {0:S}", d: DebugType.IMU); if (state > state_.NO_JOYCONS) { Subcommand(0x30, new byte[] { 0x0 }, 1); Subcommand(0x40, new byte[] { 0x0 }, 1); Subcommand(0x48, new byte[] { 0x0 }, 1); Subcommand(0x3, new byte[] { 0x3f }, 1); } if (state > state_.DROPPED) { HIDapi.hid_close(handle); } state = state_.NOT_ATTACHED; }
public void Detach() { end = true; if (State > State.NO_JOYCONS) { new Subcommand(SubcommandType.SetPlayerLights).PackByte(0x0).Send(handle); new Subcommand(SubcommandType.EnableIMU).PackBool(false).Send(handle); new Subcommand(SubcommandType.EnableVibration).PackBool(false).Send(handle); new Subcommand(SubcommandType.SetInputReportMode).PackByte(0x3f).Send(handle); } if (State > State.DROPPED) { HIDapi.hid_close(handle); } State = State.NOT_ATTACHED; //Debug.Log("Detached."); }
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); }
void OnApplicationQuit() { connected = false; DebugPrint("Done"); HIDapi.hid_close(handle); }
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); }