Using the Wiimote with Windows Phone 7 Emulator

So over the weekend I had the bright idea of using a Wiimote to simulate the accelerometer for Windows Phone 7 development.  I figured it couldn’t be that difficult, as WiimoteLib has been around a while now.  3 days later, I’ve finally managed to get that to work.  Through no small effort, either.

Things started off on the wrong foot when I discovered I didn’t actually have a Bluetooth USB dongle handy.  So I couldn’t actually connect the Wiimote to my desktop PC where I do my development.

No problem, I’ll just sync it to my laptop, and feed the data over my network.  Latency shouldn’t be a problem, as they’d both be connected via Cat5, with a single switch between them.

Which is when I discovered problem #2.  My laptop didn’t have a Bluetooth module in it.  Never noticed before, because I never needed it.

Okay, so after a bit of head-scratching, I found an older laptop with a Bluetooth module in it.  I yanked it, and put it into my working laptop, found all of the drivers, and figured I was good to go.

Popping some fresh batteries into the Wiimote, I did the sync hold (1+2) and my laptop detected it just fine.  They synced up, and I went and fired up the test app that comes with WiimoteLib.

At which point it told me there weren’t any Wiimotes attached.  So, I double checked that it was still synced, and it was.  Several profanities later, I went back over the WiimoteLib reading materials.  Seems I needed to install the Windows SDK in order to get the sensor suite to work.

Once that was all installed, I tried syncing again.  This time, I was successful in connecting the test app to the Wiimote, and it was spitting out data at me.  Woohoo!

Okay, now to write up a little HttpListener app to feed the data over the network.  Borrowing liberally from the test app code, I get something written up which does the job.  Not pretty, but pretty is not necessary.

So I write up a little client app to test that everything is working alright.  But now I see another problem.  I’m only getting about 2 updates per second fed into the client.  I put a timer into the code to double check, and sure enough, it’s taking over 400ms for each HttpRequest.

That’s just not good enough.  You can’t test input when latency is over 400ms.  (Hear that, OnLive?)  After hours of searching, I could find no reason for that kind of latency.  Even now I have no idea why it was so high.

I tested the same HttpListener / HttpRequest code with client and server both on my desktop, and the latency was 1ms.  Where did the 399ms come from over the network?

So, time for another plan.  I thought about the problem I had to solve.  I needed to get the Wiimote data from my laptop to my desktop with as little latency as possible.  I considered the limitations I had.  WP7 can’t network except through HTTP.

With that in mind, I struck on the idea of creating a ‘proxy’ app.  My laptop would send the data over TCP sockets to my desktop, where a proxy app would be running to receive it.  The proxy app would be the client in the TCP data exchange, but would also be the server in the HTTP exchange.

I downloaded the latest Lidgren networking library, since I had never used it and this seemed like a good excuse to become familiar with it.  The samples showed how to send messages simply enough, and soon I had that part working.  My laptop was sending the Wiimote data to my proxy app on the desktop, with practically no latency.

Then the moment of truth finally arrived.  What kind of latency would I experience using the HttpRequest to ask the proxy for the data?

When I first fired up the Http client, I was worried, as it appeared to be back to 400ms latency.  But after a second or so, the data started pouring in at 1ms latency.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: