dyadica.net

Fun and Games in the Dyadic Sea

The Step Event

This post has been updated to include the Step ON and Step OFF calls

Pending Review – Check Back Soon!

In this Instruction we are going to modify WiimoteLib by including a new event trigger that is activated upon a change in either Quad or Zone value.

This is a very useful addition that allows for “singular” triggering so that you don’t have to rely on “flag checks” within the standard WiimoteState loop and thus allowing the Libary to do all the work!

Before we start, this instruction assumes you have previously completed the Areas Quad and Zone Instruction.

Start off by opening up the Events.cs file and add the following code below the public class WiimoteChangedEventArgs:

/// <summary>
/// Argument sent through the BalanceBoardStepEvent
/// </summary>
public class BalanceBoardStepEventArgs : EventArgs
{
/// <summary>
/// The current state of the Balance Board
/// </summary>
public BalanceBoardState BalanceBoard;

/// <summary>
/// The Type of trigger that activated the event
/// </summary>
public StepType StepType;

/// <summary>
/// Constructor
/// </summary>
/// <param name=”bb”>Wiimote Balance Board State</param>
/// <param name=”type”>Step trigger type: Standard/Damped Quad/Zone</param>
public BalanceBoardStepEventArgs(BalanceBoardState bb, StepType type)
{
BalanceBoard = bb;
StepType = type;
}
}

What we have done here is create a new class that provides access to two arguments:

  1. BalanceBoardState
  2. StepType

By passing BalanceBoardState we are provided direct access to all the relevant Balance Board calls that we would normally have via WiimoteState. The StepType argument provides us with a reference to the trigger that we can then use to filter/output a desired response etc.

To define StepType open up DataTypes.cs and add the following enum block below the BalanceBoardState struct:

/// <summary>
/// The type of Step Triggered
/// </summary>
[DataContract]
public enum StepType
{
/// <summary>
/// Zone Change
/// </summary>
Zone = 0,
/// <summary>
/// Quad Change
/// </summary>
Quad = 1,
};

Next we need to add this enum to the BalanceBoardState struct itself. We do this as follows:

/// <summary>
/// The Action which has Triggered the Step Event
/// </summary>
[DataMember]
public StepType StepType;

With this done we now have all the foundations needed for the “Event”, now we need to add it! Open up Wiimote.cs and add the following block at the top just below the equivalent WiimoteChanged and WiimoteExtensionChanged handles.

/// <summary>
/// Event raised when a Balance Board Step is Triggered
/// </summary>
public event EventHandler<BalanceBoardStepEventArgs> BalanceBoardStep;

This defines our brand new BalanceBoardStep handle. With this in place we now need to be able to trigger it.  As we are going to use either a change in Zone or Quad location to trigger the call.

Update the Area struct (DataTypes.cs) to include two additional definitions as shown below:

/// <summary>
/// Last Balance Board Zone Area
/// </summary>
[DataMember]
public string LastZone;

/// <summary>
/// Last Balance Board Quad Area
/// </summary>
[DataMember]
public string LastQuad;

These 2 new definitions simply provide us with a location to archive the states of both Zone and Quad. I chose to add them like this as there is likely to be other occasions when access to this information would provide useful! Also facility is provided for standard WiimoteState access (just in case).

With this complete we now need to add the code to facilitate the trigger. Still within Wiimote.cs add the following two blocks just below the Area.Quad and Area.Zone updates within ParseExtension:

#region step filtering quad

// calculate if there is a Quad Step
if (mWiimoteState.BalanceBoardState.Area.CurrentQuad != mWiimoteState.BalanceBoardState.Area.LastQuad)
{
// update the archived Quad
mWiimoteState.BalanceBoardState.Area.LastQuad = mWiimoteState.BalanceBoardState.Area.CurrentQuad;
// post a Standard Step event
if (BalanceBoardStep != null)
{ BalanceBoardStep(this, new BalanceBoardStepEventArgs(mWiimoteState.BalanceBoardState, StepType.Quad)); }
}

#endregion step filtering quad

#region step filtering zone

// calculate if there is a Zone Step
if (mWiimoteState.BalanceBoardState.Area.CurrentZone != mWiimoteState.BalanceBoardState.Area.LastZone)
{
// update the archived Zone
mWiimoteState.BalanceBoardState.Area.LastZone = mWiimoteState.BalanceBoardState.Area.CurrentZone;
// post a Standard Step event
if (BalanceBoardStep != null)
{ BalanceBoardStep(this, new BalanceBoardStepEventArgs(mWiimoteState.BalanceBoardState, StepType.Zone)); }
}

#endregion step filtering zone

