Пример #1
0
    private int ReceiveRaw()
    {
        if (handle == IntPtr.Zero)
        {
            return(-2);
        }
        HIDapi.hid_set_nonblocking(handle, 0);
        byte[] raw_buf = new byte[report_len];
        int    ret     = HIDapi.hid_read(handle, raw_buf, new UIntPtr(report_len));

        if (ret > 0)
        {
            lock (reports)
            {
                reports.Enqueue(new Report(raw_buf, System.DateTime.Now));
            }
            if (ts_en == raw_buf[1])
            {
                DebugPrint(string.Format("Duplicate timestamp enqueued. TS: {0:X2}", ts_en), DebugType.THREADING);
            }
            ts_en = raw_buf[1];
            DebugPrint(string.Format("Enqueue. Bytes read: {0:D}. Timestamp: {1:X2}", ret, raw_buf[1]), DebugType.THREADING);
        }
        return(ret);
    }
Пример #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);
    }
Пример #3
0
    void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
        }
        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);
                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);
    }
Пример #4
0
        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);
        }
Пример #5
0
 public void Cleanup()
 {
     if (hidapi_handle != IntPtr.Zero)
     {
         HIDapi.hid_close(hidapi_handle);
         hidapi_handle = IntPtr.Zero;
     }
 }
Пример #6
0
 public void ExitProtocol()
 {
     for (int i = 0; i < j.Count; ++i)
     {
         j[i].Detach();
     }
     HIDapi.hid_exit();
 }
Пример #7
0
        /// \brief Attempts to recieve RAW DATA to the given bluetooth HID device.  This is essentially a wrapper around HIDApi.
        /// \param hidapi_wiimote The HIDApi device handle to write to.
        /// \param buf The data to write.
        /// \sa Wiimote::ReadWiimoteData()
        /// \warning DO NOT use this unless you absolutely need to bypass the given Wiimote communication functions.
        ///          Use the functionality provided by Wiimote instead.
        public static int RecieveRaw(IntPtr hidapi_wiimote, byte[] buf)
        {
            if (hidapi_wiimote == IntPtr.Zero) return -2;

            HIDapi.hid_set_nonblocking(hidapi_wiimote, 1);
            int res = HIDapi.hid_read(hidapi_wiimote, buf, new UIntPtr(Convert.ToUInt32(buf.Length)));

            return res;
        }
Пример #8
0
 void hid_write(byte[] buf)
 {
     buf[0] = hid_timestamp;
     for (int i = 0; i < rawhidTxSize; ++i)
     {
         send_buffer_[i + 1] = buf[i];
     }
     HIDapi.hid_write(handle, send_buffer_, new UIntPtr(rawhidTxSize + 1));
 }
Пример #9
0
        /// \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 != null)
            {
                if (remote.hidapi_handle != IntPtr.Zero)
                    HIDapi.hid_close(remote.hidapi_handle);

                Wiimotes.Remove(remote);
            }
        }
Пример #10
0
    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);
    }
Пример #11
0
        public static void Cleanup(DS4 remote)
        {
            if (remote != null)
            {
                if (remote.hidapi_handle != IntPtr.Zero)
                {
                    HIDapi.hid_close(remote.hidapi_handle);
                }

                Controllers.Remove(remote);
            }
        }
Пример #12
0
        public static int RecieveRaw(IntPtr hidapi_handle, byte[] buf)
        {
            if (hidapi_handle == IntPtr.Zero)
            {
                return(-2);
            }

            HIDapi.hid_set_nonblocking(hidapi_handle, 1);
            int res = HIDapi.hid_read(hidapi_handle, buf, new UIntPtr(Convert.ToUInt32(buf.Length)));

            return(res);
        }
Пример #13
0
    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);
    }
Пример #14
0
    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();
    }
Пример #15
0
    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);
    }
Пример #16
0
    int hid_read()
    {
        int res;

        byte[] r = new byte[rawhidRxSize];
        res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(rawhidRxSize), HID_TIMEOUT_UNITY);
        if (res > 0)
        {
            lock (rec_buffer)
            {
                rec_buffer = r;
            }
        }
        return(res);
    }
Пример #17
0
 private static void SendThread()
 {
     while (true)
     {
         lock (WriteQueue)
         {
             if (WriteQueue.Count != 0)
             {
                 WriteQueueData wqd = WriteQueue.Dequeue();
                 int res = HIDapi.hid_write(wqd.pointer, wqd.data, new UIntPtr(Convert.ToUInt32(wqd.data.Length)));
                 if (res == -1) Debug.LogError("HidAPI reports error " + res + " on write: " + Marshal.PtrToStringUni(HIDapi.hid_error(wqd.pointer)));
                 else if (Debug_Messages) Debug.Log("Sent " + res + "b: [" + wqd.data[0].ToString("X").PadLeft(2, '0') + "] " + BitConverter.ToString(wqd.data, 1));
             }
         }
         Thread.Sleep(MaxWriteFrequency);
     }
 }
Пример #18
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));
 }
Пример #19
0
    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);
    }
Пример #20
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;
 }
