Thursday, January 3, 2008

Get values from Wii Remote (through IronPython and WiimoteLib.dll)

This entry is translation of my entry in Japanese. http://d.hatena.ne.jp/nishiohirokazu/20071227/1198746597

To get values from Wii Remote through IronPython is very easy.
At first you need to get WiimoteLi.dll from
Managed Library for Nintendo's Wiimote - Release: WiimoteLib v1.2.
Its document is nice.

And then save a following script as name "setup_wii.py"

import clr
clr.AddReferenceToFile("wiimotelib.dll")

from WiimoteLib import *

wii = Wiimote()
def get_value(sender, args):
global a
a = args

wii.WiimoteChanged += WiimoteChangedEventHandler(get_value)

wii.Connect()
wii.SetReportType(wii.InputReport.IRAccel, True)

In the script, at first I get a namespace "WiimoteLib" from wiimotelib.dll and import all objects in it.
Secondly I make an instance of "Wiimote" class. Its "WiimoteChanged" field is "event" and "+=" means "add event listener". I set a minimal event listener.
Finally I call connect its instance and tell it should return IR-Camera data and accelation data. You should read WiimoteLib.dll's document and choice the correct report type.

Let's use the script in interactive console.

>>> import setup_wii
>>> setup_wii.a
<WiimoteChangedEventArgs object at 0x000000000000002B>
OK, I got the argument of the event listener.
To know what fields the object have, you should read WiimoteLib.dll's document. It is very comprehensive. Today I want to get the result of IR-Camera, so I did like below.

>>> setup_wii.a.WiimoteState
<WiimoteState object at 0x000000000000002C>
>>> setup_wii.a.WiimoteState.IRState
<WiimoteLib.IRState object at 0x000000000000002D [WiimoteLib.IRState]>
>>> setup_wii.a.WiimoteState.IRState.X1
0.379091
I got it! It is very easy!

Reference:
Coding4Fun : Managed Library for Nintendo's Wiimote

Acknowledge:
The reference to my Japanese entry in IronPython URL's: Using the Wiimote from IronPython motivated me to write blog in English. Thanks!

1 comment:

Arnold said...

This entry is translation of my entry in Japanese. http://d.hatena.ne.jp/nishiohirokazu/20071227/1198746597. To get values from Wii Remote ... fernbedienung.blogspot.de