<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog . sebastian martens &#187; WiiFlash</title>
	<atom:link href="http://blog.sebastian-martens.de/tag/wiiflash/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sebastian-martens.de</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 15:55:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Use your Wii-Remote with the FlashPlayer &#8211; WiiFlash</title>
		<link>http://blog.sebastian-martens.de/2009/08/use-your-wii-remote-with-the-flashplayer-wiiflash/</link>
		<comments>http://blog.sebastian-martens.de/2009/08/use-your-wii-remote-with-the-flashplayer-wiiflash/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 20:12:43 +0000</pubDate>
		<dc:creator>Sebastian.Martens</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[com.nonstatics.GestureDetection]]></category>
		<category><![CDATA[Gesture Detection]]></category>
		<category><![CDATA[joa ebert]]></category>
		<category><![CDATA[lowpass filter]]></category>
		<category><![CDATA[Motion Detection]]></category>
		<category><![CDATA[Nintendo]]></category>
		<category><![CDATA[Thibault Imbert]]></category>
		<category><![CDATA[Wii]]></category>
		<category><![CDATA[Wii Remote Control]]></category>
		<category><![CDATA[WiiFlash]]></category>
		<category><![CDATA[Wiimote]]></category>

		<guid isPermaLink="false">http://blog.sebastian-martens.de/?p=204</guid>
		<description><![CDATA[You maybe know the Nintendo Wii and its controller the Wiimote. Joa Ebert and Thibault Imbert implement a AS3 package called WiiFlash which allows you to connect the Wiimote ( or Wii Remote Control ) with your Flash/Flex Application. The Wiimote uses several sensors to detect different motion values. It allows you to read the [...]]]></description>
			<content:encoded><![CDATA[<p><a  href="http://www.flexughh.de/referenten/" style="border:0; padding:0; margin:0;"><img src="http://www.flexughh.de/images/ads/flexughh_speaker_234_60.jpg" width="234" height="60" border="0" title="Speaker at Adobe Flex User Group Hamburg - Germany" style="border:0; padding:0; margin:0 10px 5px 0; " align="left"/></a>You maybe know the <a  href="http://www.wii.com/">Nintendo Wii</a> and its controller the Wiimote. <a  href="http://www.joa-ebert.com/">Joa Ebert</a> and <a  href="http://www.bytearray.org/">Thibault Imbert</a> implement a AS3 package called <a  href="http://wiiflash.bytearray.org/">WiiFlash</a> which allows you to connect the Wiimote ( or Wii Remote Control ) with your Flash/Flex Application.</p>
<p>The Wiimote uses several sensors to detect different motion values. It allows you to read the acceleration value for X-,Y-, Z-axis and Roll, Pitch and Yaw, which is the rotation around the x-,y- and z-axis. So you have a lot more degrees of freedom than with a simple mouse or keyboard. A funny thing you could implement with this for example is <a  href="http://www.youtube.com/watch?v=PfrKkPLXWYk">WiiSpray</a>, which also uses the WiiFlash Lib.</p>
<p>The WiiFlash package consists of two parts. The first one is the WiiFlash Server. It&#8217;s written in C++ / .NET and handles the communication with the Wiimote. The second part is the WiiFlash ActionScript API, which gives you methods and events to get and handle the Wiimote data.<br />
<span id="more-204"></span><br />
The Wiimote communicates via Bluetooth with its endpoint. So the WiiFlash Server connects to your PCs or Mac Bluetooth stack and reads the data from the Bluetooth connection. This data are send via Socketconnection to the Flash Player. The WiiFlash AS3 API now converts the Wiimote binary protocoll into useable data. Which means you could directly read the values and also get update and/or change events if anything happens.</p>
<p><strong>Connect the Wiimote</strong><br />
The WiiServer is available for PC or Mac. At first you have to connect your Wiimote with your PC or Mac. That means you have to create a Bluetooth connection between them &#8211; pair them via Bluetooth. After this step the WiiFlash Server should be able to find the Bluetooth connection and create the connection to the Flash Player. Find a detailed description how to connect your Wiimote with your PC <a  href="http://wiiflash.bytearray.org/?page_id=25">here</a>.</p>
<p>After you could pair your PC/Mac with the Wiimote, disconnect the Wiimote and start the WiiFlash Server. When the server started connect your Wiimote again ( this is done by pressing the Buttons &#8220;1&#8243; + &#8220;2&#8243; of your Wiimote at the same time ). After a short time you should see the connected Wiimote at the WiiFlash Server. It shows you the Batterylevel and the Wiimote-ID. Both Server versions could connect up to four Wiimotes at the same time.</p>
<p>I did not test the WiiFlash under Windows Vista. But wit OSX 10.5.x you&#8217;ll get an Java error when you start the server:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Cannot launch Java application.  <span style="color: #006633;">Uncaught</span> exception in main method<span style="color: #339933;">:</span>
java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">IllegalStateException</span><span style="color: #339933;">:</span> Bluetooth failed to initialize.
<span style="color: #006633;">There</span> is probably a problem with your local Bluetooth stack or API.</pre></div></div>

<p>You just have to declare the WiiFlash Server as 32bit application. Just open the file properties ( Apple + i ) and check the 32bit-mode checkbox. This will fix the problem. ( Otherwise the application is launched as 64bit application ).</p>
<p>Unfortunately both server versions aren&#8217;t that stabel. From time to time the server crashes or the connection between server and Flash Player doesn&#8217;t seem to work any more. You just have to close the server, disconnect the Wiimote from your PC or Mac and restart the connection. This also helps if the rumble function or the LED setter method seem to quit their work.</p>
<p><strong>Connect the Wiimote with your Flex App</strong><br />
The WiiFlash AS3 API gives you a very clear set of methods, which gives you fully access to the Wiimote data. At first you have to create an instance of the Wiimote class. Now you could create the connection to the Wiimote and access the data.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> org.<span style="color: #006600;">wiiflash</span>.<span style="color: #006600;">Wiimote</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> wiimote:Wiimote;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	wiimote = <span style="color: #000000; font-weight: bold;">new</span> Wiimote<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* use Event.Connect NOT WiimoteEvent.CONTROLLER_CONNECT */</span>
	wiimote.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #0066CC;">CONNECT</span>, connectHandler <span style="color: #66cc66;">&#41;</span>;
&nbsp;
	wiimote.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> connectHandler<span style="color: #66cc66;">&#40;</span> evt:Event <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Wiimote is connected&quot;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><strong>Read Wiimote data</strong><br />
The accelerator values could be accessed on to different ways. First you could read the data directly from the Wiimote-instance. This is helpful if you have an timed update interval, which handles the data. For example if you use an the enterFrame event to call a method which handles the Wiimote data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	wiimote = <span style="color: #000000; font-weight: bold;">new</span> Wiimote<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* get Wiimote data onEnterFrame */</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameHandler <span style="color: #66cc66;">&#41;</span>;
&nbsp;
	wiimote.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameHandler<span style="color: #66cc66;">&#40;</span> evt:Event <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> wiimote.<span style="color: #006600;">sensorX</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> wiimote.<span style="color: #006600;">roll</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The common way is to use the Wiimote Update event. This event is fired on each update update call from the Bluetooth stack. ATTENTION: The communication works with about 100Hz. This means you will get about 100 datasets per second. This is important for how you will later work with your data, and have to be aware of performance issues if you might bad datastructures or uneffective datahandling methods.</p>
<p><strong>Write Wiimote data</strong><br />
You could not only read values from the Wiimote. You could also write two values of the Wiimote. This are the <em>led</em> and the <em>rumble</em> attribute. With the led attribute you should be able to specify which led-light on the Wiimote shines. This is not really working well, but works sometimes &#8230;<br />
You could also use the Force-Feedback of the Wiimote. Just set the <em>rumble</em> value to true and the Wiimote start vibrating. If its used this way, its vibrating all time. You could also set the <em>rumbleTimeout</em> value. The parameter is the time in milliseconds the Wiimote starts to rumble.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* set LEDs */</span>
wiimote.<span style="color: #006600;">leds</span> = Wiimote.<span style="color: #006600;">LED4</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* set Force Feedback - for positiv direction */</span>
<span style="color: #808080; font-style: italic;">// wiimote.rumble = ( wiimote.sensorX&gt;3 || wiimote.sensorY&gt;3 || wiimote.sensorZ&gt;3 );</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> wiimote.<span style="color: #006600;">sensorX</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">||</span> wiimote.<span style="color: #006600;">sensorY</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">||</span> wiimote.<span style="color: #006600;">sensorZ</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	wiimote.<span style="color: #006600;">rumbleTimeout</span> = <span style="color: #cc66cc;">1000</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><strong>Interprete the Wiimote data</strong><br />
The Wiimote controller returns the acceleration concerning to the gravity value <em>g</em>. 1g means a gravity acceleration of 9.82m/s<sup>2</sup>. The controller itself is able to detect values between -3.6g and +3.6g.</p>
<p>If the Wiimote will not be moved and rests still on the table, you will always have on value (sensorX,sensorY,sensorZ) which is -1 or +1. This is the gravity affecting on the Wiimote. This is the criteria to detect if you Wiimote is in motion or not. Therefore you could use the Euclid Normal Form. If you take the root of the sum of all squares, this will be near +1. Normally you will define a constant values which is a bit higher than exact +1 to ignore very little movements of the Wiimote.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* const to detect motion */</span>
<span style="color: #0066CC;">private</span> const THRESHOLD:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.01</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> wiimoteUpdateHandler<span style="color: #66cc66;">&#40;</span> evt:WiimoteEvent <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">_x</span> = evt.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">sensorX</span>;
	<span style="color: #0066CC;">_y</span> = evt.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">sensorY</span>;
	_z = evt.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">sensorZ</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> euLength:<span style="color: #0066CC;">Number</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* check for &quot;NO MOTION&quot; */</span>
	euLength = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">_x</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">_x</span> + <span style="color: #0066CC;">_y</span><span style="color: #66cc66;">*</span><span style="color: #0066CC;">_y</span> + _z<span style="color: #66cc66;">*</span>_z <span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/** set Force Feedback - for positiv direction **/</span>
	wiimote.<span style="color: #006600;">rumble</span> = <span style="color: #66cc66;">&#40;</span> _x<span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">||</span> _y<span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">||</span> _z<span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>euLength<span style="color: #66cc66;">&gt;</span>THRESHOLD<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/* do something */</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><strong>Optimize Wiimote date</strong><br />
Normally you won&#8217;t directly use the rough data from the Wiimote. Because the detection is relative precise you will get a lot of little peaks and a very unsmooth curve of data. If you want to detect a specific gesture or just want to transform the Wiimote motion into a motion of objects in the Flash you should parse the data. For smoothing the input data you should use a lowpass filter. It will smooth the curve by multiply the actual value with older values. In the following example a new value will only enter 10%. 90% of the value is taken from the old value.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* const for filtering values */</span>
<span style="color: #0066CC;">private</span> const FILTER_FACTOR:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.1</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* high pass filter */</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> highPassFilter<span style="color: #66cc66;">&#40;</span> oldValue:<span style="color: #0066CC;">Number</span>, value:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> value - lowPassFilter<span style="color: #66cc66;">&#40;</span> oldValue, value <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* low pass filter */</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> lowPassFilter<span style="color: #66cc66;">&#40;</span> oldValue:<span style="color: #0066CC;">Number</span>, value:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span>  value <span style="color: #66cc66;">*</span> FILTER_FACTOR + oldValue <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1.0</span> - FILTER_FACTOR <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> wiimoteUpdateHandler<span style="color: #66cc66;">&#40;</span> evt:WiimoteEvent <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">_x</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">lowPassFilter</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">_x</span>, evt.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">sensorX</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_y</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">lowPassFilter</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">_y</span>, evt.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">sensorY</span> <span style="color: #66cc66;">&#41;</span>;
	_z = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">lowPassFilter</span><span style="color: #66cc66;">&#40;</span> _z, evt.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">sensorZ</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* do something with the data */</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><strong>Gesture detection</strong><br />
If you know have smooth data, which you only will process if the Wiimote is really in motion, you could start to detect gestures. One of the very simple gestures is a shift. If you just shake the Wiimote a little step right it should detect a rightshift and maybe open the next Powerpoint slide. If you shift to left left the application should go on step back.</p>
<p>For this examples you have to watch the x sensor values of the Wiimote. Because you got the accelation of the controller you will not just get on positive peak in x-direction, but you will get a positive peak in x-direction followed by a negative peak in the x-direction. This is because you start a motion in positive x-direction but than stop to motion, which is a negative accelaration. An right shift is a positive x peak followed by a negative x peak. Vice versa a left shift is a negative x peak followed by a positive x peak.</p>
<p>So the first thing to implement is a peak detection class. I do this by writing several values to a history array ( for example of the last 40 values ). I now compare the avarage of the last 2 or 3 values to the avarage of all history values. If the difference between the two avarage values exceed a specific value ( depending on how exact my motion detection should work ) a peak is detected.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * @classdescription: peak detection. detects a peak in an array of values
 * @author: Sebastian Martens - http://blog.sebastian-martens.de/
 * @SVN: $Id$
 *
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PeakDetect<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _history:Vector.<span style="color: #66cc66;">&lt;</span>number<span style="color: #66cc66;">&gt;</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _historyLength:<span style="color: #0066CC;">int</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _kernelLength:<span style="color: #0066CC;">int</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _peakFactor:<span style="color: #0066CC;">Number</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _lastPeak:<span style="color: #0066CC;">Boolean</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * @constructor
	 * @param kernelLength Number of values which represent the actual motion
	 * @param historyLength Number of values which are taken as history of motion
	 * @param peakFactor If distance between history and actual motion greater than this value motion is detected
	 *
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PeakDetect<span style="color: #66cc66;">&#40;</span> kernelLength:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">2</span>, historyLength:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">32</span>, peakFactor:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">0.2</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;
&nbsp;
		_history = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>number<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		_kernelLength = kernelLength;
		_historyLength = historyLength;
		_peakFactor = peakFactor;
&nbsp;
		_lastPeak = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// init with 0</span>
		<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>;i<span style="color: #66cc66;">&lt;</span>_historyLength;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			_history<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * calculates the intersection value of items in the array
	 * @param startIndex to get avarage value from the history list
	 * @param length number of values which should calculate to avarage value
	 * @return avarage value
	 */</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getIntersection<span style="color: #66cc66;">&#40;</span> startIndex:<span style="color: #0066CC;">int</span>, <span style="color: #0066CC;">length</span>:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;
		<span style="color: #000000; font-weight: bold;">var</span> mValue:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=startIndex;<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#40;</span>startIndex+<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&amp;&amp;</span>i<span style="color: #66cc66;">&lt;</span>_historyLength<span style="color: #66cc66;">&#41;</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			mValue += _history<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>mValue<span style="color: #66cc66;">/</span><span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * tries to detect a peak in the list of value
	 * @return true if a peak was detected
	 *
	 */</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _checkForPeak<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> pValue:<span style="color: #0066CC;">Number</span>;
		<span style="color: #000000; font-weight: bold;">var</span> aValue:<span style="color: #0066CC;">Number</span>;
		<span style="color: #000000; font-weight: bold;">var</span> peakVal:<span style="color: #0066CC;">Number</span>;
		<span style="color: #000000; font-weight: bold;">var</span> multiPl:<span style="color: #0066CC;">int</span>;
&nbsp;
		pValue = _getIntersection<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,_historyLength<span style="color: #66cc66;">&#41;</span>;
		aValue = _getIntersection<span style="color: #66cc66;">&#40;</span> _historyLength-_kernelLength, _kernelLength <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> aValue<span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			peakVal = <span style="color: #66cc66;">&#40;</span>aValue-pValue<span style="color: #66cc66;">&#41;</span>;
			multiPl = <span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
			peakVal = <span style="color: #66cc66;">&#40;</span>pValue-aValue<span style="color: #66cc66;">&#41;</span>;
			multiPl = -<span style="color: #cc66cc;">1</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> peakVal<span style="color: #66cc66;">&gt;</span>_peakFactor <span style="color: #66cc66;">&#41;</span>?<span style="color: #66cc66;">&#40;</span>peakVal<span style="color: #66cc66;">*</span>multiPl<span style="color: #66cc66;">&#41;</span>:<span style="color: #cc66cc;">0</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Adds a new value to item history. Return true if peak is detected.
	 * @param value Number-
	 * @return Boolean -
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addValue<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#123;</span>
		_history.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		_history<span style="color: #66cc66;">&#91;</span> <span style="color: #66cc66;">&#40;</span>_historyLength-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#93;</span> =  value;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._checkForPeak<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Finally you could now decide on the order of the different peaks which type of gesture it should be. Therefore i use a timer. If one peak is detected the timer starts. If another peak is detected within the runtime of the timer a gesture is detected if the peak order fits to a gusture.</p>
<p>Please find attached a example FlashBuilder Project which consists serveral steps with examples. Including the WiiFlash framework and my GestureDetection library.</p>
<p><a  href="http://blog.sebastian-martens.de/wp-content/uploads/2009/08/WiiFlashTest.zip">WiiFlashTest &#8211; FlashBuilder Project</a><br />
<a  href="http://blog.sebastian-martens.de/wp-content/uploads/2009/08/com.nonstatics.GestureDetection.zip">com.nonstatics.GestureDetection &#8211; Gesture Detection Library for WiiFlash v0.1</a></p>
<p>cheers,<br />
Sebastian</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sebastian-martens.de/2009/08/use-your-wii-remote-with-the-flashplayer-wiiflash/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

