WiiLAB - WiiLAB
Annotations
This is the wrapper class for the WiimoteLib that exposes the library as a
COM interop.
All of the global functions listed in the previous section use the methods and variables of this Wiimote class to perform their respective tasks. The following links will go over each section of the code in greater detail.
Raw Source Code
The source code is also available as a .cs file download at the bottom of the page. A zip file of the project solution is also attached.
/*********************************************************************************
* WiiLAB.cs
* Wrapper class for WiimoteLib.cs
* Written by Jordan Brindza and Jessica Szweda (University of Notre Dame)
* * WiimoteLib.cs written by Brian Peek (http://www.brianpeek.com/)
*********************************************************************************/
#region Imports
using System;
using System.Runtime.InteropServices;
using WiimoteLib;
using System.Collections.Generic;
#endregion
namespace WiiLAB
{
#region Interface
/// <summary>
/// Interface for the COM class/lib
/// </summary>
public interface WiiLABSignature
{
int Connect();
int CheckConnection();
bool Disconnect();
void DisconnectAll();
//wiimote functions
void SetLEDsInt(int leds);
void SetLEDsBool(bool led1, bool led2, bool led3, bool led4);
void SetRumble(bool on);
bool[] GetButtonState();
double[] GetAccelState();
double[,] GetIRState();
double GetBatteryState();
//nunchuk functions
bool[] GetNunchukButtonState();
double[] GetNunchukAccelState();
double[] GetNunchukJoystickState();
}
#endregion
#region Class Definition
/// <summary>
/// Wrapper class for the WiimoteLib
/// COM class used to interface with Matlab
/// </summary>
[ClassInterface(ClassInterfaceType.AutoDual)]
public class WiiLAB : WiiLABSignature
{
#region Private Variables
private static WiimoteCollection wc;
private static List<Guid> connectedWiimotes = new List<Guid>();
private Wiimote wm;
#endregion
#region Constructor
/// <summary>
/// Default Constructor
/// </summary>
public WiiLAB()
{
wm = new Wiimote();
}
#endregion
#region Connect/Disconnect
#region Connect
/// <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)
{
//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;
}
#endregion
#region Check Connection
/// <summary>
/// Checks the Wiimotes Connection
/// </summary>
/// <returns>integer indicating the state of the Connection
/// (1 = connected, 0 = not connected, -1 = read error occured) </returns>
public int CheckConnection()
{
return (int)wm.ConnectionState;
}
#endregion
#region Set Report
/// <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.IRExtensionAccel, true);
return 1;
case ExtensionType.None:
wm.SetReportType(InputReport.IRAccel, true);
return 0;
}
return -1;
}
#endregion
#region Disconnect
/// <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;
}
}
/// <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)
{
wm.Disconnect();
}
}
//Clear the connected Wiimotes ID list
connectedWiimotes = new List<Guid>();
wc = null;
}
#endregion
#endregion
#region Wiimote Functions
#region Feedback
#region LEDs
/// <summary>
/// Calls WiimoteLib's SetLEDs(int leds)
/// For specific wiimote int mappings of the LED refer to WiiBrew wiki
/// It is reccomended to use SetLEDsBool
/// </summary>
/// <param name="leds">int instructing wiimote to set indicated LEDs based on the binary representation</param>
public void SetLEDsInt(int leds)
{
wm.SetLEDs(leds);
}
/// <summary>
/// Calls WiimoteLib's SetLEDs(bool led1, bool led2, bool led3, bool led4)
/// I could not get overloaded functions to work in Matlab so i changed the name slightly
/// </summary>
/// <param name="led1">if true :: turns on LED 1</param>
/// <param name="led2">if true :: turns on LED 2</param>
/// <param name="led3">if true :: turns on LED 3</param>
/// <param name="led4">if true :: turns on LED 4</param>
public void SetLEDsBool(bool led1, bool led2, bool led3, bool led4)
{
wm.SetLEDs(led1, led2, led3, led4);
}
#endregion
#region Rumble
/// <summary>
/// Calls WiimoteLib's SetRumble(bool on)
/// </summary>
/// <param name="on">if true :: turns on rumble</param>
public void SetRumble(bool on)
{
wm.SetRumble(on);
}
#endregion
#endregion
#region State
#region Buttons
/// <summary>
/// Retrieves the button status from the Wiimote
/// A bool array of the buttons is returned.
/// [A B Up Right Down Left Plus Minus Home One Two]
/// </summary>
/// <returns>True if the button is pressed</returns>
public bool[] GetButtonState()
{
//init array
bool[] state = new bool[11];
//set button status'
state[0] = wm.WiimoteState.ButtonState.A;
state[1] = wm.WiimoteState.ButtonState.B;
state[2] = wm.WiimoteState.ButtonState.Up;
state[3] = wm.WiimoteState.ButtonState.Right;
state[4] = wm.WiimoteState.ButtonState.Down;
state[5] = wm.WiimoteState.ButtonState.Left;
state[6] = wm.WiimoteState.ButtonState.Plus;
state[7] = wm.WiimoteState.ButtonState.Minus;
state[8] = wm.WiimoteState.ButtonState.Home;
state[9] = wm.WiimoteState.ButtonState.One;
state[10] = wm.WiimoteState.ButtonState.Two;
return state;
}
#endregion
#region Accel
/// <summary>
/// Retrieves the Acceleration values from the Wiimote
/// Multiplies the normalized values by 9.8 to get values in m/s
/// [X Y Z]
/// </summary>
/// <param name="round">Number of decimals places to round</param>
/// <returns>Double array of acceleration values</returns>
public double[] GetAccelState()
{
//init array
double[] state = new double[3];
//calc values
state[0] = (double)(wm.WiimoteState.AccelState.Values.X);
state[1] = (double)(wm.WiimoteState.AccelState.Values.Y);
state[2] = (double)(wm.WiimoteState.AccelState.Values.Z);
return state;
}
#endregion
#region IR
/// <summary>
/// Returns a 2-dimensional double array of the IR values of the Wiimote
/// [ found_s1 X Y ]
/// [ found_s2 X Y ]
/// [ found_s3 X Y ]
/// [ found_s4 X Y ]
/// [ found_mid X Y ]
/// </summary>
/// <returns>2-dimensional double array of the IR values</returns>
public double[,] GetIRState()
{
//init array
double[,] state = new double[5, 3];
//set IR status
state[0, 0] = (double)(wm.WiimoteState.IRState.IRSensors[0].Found ? 1 : 0);
state[0, 1] = (double)(wm.WiimoteState.IRState.IRSensors[0].Position.X);
state[0, 2] = (double)(wm.WiimoteState.IRState.IRSensors[0].Position.Y);
state[1, 0] = (double)(wm.WiimoteState.IRState.IRSensors[1].Found ? 1 : 0);
state[1, 1] = (double)(wm.WiimoteState.IRState.IRSensors[1].Position.X);
state[1, 2] = (double)(wm.WiimoteState.IRState.IRSensors[1].Position.Y);
state[2, 0] = (double)(wm.WiimoteState.IRState.IRSensors[2].Found ? 1 : 0);
state[2, 1] = (double)(wm.WiimoteState.IRState.IRSensors[2].Position.X);
state[2, 2] = (double)(wm.WiimoteState.IRState.IRSensors[2].Position.Y);
state[3, 0] = (double)(wm.WiimoteState.IRState.IRSensors[3].Found ? 1 : 0);
state[3, 1] = (double)(wm.WiimoteState.IRState.IRSensors[3].Position.X);
state[3, 2] = (double)(wm.WiimoteState.IRState.IRSensors[3].Position.Y);
state[4, 0] = (double)(wm.WiimoteState.IRState.Midpoint.Found ? 1 : 0);
state[4, 1] = (double)(wm.WiimoteState.IRState.Midpoint.Position.X);
state[4, 2] = (double)(wm.WiimoteState.IRState.Midpoint.Position.Y);
return state;
}
#endregion
#region Battery
/// <summary>
/// Returns the Battery level of the Wiimote
/// </summary>
/// <returns>A double from 0-100 representing the percentage of remaining Battery</returns>
public double GetBatteryState()
{
//convert the raw battery data to percent
float battery = (((100.0f * 48.0f * (float)(wm.WiimoteState.Battery / 48.0f))) / 192.0f);
return battery;
}
#endregion
#endregion
#endregion
#region Nunchuk Functions
#region Buttons
/// <summary>
/// Retrieves the button status from the Nunchuk
/// A bool array of the buttons is returned.
/// [C Z]
/// </summary>
/// <returns>True if the button is pressed</returns>
public bool[] GetNunchukButtonState()
{
//init array
bool[] state = new bool[2];
state[0] = wm.WiimoteState.NunchukState.C;
state[1] = wm.WiimoteState.NunchukState.Z;
return state;
}
#endregion
#region Accel
/// <summary>
/// Retrieves the Acceleration values from the Nunchuk
/// Multiplies the normalized values by 9.8 to get values in m/s
/// [X Y Z]
/// </summary>
/// <param name="round">Number of decimals places to round</param>
/// <returns>Double array of acceleration values</returns>
public double[] GetNunchukAccelState()
{
//init array
double[] state = new double[3];
state[0] = (double)(wm.WiimoteState.NunchukState.AccelState.Values.X);
state[1] = (double)(wm.WiimoteState.NunchukState.AccelState.Values.Y);
state[2] = (double)(wm.WiimoteState.NunchukState.AccelState.Values.Z);
return state;
}
#endregion
#region Joystick
/// <summary>
/// Returns a double array of the joystick values of the Nunchuk
/// [X Y]
/// </summary>
/// <returns>Double array of the joystick values</returns>
public double[] GetNunchukJoystickState()
{
//init array
double[] state = new double[2];
state[0] = (double)(wm.WiimoteState.NunchukState.Joystick.X);
state[1] = (double)(wm.WiimoteState.NunchukState.Joystick.Y);
return state;
}
#endregion
#endregion
#region Events
/// <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();
}
#endregion
}
#endregion
}
--
JordanBrindza - 30 Jun 2008