Пример #21
0
    private int ReceiveRaw()
    {
        if (handle == IntPtr.Zero)
        {
            return(-2);
        }

        HIDapi.hid_set_nonblocking(handle, 0);
        byte[] rawBuf = new byte[ReportLen];
        int    ret    = HIDapi.hid_read(handle, rawBuf, new UIntPtr(ReportLen));

        if (ret > 0)
        {
            lock (reports)
            {
                reports.Enqueue(new Report(rawBuf, DateTime.Now));
            }
        }
        return(ret);
    }
Пример #22
0
    private int ReceiveRaw()
    {
        if (handle == IntPtr.Zero)
        {
            return(-2);
        }
        HIDapi.hid_set_nonblocking(handle, 0);
        byte[] raw_buf = new byte[report_len];
        int    ret     = HIDapi.hid_read(handle, raw_buf, new UIntPtr(report_len));

        if (ret > 0)
        {
            lock (reports)
            {
                reports.Enqueue(new Report(raw_buf, System.DateTime.Now));
            }
            ts_en = raw_buf[1];
        }
        return(ret);
    }
Пример #23
0
    public byte[] Send(IntPtr handle)
    {
        var data = this.GetByteData();

        HIDapi.hid_write(handle, data, new UIntPtr((uint)data.Length));
        //Debug.Log("Send Data: " + Helper.ByteArrayToString(data));

        if (GlobalCount == 0xF)
        {
            GlobalCount = 0;
        }
        else
        {
            GlobalCount++;
        }

        var responseData = new byte[ReportLen];
        int result       = 0;
        int count        = 0;

        while (result != 1)
        {
            result = HIDapi.hid_read_timeout(handle, responseData, new UIntPtr((uint)ReportLen), 50);

            // TODO: Check error type
            count++;
            if (count > 8)
            {
                break;
            }
        }
        if (result < 1)
        {
            //Debug.Log("No response.");
        }
        else
        {
            //Debug.Log("Recieve Data: " + Helper.ByteArrayToString(responseData));
        }
        return(responseData);
    }
Пример #24
0
    public int ReadData(ref byte[] packet)
    {
        if (omniDisconnected)   //if disconnected, don't do anything
        {
            return(-2);
        }

        if (hidapi_handle == IntPtr.Zero)
        {
            return(-2);
        }

        Array.Clear(inputBuf, 0, 65);

        HIDapi.hid_read(hidapi_handle, inputBuf, new UIntPtr(Convert.ToUInt32(inputBuf.Length)));


        if (inputBuf[0] != 0xEF) //Checking for invalid message
        {
            Debug.LogError(System.DateTime.Now.ToLongTimeString() + ": OmniManager(ReadData) - The Omni has been diconnected and is no longer being detected.");
            omniDisconnected = true;
            Cleanup();
        }


        //if (inputBuf[1] == 26)
        if (inputBuf[2] == 156) // hex 9C
        {
            packet = new byte[inputBuf[1]];
            Buffer.BlockCopy(inputBuf, 0, packet, 0, inputBuf[1]);
        }
        else
        {
            return(-2); //pods off or write data
        }


        //Debug.Log(inputBuf[2].ToString());

        return(inputBuf[1]);
    }
Пример #25
0
    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 rwPage(IntPtr handle, byte address, byte[] s, byte[] r)
        {
            int res = 0;

            s[0] = 0;
            s[1] = address;
            res  = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen));
            if (res < 1)
            {
                return(-1);
            }
            res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000);
            if (res < 1)
            {
                return(-2);
            }
            if (r[1] != s[1])
            {
                return(-3);
            }
            return(0);
        }
Пример #27
0
 void Start()
 {
     dummy_buf = new byte[rawhidTxSize];
     for (int i = 0; i < rawhidTxSize; ++i)
     {
         dummy_buf[i] = 0;
     }
     dummy_buf[1]    = (byte)Command.NULL;
     hid_timestamp   = 2;
     setup_buffer    = new byte[rawhidTxSize];
     setup_buffer[1] = (byte)Command.SETUP;
     setup_buffer[2] = RECV_TICKRATE_MS;
     setup_buffer[3] = RADIO_TIMEOUT;
     setup_buffer[4] = HID_TIMEOUT;
     setup_buffer[5] = WAND_TICKRATE_MS;
     rec_buffer      = new byte[rawhidRxSize];
     HIDapi.hid_init();
     connected       = TryConnect();
     send_buffer_    = new byte[rawhidTxSize + 1];
     send_buffer_[0] = 0;
     StartCoroutine("TransmitLoop");
 }
Пример #28
0
    public int SendData(byte[] data)
    {
        if (hidapi_handle == IntPtr.Zero)
        {
            return(-2);
        }

        if (data.Length > 64)
        {
            return(-1);
        }

        Array.Clear(outputBuf, 0, 65);

        outputBuf[0] = (byte)0x00; // Maybe change

        Buffer.BlockCopy(data, 0, outputBuf, 1, data.Length);

        int res = 0;

        res = HIDapi.hid_write(hidapi_handle, outputBuf, new UIntPtr(Convert.ToUInt32(outputBuf.Length)));

        return(res);
    }
        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);
        }
Пример #30
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);
    }