WiiLab Example Code - WiiLabCOM: Connect/Disconnect
Connect
One of the main problems we encountered when attempting to incorporate MATLAB with the WiimoteLib was the limited data types allowed in Matlab. We were unable to consistently and reliably convert C# objects to MATLAB objects or structs. Therefore, throughout the
WiimoteCOM? wrapper we used only integer, double, string, and array return types. Thus, many of the functions return a range of integers, each of which corresponds to a return message. The Connect method is one of these methods.
The method begins by iterating through all the Wiimotes paired with the computer and looks for one that is not connected. The Wiimote is then connected, a reference to the Wiimote is added to the connectedWiimotes variable, and the corresponding LEDs on the Wiimote are set.
The method will return an integer from -1 to 2+. Based on the integer returned you can determine what the method succeeded in doing and what it was unable to do.
- -1 - Indicates that there are no Wiimotes paired with this computer.
- 0 - Indicates that there are Wiimotes paired with the computer but they are all in use. You will have to disconnect one before you can connect.
- 1 - Indicates the Wiimote was successfully connected.
- 2+ - (Anything greater than 1) Indicates the Wiimote was successfully connected. The number is a sort of ID for the Wiimote. The first Wiimote connected will return 1. The next Wiimote that is connected will return 2, and so on.
/// <summary>
/// Connects to the first free Wiimote on the Bluetooth HID List
/// </summary>
/// <returns>
/// The number associated with the wiimote (1,2,3,4)
/// 0 - no new wiimote is available on the HID list
/// -1 - There are no wiimotes connected to the computer
/// </returns>
public int Connect()
{
//Find all wiimotes connected to the computer
if (wc == null)
{
wc = new WiimoteCollection();
try
{
wc.FindAllWiimotes();
}
catch (WiimoteNotFoundException we)
{
//there are no Wiimotes on the HID list
return -1;
}
}
//For each wiimote that is found
foreach (Wiimote tempWM in wc)
{
//If we haven't already established a connection to this wiimote
//Set up the connection
if (!connectedWiimotes.Contains(tempWM.ID))
{
//Connect the Wiimote
wm = tempWM;
wm.Connect();
//Set initial report type
wm.SetReportType(InputReport.IRAccel, true);
wm.WiimoteExtensionChanged += new EventHandler<WiimoteExtensionChangedEventArgs>(wm_WiimoteExtensionChanged);
//Add the wiimote ID to the connected wiimotes list
connectedWiimotes.Add(wm.ID);
SetReportType();
//Set the LEDs accordingly
switch (connectedWiimotes.Count)
{
case 1:
wm.SetLEDs(true, false, false, false);
return 1;
case 2:
wm.SetLEDs(false, true, false, false);
return 2;
case 3:
wm.SetLEDs(false, false, true, false);
return 3;
case 4:
wm.SetLEDs(false, false, false, true);
return 4;
default:
wm.SetLEDs(connectedWiimotes.Count);
return connectedWiimotes.Count;
}
}
}
if (wc.Count == 0)
//There are no wiimotes paired with to the computer
return -1;
else
//There are no available wiimotes
return 0;
}
Disconnect
Disconnect
Disconnect is the method that will disconnect the single Wiimote that is the private variable for this instance of the class. It disconnects the Wiimote and removes it from the connectedWiimotes variable to allow it to be connected in the future.
/// <summary>
/// Disconnects a Connected wiimote
/// </summary>
/// <returns>
/// True if a wiimote was successfully disconnected
/// </returns>
public bool Disconnect()
{
try
{
//Remove the wiimote ID from the connected list
connectedWiimotes.Remove(wm.ID);
//Disconnect the wiimote
wm.Disconnect();
return true;
}
catch
{
return false;
}
}
Disconnect All
Disconnect All will disconnect all the Wiimotes that are paired with this computer. This method was included out of necessity because of the way we keep track of which Wiimotes are connected. If a Wiimote is not properly disconnected, then you would be unable to connect to that Wiimote again. This method thus allows anyone to free up all the Wiimotes paired with the computer.
/// <summary>
/// Iterates through all connected wiimotes and disconnects them
/// </summary>
public void DisconnectAll()
{
if (wc != null)
{
//iterate through all wiimotes and disconnect each one
foreach (Wiimote tempWM in wc)
{
try
{
wm.Disconnect();
}
catch { /*Do Nothing*/ }
}
}
//Clear the connected Wiimotes ID list
connectedWiimotes = new List<Guid>();
}
Set Report Type
The report type of the Wiimote dictates the information that the Wiimote sends to the computer and the format in which that data is sent. There are a number of report types available for the Wiimote. Below are a few of the more common ones:
- Buttons: Returns the state of the Buttons of the Wiimote
- ButtonsExtension: Returns the state of the Buttons of the Wiimote and the Buttons and Acceleration, if applicable, of the Extension (if attached)
- ButtonsAccel: Returns the state of the Buttons and Acceleration of the Wiimote
- ExtensionAccel: Returns the state of the Buttons and Acceleration of the Wiimote and the state of the Buttons and Acceleration, if applicable, of the Extension (if attached)
- IRAccel: Returns the state of the Buttons, Acceleration, and IR camera of the Wiimote
- IRExtensionAccel: Returns the state of the Buttons, Acceleration, and IR camera of the Wiimote, along with the Buttons and Acceleration, if applicable, of the Extension (if attached)
The amount of detail in a report sent by the Wiimote directly affects the amount of energy (battery power) the Wiimote consumes. Therefore it is not a good idea to set the most detailed report type if you are not using all the information.
In our case, the report is set automatically when you connect a Wiimote. An event is added to the WiimoteExtensionChanged listener of the Wiimote object that calls the SetReportType method. The only two reports we use are IRAccel and IRExtensionAccel so when there is no extension it is set to IRAccel, and when there is an extension it is set to IRExtensionAccel.
/// <summary>
/// Sets the report type
/// Only initializes the main wiimote report if no extension is connected
/// </summary>
/// <returns>
/// Int based on the report set
/// 1 - InputReport.ExtensionAccel
/// 0 - InputReport.ButtonsAccel
/// -1 - error
/// </returns>
public int SetReportType()
{
switch (wm.WiimoteState.ExtensionType)
{
case ExtensionType.ClassicController:
case ExtensionType.Guitar:
case ExtensionType.Nunchuk:
wm.SetReportType(InputReport.ExtensionAccel, true);
return 1;
case ExtensionType.None:
wm.SetReportType(InputReport.IRAccel, true);
return 0;
}
//error
return -1;
}
/// <summary>
/// Called when the Wiimote Extension is changed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs e)
{
SetReportType();
}
--
JordanBrindza - 02 Jul 2008