maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons) (https://talk.maemo.org/showthread.php?t=60178)

thp 2010-08-11 15:11

HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
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 :)

johnel 2010-08-12 13:54

Re: HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
That's brilliant I use cwiid on my laptop all the time and now it is available on my n900.

BTW: How did you successfully get cwiid to cross-compile?

(I tried it a few months ago but it wouldn't work for me).

Thanks for this :)

digitalvoid 2010-08-13 08:02

Re: HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
Works great, thanks!

Is there also a possibility in the future to read out the camera of the wiimote?

thp 2010-08-13 11:55

Re: HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
Quote:

Originally Posted by johnel (Post 784215)
BTW: How did you successfully get cwiid to cross-compile?

I simply compiled the Git master branch in Scratchbox (FREMANTLE_ARMEL target). You have to make sure that all build-dependencies (libbluetooth, etc..) are installed, then "aclocal", "autoconf" and "./configure". After that you can build it. Now, you can simply grab the source package (cwiid) and build it with "dpkg-buildpackage -rfakeroot".

Quote:

Originally Posted by digitalvoid (Post 785015)
Is there also a possibility in the future to read out the camera of the wiimote?

Simply OR cwiid.RPT_IR to the "rpt_mode" of the WiiMote object. You then should have a "ir_src" key in the state of the object and that one should have a list with positions:

Code:

{'led': 1, 'ir_src': [{'pos': (475, 281), 'size': 1}, {'pos': (560, 663), 'size': 1}, {'pos': (723, 134), 'size': 3}, None], 'rpt_mode': 8, 'ext_type': 0, 'rumble': 0, 'error': 0, 'battery': 99}

Hyrum 2015-01-06 16:05

Re: HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
Two questions... :)

1) Is it possible to connect more than one wiimote?

2) is it possible to use the speaker on the wiimote in any way??

Thanks! :D

fabreu249 2015-06-07 20:07

Re: HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
Hi, great post, how i enable nunchuck report state, because the nunchuck it's connect the wiimote, but when i press the buttons on it, i can't see the info it's pressed,
For example:
when i press the button B on wiimote (code on python)

if (buttons & cwiid.BTN_B):
print 'Button B pressed' --> i see this
time.sleep(0.2)

but when i press the button C on nunchuck

if (buttons & cwiid.NUNCHUCK_BTN_C):
print 'Button C nunchuck pressed' --> i canīt see this
time.sleep(0.2)

LeHomer 2015-07-10 19:07

Re: HOWTO: Python Wiimote fun (LEDs, rumble, accel, buttons)
 
Wow, nice work :eek:


All times are GMT. The time now is 03:35.

vBulletin® Version 3.8.8