Notices


Reply
Thread Tools
thp's Avatar
Posts: 1,389 | Thanked: 4,248 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:
Posts: 999 | Thanked: 1,117 times | Joined on Dec 2009 @ earth?
#2
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
__________________
I like cake.
 
Posts: 200 | Thanked: 300 times | Joined on Nov 2009 @ The Netherlands
#3
Works great, thanks!

Is there also a possibility in the future to read out the camera of the wiimote?
 
thp's Avatar
Posts: 1,389 | Thanked: 4,248 times | Joined on Sep 2007 @ Vienna, Austria
#4
Originally Posted by johnel View Post
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".

Originally Posted by digitalvoid View Post
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}
 

The Following User Says Thank You to thp For This Useful Post:
Posts: 1 | Thanked: 1 time | Joined on Jan 2015
#5
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!
 

The Following User Says Thank You to Hyrum For This Useful Post:
Posts: 1 | Thanked: 0 times | Joined on Jun 2015
#6
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)
 
Posts: 3 | Thanked: 1 time | Joined on Jul 2015
#7
Wow, nice work
 
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 07:02.