// Requests the Wiimote to report data from its internal registers. // type: The type of register we would like to read from // offset: The starting offset of the block of data we would like to read // size: The size of the block of data we would like to read // Responder: This will be called when the Wiimote finishes reporting the requested data. public int SendRegisterReadRequest(RegisterType type, int offset, int size, ReadResponder Responder) { if (CurrentReadData != null) { Debug.LogWarning("Aborting read request; There is already a read request pending!"); return(-2); } CurrentReadData = new CS_RegisterReadData(offset, size, Responder); byte address_select = (byte)type; byte[] offsetArr = IntToBigEndian(offset, 3); byte[] sizeArr = IntToBigEndian(size, 2); byte[] total = new byte[] { address_select, offsetArr[0], offsetArr[1], offsetArr[2], sizeArr[0], sizeArr[1] }; return(SendWithType(OutputDataType.READ_MEMORY_REGISTERS, total)); }
// 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); }