Example #1
0
        public void Detach()
        {
            stop_polling = true;

            if (xin != null)
            {
                xin.Disconnect(); xin.Dispose();
            }

            if (state > state_.NO_JOYCONS)
            {
                HIDapi.hid_set_nonblocking(handle, 0);

                Subcommand(0x40, new byte[] { 0x0 }, 1);
                //Subcommand(0x48, new byte[] { 0x0 }, 1); // Would turn off rumble?

                if (isUSB)
                {
                    byte[] a = Enumerable.Repeat((byte)0, 64).ToArray();
                    a[0] = 0x80; a[1] = 0x05;                     // Allow device to talk to BT again
                    HIDapi.hid_write(handle, a, new UIntPtr(2));
                    a[0] = 0x80; a[1] = 0x06;                     // Allow device to talk to BT again
                    HIDapi.hid_write(handle, a, new UIntPtr(2));
                }
            }
            if (state > state_.DROPPED)
            {
                HIDapi.hid_close(handle);
            }
            state = state_.NOT_ATTACHED;
        }
Example #2
0
        private byte[] Subcommand(byte sc, byte[] buf, uint len, bool print = true)
        {
            byte[] buf_     = new byte[report_len];
            byte[] response = new byte[report_len];
            Array.Copy(default_buf, 0, buf_, 2, 8);
            Array.Copy(buf, 0, buf_, 11, len);
            buf_[10] = sc;
            buf_[1]  = global_count;
            buf_[0]  = 0x1;
            if (global_count == 0xf)
            {
                global_count = 0;
            }
            else
            {
                ++global_count;
            }
            if (print)
            {
                PrintArray(buf_, DebugType.COMMS, len, 11, "Subcommand 0x" + string.Format("{0:X2}", sc) + " sent. Data: 0x{0:S}");
            }
            ;
            HIDapi.hid_write(handle, buf_, new UIntPtr(len + 11));
            int res = HIDapi.hid_read_timeout(handle, response, new UIntPtr(report_len), 50);

            if (res < 1)
            {
                DebugPrint("No response.", DebugType.COMMS);
            }
            else if (print)
            {
                PrintArray(response, DebugType.COMMS, report_len - 1, 1, "Response ID 0x" + string.Format("{0:X2}", response[0]) + ". Data: 0x{0:S}");
            }
            return(response);
        }
Example #3
0
 private void SendRumble(byte[] buf)
 {
     byte[] buf_ = new byte[report_len];
     buf_[0] = 0x10;
     buf_[1] = global_count;
     if (global_count == 0xf)
     {
         global_count = 0;
     }
     else
     {
         ++global_count;
     }
     Array.Copy(buf, 0, buf_, 2, 8);
     PrintArray(buf_, DebugType.RUMBLE, format: "Rumble data sent: {0:S}");
     HIDapi.hid_write(handle, buf_, new UIntPtr(report_len));
 }
Example #4
0
        public int Attach(byte leds_ = 0x0)
        {
            state = state_.ATTACHED;

            // Make sure command is received
            HIDapi.hid_set_nonblocking(handle, 0);

            byte[] a = { 0x0 };

            // Connect
            if (!isUSB)
            {
                // Input report mode
                Subcommand(0x03, new byte[] { 0x30 }, 1, false);

                a[0] = 0x1;
                dump_calibration_data();
            }
            else
            {
                Subcommand(0x03, new byte[] { 0x3f }, 1, false);

                a = Enumerable.Repeat((byte)0, 64).ToArray();
                form.AppendTextBox("Using USB.\r\n");

                a[0] = 0x80;
                a[1] = 0x01;
                HIDapi.hid_write(handle, a, new UIntPtr(2));
                HIDapi.hid_read(handle, a, new UIntPtr(64));

                if (a[2] != 0x3)
                {
                    PadMacAddress = new PhysicalAddress(new byte[] { a[9], a[8], a[7], a[6], a[5], a[4] });
                }

                // USB Pairing
                a    = Enumerable.Repeat((byte)0, 64).ToArray();
                a[0] = 0x80; a[1] = 0x02;                 // Handshake
                HIDapi.hid_write(handle, a, new UIntPtr(2));

                a[0] = 0x80; a[1] = 0x03;                 // 3Mbit baud rate
                HIDapi.hid_write(handle, a, new UIntPtr(2));

                a[0] = 0x80; a[1] = 0x02;                 // Handshake at new baud rate
                HIDapi.hid_write(handle, a, new UIntPtr(2));

                a[0] = 0x80; a[1] = 0x04;                 // Prevent HID timeout
                HIDapi.hid_write(handle, a, new UIntPtr(2));

                dump_calibration_data();
            }

            BlinkHomeLight();

            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[] { 0x01 }, 1, true);

            Subcommand(0x41, new byte[] { 0x03, 0x00, 0x00, 0x01 }, 4, false);             // higher gyro performance rate

            DebugPrint("Done with init.", DebugType.COMMS);

            HIDapi.hid_set_nonblocking(handle, 1);

            return(0);
        }