WIIMOTELIB
Nintendo WBB and Nintendo Wii Remote are the controllers for Nintendo Wii system. The controllers for the Wii system communicate via Bluetooth. Because of that, we can leverage these controllers for our game as they communicate via the Bluetooth protocol.
When the WBB is paired with the PC, it will be identified as a human interface compliant device (HID). Hence to connect to the device, we must use the HID and Device Management Win32 APIs. Unfortunately, there is no built-in support for these APIs in the current .NET runtime. But theses APIs are defined in the Windows Driver Kit (WDK). P/Invoke allows to directly call the methods of the Win32 API from .NET. The WiimoteLib library in our game code provides these calls to the Windows Driver. The process to begin communicating with the WBB is as follows:
1. Get the GUID of the HID class defined by Windows
2. Get a handle to the list of all the devices which are part of the HID class
3. Walk through those devices and get detailed information about each
4. Compare the product ID and the vendor ID of each device to the known WBB
product ID and the vendor ID
5. If found, create a FileStream to read or write to the device
6. Clear up the device list
WiimoteLib for using with a Nintendo WBB is most famous library on the web. It has good reviews by the users who express love for this library. The fact that the library is stable, free, easy to use and embed, gave us reason to use the library in our game code as oppose to writing one by ourselves. Out of the box the library provides the following namespace:
namespace WiimoteLib
{
public class Wiimote : IDisposable
{
public Wiimote();
public string HIDDevicePath { get; }
public Guid ID { get; }
public WiimoteState WiimoteState { get; }
public event EventHandler<WiimoteChangedEventArgs> WiimoteChanged;
public event EventHandler<WiimoteExtensionChangedEventArgs>
WiimoteExtensionChanged;
public void Connect();
public void Disconnect();
public void Dispose();
protected virtual void Dispose(bool disposing);
public void GetStatus();
public byte[] ReadData(int address, short size);
public void SetLEDs(int leds);
public void SetLEDs(bool led1, bool led2, bool led3, bool led4);
public void SetReportType(InputReport type, bool continuous);
public void SetReportType(InputReport type, IRSensitivity irSensitivity, bool
continuous);
public void SetRumble(bool on);
public void WriteData(int address, byte data);
public void WriteData(int address, byte size, byte[] buff);
}
}
To start with, we need to initiate the Bluetooth connection between the PC and the WBB. The following code accomplishes the connection in BalanceBoard.cs Wiimote balanceBoard = new Wiimote();
balanceBoard.Connect();
After the connection is successful, we set the light emitting diode (LED) on the balance board to indicate the connection has been established. The following code sets the LED in BalanceBoard.cs balanceBoard.SetLEDs(1);
Our code interacts with the balance board to get the coordinates of the center of gravity. The following code fetches the X and Y coordinates from the balance board in
update methods of Level1.cs, Level2.cs and Level3.cs
WiimoteState wiiState = BalanceBoard.getBalanceBoard().WiimoteState;
BalanceBoardState balanceBoard = wiiState.BalanceBoardState;
xCoordinate = balanceBoard.CenterOfGravity.X;
yCoordinate = balanceBoard.CenterOfGravity.Y;
The game uses the above X and Y coordinates from the WBB to act as the controller during the game play. The Bluetooth connection to the WBB is automatically dropped off when the game finishes.
The author of the WiimoteLib library maintains a list of .NET applications that use WiimoteLib. Our rehab game software has been recognized and listed in the list of .NET based wiimote applications