Beispiel #1
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);
        }
Beispiel #2
0
 public CS_NunchuckData(CS_WiiMote Owner)
     : base(Owner)
 {
     _stick          = new byte[2];
     _stick_readonly = new CS_ReadOnlyArray <byte>(_stick);
 }