WiiLab Example Code - WiiLabCOM: Wiimote State
Buttons
The method returns an boolean array that represents the Wiimote's buttons state. Each index in the array corresponds to one of the buttons on the Wiimote. The value of each index is true if that button is pressed and false if it is not.
The array indices are as follows:
0 1 2 3 4 5 6 7 8 9 10
[ A B UP RIGHT DOWN LEFT PLUS MINUS HOME ONE TWO ]
/// <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;
}
Acceleration
GetAccelState simply accesses the Wiimote's acceleration values and returns them in a double array. The actual values retrieved from the Wiimote are normalized values between -1 and 1. We multiply the values by 9.8 before returning them so that the values are in meters per second which is much more familiar to students.
The array indices are as follows:
0 1 2
[ X_Accel Y_Accel Z_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 * 9.8);
state[1] = (double)(wm.WiimoteState.AccelState.Values.Y * 9.8);
state[2] = (double)(wm.WiimoteState.AccelState.Values.Z * 9.8);
return state;
}
IR
GetIRState accesses the Wiimote's IR camera values and returns them in a two-dimensional double array. There is a lot of information contained in the array. The first entry of each row indicates whether that IR source was detected. The next two entries of each row are the X and Y values of the IR source respectively. The last row (5th) contains the value of the Midpoint. The Wiimote is generally used with a sensor bar that consists of two IR sources. The Midpoint is the midpoint between the first two IR sources. The first entry of the row indicates if the Midpoint exists and the next two entries are the X and Y values of the Midpoint.
The array indices are as follows:
0 1 2
0 [ isFound_Source1 X_s1 Y_s1 ]
1 [ isFound_Source2 X_s2 Y_s2 ]
2 [ isFound_Source3 X_s3 Y_s3 ]
3 [ isFound_Source4 X_s4 Y_s4 ]
4 [ isFound_Midpoint X_mid Y_mid ]
/// <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;
}
Battery
GetBatteryState returns a percentage representing the amount of battery power left in the Wiimote.
/// <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;
}
--
JordanBrindza - 14 Jul 2008