Ejemplo n.º 1
0
    private void Poll()
    {
        int attempts = 0;

        while (!stop_polling & state > state_.NO_JOYCONS)
        {
            SendRumble(rumble_obj.GetData());
            int a = ReceiveRaw();
            a = ReceiveRaw();
            if (a > 0)
            {
                state    = state_.IMU_DATA_OK;
                attempts = 0;
            }
            else if (attempts > 1000)
            {
                state = state_.DROPPED;
                DebugPrint("Connection lost. Is the Joy-Con connected?", DebugType.ALL);
                break;
            }
            else
            {
                DebugPrint("Pause 5ms", DebugType.THREADING);
                Thread.Sleep((Int32)5);
            }

            ++attempts;
        }

        DebugPrint("End poll loop.", DebugType.THREADING);
    }
Ejemplo n.º 2
0
        private Thread PollThreadObj;         // pro times out over time randomly if it was USB and then bluetooth??
        private void Poll()
        {
            int attempts = 0;

            while (!stop_polling & state > state_.NO_JOYCONS)
            {
                SendRumble(rumble_obj.GetData()); // Needed for EVERYTHING to not time out. Never remove pls
                int a = ReceiveRaw();

                if (a > 0)
                {
                    state    = state_.IMU_DATA_OK;
                    attempts = 0;
                }
                else if (attempts > 240)
                {
                    state = state_.DROPPED;
                    form.AppendTextBox("Dropped.\r\n");

                    DebugPrint("Connection lost. Is the Joy-Con connected?", DebugType.ALL);
                    break;
                }
                else
                {
                    //form.AppendTextBox("Pause 5ms");
                    Thread.Sleep((Int32)5);
                }
                ++attempts;
            }
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
    private void Poll()
    {
        int attempts = 0;

        while (!stop_polling & state > state_.NO_JOYCONS)
        {
            SendRumble(rumble_obj.GetData());
            int a = ReceiveRaw();
            a = ReceiveRaw();
            if (a > 0)
            {
                state    = state_.IMU_DATA_OK;
                attempts = 0;
            }
            else if (attempts > 1000)
            {
                state = state_.DROPPED;
                break;
            }
            else
            {
                Thread.Sleep((Int32)5);
            }
            ++attempts;
        }
    }
Ejemplo n.º 5
0
        private Thread PollThreadObj;         // pro times out over time randomly if it was USB and then bluetooth??
        private void Poll()
        {
            int       attempts = 0;
            Stopwatch watch    = new Stopwatch();

            watch.Start();
            while (!stop_polling & state > state_.NO_JOYCONS)
            {
                if (isUSB || rumble_obj.t > 0)
                {
                    SendRumble(rumble_obj.GetData());
                }
                else if (watch.ElapsedMilliseconds >= 1000)
                {
                    // Send a no-op operation as heartbeat to keep connection alive.
                    // Do not send this too frequently, otherwise I/O would be too heavy and cause lag.
                    // Needed for both BLUETOOTH and USB to not time out. Never remove pls
                    SendRumble(rumble_obj.GetData());
                    watch.Restart();
                }
                int a = ReceiveRaw();

                if (a > 0)
                {
                    state    = state_.IMU_DATA_OK;
                    attempts = 0;
                }
                else if (attempts > 240)
                {
                    state = state_.DROPPED;
                    form.AppendTextBox("Dropped.\r\n");

                    DebugPrint("Connection lost. Is the Joy-Con connected?", DebugType.ALL);
                    break;
                }
                else if (a < 0)
                {
                    // An error on read.
                    //form.AppendTextBox("Pause 5ms");
                    Thread.Sleep((Int32)5);
                    ++attempts;
                }
                else if (a == 0)
                {
                    // The non-blocking read timed out. No need to sleep.
                    // No need to increase attempts because it's not an error.
                }
            }
        }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
        public void Detach()
        {
            stop_polling = true;

            if (state > state_.NO_JOYCONS)
            {
                Subcommand(0x40, new byte[] { 0x0 }, 1);
                Subcommand(0x48, new byte[] { 0x0 }, 1);

                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));
                }
            }
            if (state > state_.DROPPED)
            {
                HIDapi.hid_close(handle);
            }
            state = state_.NOT_ATTACHED;
        }
Ejemplo n.º 8
0
 public int Attach(byte leds_ = 0x0)
 {
     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);
     return(0);
 }
Ejemplo n.º 9
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();
                Console.WriteLine("Using USB.");

                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();
            }

            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);

            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);
        }
Ejemplo n.º 10
0
    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);
    }