View Single Post
thp's Avatar
Posts: 1,391 | Thanked: 4,272 times | Joined on Sep 2007 @ Vienna, Austria
#1
I've packaged python-cwiid today and uploaded it to Extras-Devel for Fremantle. This is a small HOW-TO on interfacing with the Wiimote (Wii Remote) via Python.
  • Temporarily enable Extras-Devel
  • Install "python-cwiid" from Extras-Devel (apt-get install python-cwiid)
  • Disable Extras-Devel (optional and recommended)

Now, fire up a Python shell ("python" in X Terminal) and enter the following code:

Code:
import cwiid
wm = cwiid.Wiimote()
Now, you need to press 1+2 on your Wiimote (make sure Bluetooth is enabled on your device, otherwise it won't work). After a while, your Wiimote should be connected, and you now have an object for it that is called "wm".

Let's play a bit with the LEDs:

Code:
import time
for i in range(16):
    wm.led = i
    time.sleep(.5)
This will count to 15 (in binary) on your Wiimote LEDs. Of course, you can also enable and disable the vibration motor ("rumble") by setting the "rumble" attribute. Combined with the previous example (only a bit faster - 200ms per iteration), you get:

Code:
for i in range(16):
    wm.led = i
    wm.rumble = 1-i%2
    time.sleep(.2)
Now, let's enable button reporting:

Code:
wm.rpt_mode = cwiid.RPT_BTN
The "state" attribute gives some information about the current wiimote state:

Code:
wm.state
For me, it gives:

Code:
{'led': 15, 'rpt_mode': 2, 'ext_type': 0, 'buttons': 2, 'rumble': 0, 'error': 0, 'battery': 147}
You can now try to press buttons and see how the "buttons" field changes. You can access the "buttons" field directly using:

Code:
wm.state['buttons']
The "cwiid" module provides some constants that you can use, you can list them with

Code:
dir(cwiid)
For example, if you want to check if the "A" button is pressed, you can use something like this:

Code:
bool(wm.state['buttons'] & cwiid.BTN_A)
Last but not least, let's now also get the accelerometer data by enabling the "RPT_ACC" reporting mode (we keep RPT_BTN here as well for the buttons, but don't need to):

Code:
wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
wm.state
This gives (with the Wiimote lying flat on the surface):

Code:
{'acc': (126, 127, 152), 'led': 15, 'rpt_mode': 6, 'ext_type': 0, 'buttons': 0, 'rumble': 0, 'error': 0, 'battery': 147}
Let me finish off this short tutorial with a complete example that does the following:
  • If the Wiimote is tilted down on the X axis, enable rumble (otherwise disable)
  • While the "A" button is pressed, increment the LED counter, otherwise leave it the same

Here's the code, you can abort it with Ctrl+C:

Code:
import cwiid
import time

print 'Press 1+2 on your Wiimote now...'
wm = cwiid.Wiimote()

time.sleep(1)

wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC

wm.led = 1

while True:
    wm.rumble = (wm.state['acc'][0] < 126)
    if wm.state['buttons'] & cwiid.BTN_A:
        wm.led = (wm.state['led'] + 1) % 16
    time.sleep(.2)
Enjoy and post cool scripts here if you write them
 

The Following 7 Users Say Thank You to thp For This Useful Post: