예제 #1
0
        // Sends a generic block of data to the Wiimote using the specified OutputDataType.
        // Returns the total size of the data written, -1 if HIDApi reports an error, or < -1 if there is an invalid input.
        public int SendWithType(OutputDataType type, byte[] data)
        {
            byte[] final = new byte[data.Length + 1];
            final[0] = (byte)type;

            for (int x = 0; x < data.Length; x++)
            {
                final[x + 1] = data[x];
            }
            //Allows the wiimote to vibrate
            if (bRumbleOn)
            {
                final[0] |= 0x01;
                final[1] |= 0x01;
            }
            else
            {
                final[0] |= 0x00;
                final[1] |= 0x00;
            }
            int res = CS_WiiMoteManager.SendRaw(hidapi_handle, final);

            if (res < -1)
            {
                Debug.LogError("Incorrect Input to HIDAPI.  No data has been sent.");
            }
            return(res);
        }
예제 #2
0
        // Reads and interprets data reported by the Wii Remote.
        // On success, > 0, < 0 on failure, 0 if nothing has been recieved.
        public int ReadWiimoteData()
        {
            byte[] buf     = new byte[22];
            int    iStatus = CS_WiiMoteManager.RecieveRaw(hidapi_handle, buf);

            if (iStatus <= 0)
            {
                return(iStatus);          // Either there is an error or nothings been received
            }
            int iTypeSize = GetInputDataTypeSize((InputDataType)buf[0]);

            byte[] data = new byte[iTypeSize];
            for (int x = 0; x < data.Length; x++)
            {
                data[x] = buf[x + 1];
            }

            if (CS_WiiMoteManager.bDebugMessages)
            {
                Debug.Log("Recieved: [" + buf[0].ToString("X").PadLeft(2, '0') + "] " + BitConverter.ToString(data));
            }
            byte[] ext = null;

            switch ((InputDataType)buf[0]) // buf[0] is the output ID byte
            {
            case InputDataType.STATUS_INFO:
                Debug.Log("Status");
                byte flags         = data[2];
                byte battery_level = data[5];

                bool bOld_Ext_Connected = Status.ext_connected;

                byte[] total = new byte[] { flags, battery_level };
                Status.InterpretData(total);

                if (Status.ext_connected != bOld_Ext_Connected)
                {
                    if (Status.ext_connected)                //Only reads the extensions if the Nunchuck is connected
                    {
                        Debug.Log("An extension has been connected.");
                        ActivateExtension();
                        RequestIdentifyExtension();         // Identifies if the extension was a Nunchuck or not
                    }
                    else
                    {
                        _current_ext = ExtensionController.NONE;
                        Debug.Log("An extension has been disconnected.");
                    }
                }
                break;

            case InputDataType.READ_MEMORY_REGISTERS:

                if (CurrentReadData == null)
                {
                    Debug.LogWarning("Recived Register Read Report when none was expected.  Ignoring.");
                    return(iStatus);
                }

                byte size  = (byte)((data[2] >> 4) + 0x01);
                byte error = (byte)(data[2] & 0x0f);
                // Error 0x07 means reading from a write-only register
                if (error == 0x07)
                {
                    CurrentReadData = null;
                    return(iStatus);
                }
                // uLowOffset is reversed because the Wii Remote reports are in Big Endian order
                ushort uLowOffset = BitConverter.ToUInt16(new byte[] { data[4], data[3] }, 0);
                ushort uExpected  = (ushort)CurrentReadData.ExpectedOffset;
                if (uExpected != uLowOffset)
                {
                    Debug.LogWarning("Expected Register Read Offset (" + uExpected + ") does not match reported offset from Wii Remote (" + uLowOffset + ")");
                }
                byte[] read = new byte[size];
                for (int x = 0; x < size; x++)
                {
                    read[x] = data[x + 5];
                }

                CurrentReadData.AppendData(read);
                if (CurrentReadData.ExpectedOffset >= CurrentReadData.Offset + CurrentReadData.Size)
                {
                    CurrentReadData = null;
                }

                break;

            case InputDataType.REPORT_BUTTONS_IR10_EXT9:

                ext = new byte[9];
                for (int x = 0; x < 9; x++)
                {
                    ext[x] = data[x + 12];
                }

                if (_Extension != null)
                {
                    _Extension.InterpretData(ext);
                }
                break;

            case InputDataType.REPORT_BUTTONS_ACCEL_IR10_EXT6:
                ext = new byte[6];
                for (int x = 0; x < 6; x++)
                {
                    ext[x] = data[x + 15];
                }

                if (_Extension != null)
                {
                    _Extension.InterpretData(ext);
                }
                break;
            }

            if (ext == null)
            {
                _RawExtension = null;
            }
            else
            {
                _RawExtension = new CS_ReadOnlyArray <byte>(ext);
            }

            return(iStatus);
        }