Skip to content

Commit

Permalink
Fixed a bug where Extensions were not initialized correctly
Browse files Browse the repository at this point in the history
- Fixed a bug where extension identification requests were sent before activation requests (the wiimote would return 0xffffff in this case)
  • Loading branch information
Flafla2 committed Sep 24, 2015
1 parent 4ec0e04 commit 8850098
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Assets/Wiimote/Scripts/Wiimote.cs
Expand Up @@ -123,6 +123,8 @@ public class Wiimote
private byte[] InterleavedDataBuffer = new byte[18];
private bool ExpectingSecondInterleavedPacket = false;

private bool ExpectingWiiMotionPlusSwitch = false;

public Wiimote(IntPtr hidapi_handle, string hidapi_path, WiimoteType Type)
{
_hidapi_handle = hidapi_handle;
Expand All @@ -135,7 +137,7 @@ public Wiimote(IntPtr hidapi_handle, string hidapi_path, WiimoteType Type)
_Status = new StatusData(this);
_Extension = null;

RequestIdentifyWiiMotionPlus(); // why not?
//RequestIdentifyWiiMotionPlus(); // why not?
}

private static byte[] ID_InactiveMotionPlus = new byte[] {0x00, 0x00, 0xA6, 0x20, 0x00, 0x05};
Expand Down Expand Up @@ -232,12 +234,6 @@ private void RespondIdentifyExtension(byte[] data)
_current_ext = ExtensionController.NONE;
_Extension = null;
}

if(current_ext != ExtensionController.MOTIONPLUS &&
current_ext != ExtensionController.MOTIONPLUS_CLASSIC &&
current_ext != ExtensionController.MOTIONPLUS_NUNCHUCK &&
current_ext != ExtensionController.WIIU_PRO)
ActivateExtension();
}

#region Setups
Expand Down Expand Up @@ -354,15 +350,20 @@ public bool ActivateWiiMotionPlus()
res = SendRegisterWriteRequest(RegisterType.CONTROL, 0xA600FE, new byte[] { 0x04 });
if (res < 0) return false;

_current_ext = ExtensionController.MOTIONPLUS;
if (_Extension == null || _Extension.GetType() != typeof(MotionPlusData))
_Extension = new MotionPlusData(this);
ExpectingWiiMotionPlusSwitch = true;

return true;
}

public bool DeactivateWiiMotionPlus()
{
if (current_ext != ExtensionController.MOTIONPLUS && current_ext != ExtensionController.MOTIONPLUS_CLASSIC && current_ext != ExtensionController.MOTIONPLUS_NUNCHUCK)
Debug.LogWarning("There is a request to deactivate the Wii Motion Plus even though it has not been activated! Trying anyway.");

return SendRegisterWriteRequest(RegisterType.CONTROL, 0xA400F0, new byte[] { 0x55 }) > 0;
int res = SendRegisterWriteRequest(RegisterType.CONTROL, 0xA400F0, new byte[] { 0x55 });
return res > 0;
}

/// \brief Attempts to activate any connected extension controller
Expand Down Expand Up @@ -628,11 +629,18 @@ public int ReadWiimoteData()
if (Status.ext_connected) // The Wii Remote doesn't allow reading from the extension identifier
{ // when nothing is connected.
Debug.Log("An extension has been connected.");
RequestIdentifyExtension(); // Identify what extension was connected.
if (current_ext != ExtensionController.MOTIONPLUS)
{
ActivateExtension();
RequestIdentifyExtension(); // Identify what extension was connected.
}
else
ExpectingWiiMotionPlusSwitch = false;
}
else
{
_current_ext = ExtensionController.NONE;
if (!ExpectingWiiMotionPlusSwitch)
_current_ext = ExtensionController.NONE;
Debug.Log("An extension has been disconnected.");
}
}
Expand Down

0 comments on commit 8850098

Please sign in to comment.