All we are doing here is comparing states. If there is a change we then call the Event and also update the archived state for the next comparison.

With this done our new BalanceBoardStep Event is now complete.. well nearly!

So how do we use it? The easy answer is in exactly the same way you would both the UpdateWiimoteChanged and UpdateExtensionChanged Events provided in the Wiimote Test Example that comes with WiimoteLib.

First within WiimoteInfo.cs create a new delegate referance below the 2 existing ones for Wiimote State/Extension:

private delegate void UpdateBalanceBoardStepDelegate(BalanceBoardStepEventArgs args);

Next we need to add a function to invoke the delegate, this allows for cross thread calls and is triggered a call from the main application

public void UpdateStep(BalanceBoardStepEventArgs args)
{
BeginInvoke(new UpdateBalanceBoardStepDelegate(UpdateBalanceBoardStep), args);
}

Finally we add our work function. This function receives the Event and then uses the arguments provided to detirmine the required action  to take in an equivalent manner of  the existing Wiimote State/Extension functions.

/// <summary>
/// Board Step example to show triggering of action via one of the two current step filters
/// Quad – Triggered via a change in Quad Area
/// Zone – Triggered via a change in Zone Area
/// </summary>
private void UpdateBalanceBoardStep(BalanceBoardStepEventArgs args)
{
BalanceBoardState bb = args.BalanceBoard;

switch (args.StepType)
{
case StepType.Quad:
// Quadrant output usefull for WiiFit Step game equivalents
lblQuad.Text = bb.Area.CurrentQuad;
Console.WriteLine(“Call from quad step”);
break;
case StepType.Zone:
// Quadzone output usefull for WiiFit Step game equivalents
lblZone.Text = bb.Area.CurrentZone;
Console.WriteLine(“Call from zone step”);
break;
}
}

We now have everything in place but before we can use the new Event we need to register it with the Wiimote etc as you would with the existing ones.

In your connection call add (MultipleWiimoteForm.cs/SingleWiimoteForm.cs) the following below the WiimoteChanged and WiimoteExtensionChanged register calls:

wm.BalanceBoardStep += wm_BalanceBoardStep;

Finally also add the corresponding function which is used to call the update within the WiimoteInfo.cs just below the equivalent State/Extension ones:

void wm_BalanceBoardStep(object sender, BalanceBoardStepEventArgs e)
{
WiimoteInfo wi = mWiimoteMap[((Wiimote)sender).ID];
wi.UpdateStep(e);
}

That’s it debug the program and enjoy…!

Update: Step ON and Step OFF

Ok we could stop here but lets expand things a little bit more by adding to the step Event facility a check too see weather we are “ON/OFF” the Board.

To do this we are going to add a check for a defined “Minimum Weight”. If the WeightKg is greater than the Minimum Weight then we are classed as being “On” the board. Simple eh! Start off by adding a the following new definitions to the BalanceBoardState struct:

/// <summary>
/// Are we on the Balance Board
/// </summary>
[DataMember]
public bool OnBoard;

/// <summary>
/// Balance Board Minimum Weight Tolerance
/// </summary>
[DataMember]
public double Tolerance;

Before we can use these new definitions we need to initialize them by assigning some default values. At the top of Wiimote.cs just above the Default constructor add the following :

// Temporary Defaults
private double Tolerance = 6;

This enables access to the default value for customization at start up. This value can then be updated at run time, why? I have currently access to 2 boards. At rest one reads 2kg and the other 5kg, go figure!

Add the following to the SetBalanceBoardDefaults() function to set the default values.

// set the default min weight used to trigger board use etc
mWiimoteState.BalanceBoardState.Tolerance = Tolerance;
mWiimoteState.BalanceBoardState.OnBoard = false;

Please note I always assume the user not to be on the Board at startup. With these definitions in place we can now use them to check for both the stepping On and Off events but before we do that we need to add them to the StepType enum within DataTypes.cs as follows:

/// <summary>
/// Step On
/// </summary>
On = 2,
/// <summary>
/// Step Off
/// </summary>
Off = 3,

Ok, now everything is in place lets use it. The next step is to enclose the existing Quad Zone and Step code in an IF statement that polls the weight against the tolerance. Here is our statement without the contents:

if (mWiimoteState.BalanceBoardState.WeightKg > mWiimoteState.BalanceBoardState.Tolerance)
{
// step ON event check  – (we will cover this next)
// calculate and set values for Quad
// calculate and set values for Zone
// step filtering Quad
// step filtering Zone
}
else
{
// step OFF event check – (we will cover this next)
}

Please update your Wiimote.cs to reflect the above. Once complete the next step is to add Triggers for both the OFF and On events. These event call are similar to those of Quad and Zone. Replace the //step On event check comment with the following block:

#region On Board Step Event Check

if (!mWiimoteState.BalanceBoardState.OnBoard)
{
mWiimoteState.BalanceBoardState.OnBoard = true;
if (BalanceBoardStep != null)
{ BalanceBoardStep(this, new BalanceBoardStepEventArgs(mWiimoteState.BalanceBoardState, StepType.On)); }
}

#endregion On Board Step Event Check

Next replace the // step OFF event check comment with:

#region Off Board Step Event Check

if (mWiimoteState.BalanceBoardState.OnBoard)
{
mWiimoteState.BalanceBoardState.OnBoard = false;
if (BalanceBoardStep != null)
{ BalanceBoardStep(this, new BalanceBoardStepEventArgs(mWiimoteState.BalanceBoardState, StepType.Off)); }
}

#endregion Off Board Step Event Check

Finally all we need to do is update the UpdateBalanceBoardStep function within WiimoteInfo.cs by adding the following cases to the switch statement:

case StepType.On:
// Your Code
break;
case StepType.Off:
// Your Code
break;

There you have it! additional facility for logging when a user gets ON/OFF the balance board.

To do this we are going to add a check for a defined “Minimum Weight”. If the WeightKg is greater than the Minimum Weight then we are classed as being “On” the board. Simple eh! Start off by adding a the following new definitions to the BalanceBoardState struct:

/// <summary>
/// Are we on the Balance Board
/// </summary>
[DataMember]
public bool OnBoard;

/// <summary>
/// Balance Board Minimum Weight Tolerance
/// </summary>
[DataMember]
public double Tolerance;

Before we can use these new definitions we need to initialize them by assigning some default values. At the top of Wiimote.cs just above the Default constructor add the following :

// Temporary Defaults
private double Tolerance = 6;

This enables access to the default value for customization at start up. This value can then be updated at run time, why? I have currently access to 2 boards. At rest one reads 2kg and the other 5kg, go figure!

Add the following to the SetBalanceBoardDefaults() function to set the default values.

// set the default min weight used to trigger board use etc
mWiimoteState.BalanceBoardState.Tolerance = Tolerance;
mWiimoteState.BalanceBoardState.OnBoard = false;

Please note I always assume the user not to be on the Board at startup. With these definitions in place we can now use them to check for both the stepping On and Off events but before we do that we need to add them to the StepType enum within DataTypes.cs as follows:

/// <summary>
/// Step On
/// </summary>
On = 2,
/// <summary>
/// Step Off
/// </summary>
Off = 3,

Ok, now everything is in place lets use it. The next step is to enclose the existing Quad Zone and Step code in an IF statement that polls the weight against the tolerance. Here is our statement without the contents:

if (mWiimoteState.BalanceBoardState.WeightKg > mWiimoteState.BalanceBoardState.Tolerance)
{
// step ON event check  – (we will cover this next)
// calculate and set values for Quad
// calculate and set values for Zone
// step filtering Quad
// step filtering Zone
}
else
{
// step OFF event check – (we will cover this next)
}

Please update your Wiimote.cs to reflect the above. Once complete the next step is to add Triggers for both the OFF and On events. These event call are similar to those of Quad and Zone. Replace the //step On event check comment with the following block:

#region On Board Step Event Check

if (!mWiimoteState.BalanceBoardState.OnBoard)
{
mWiimoteState.BalanceBoardState.OnBoard = true;
if (BalanceBoardStep != null)
{ BalanceBoardStep(this, new BalanceBoardStepEventArgs(mWiimoteState.BalanceBoardState, StepType.On)); }
}

#endregion On Board Step Event Check

Next replace the // step OFF event check comment with:

#region Off Board Step Event Check

if (mWiimoteState.BalanceBoardState.OnBoard)
{
mWiimoteState.BalanceBoardState.OnBoard = false;
if (BalanceBoardStep != null)
{ BalanceBoardStep(this, new BalanceBoardStepEventArgs(mWiimoteState.BalanceBoardState, StepType.Off)); }
}

#endregion Off Board Step Event Check

Finally all we need to do is update the UpdateBalanceBoardStep function within WiimoteInfo.cs by adding the following cases to the switch statement:

case StepType.On:
// Your Code
break;
case StepType.Off:
// Your Code
break;

There you have it! additional facility for logging when a user gets ON/OFF the balance board.

Cats: Uncategorized

page meta

get trackback image get trackback image
details
  • Posted: Friday, 31 July '09
  • Edited: Saturday, 1 August '09
  • Author: admin
css | xhtml | dyadica.net © admin 2010 | powered: wordpress | fuel: ps-wii-elite | top | sign-up