User Tools

Site Tools


coding:wiimote

Wiimote

I've wanted to use my Wiimote to control my PC for some time now, and finally got around to trying it all out.

Getting the wiimote connected to my PC was easy - just install wmgui, a little GUI app which demonstrates connectivity (via libcwiid1.so).

There's also a package called wminput which probably does what most people want - it turns a wiimote into an X11 input device (e.g. mouse) so you can point to move the mouse around. You have to sudo modprobe uinput first on Ubuntu 9.04, btw.

Doing it myself

I wanted to play with it myself, so I installed python-cwiid and messed about with a Python script until I got the hang of the API1):

#!/usr/bin/env python
# Wiimote experiments. Hopefully this'll grow into something I can use with
# wminput (or emulate it to the same affect)
 
import os
import sys
import time
import cwiid
 
wiimote = None
print "Press 1+2 on the Wiimote now..."
while not wiimote:
	try:
		wiimote = cwiid.Wiimote()
	except e:
		print e
		print "Ignoring previous exception"
		time.sleep(0.5)
 
wiimote.led = cwiid.LED1_ON | cwiid.LED2_ON | cwiid.LED3_ON | cwiid.LED4_ON
wiimote.rumble = True
time.sleep(0.4)
wiimote.rumble = False
wiimote.led = cwiid.LED1_ON | cwiid.LED4_ON
 
print wiimote.state
 
wiimote.enable(cwiid.FLAG_MESG_IFC)	# "if changes"
wiimote.rpt_mode = cwiid.RPT_BTN
while True:
	messages = wiimote.get_mesg()
 
	for mtype, m in messages:
		if mtype == cwiid.MESG_BTN:
			for x in dir(cwiid):
				if not x.startswith("BTN_"): continue
				if eval("cwiid.%s" % x) != m: continue
				print x
				break
			if m == cwiid.BTN_A | cwiid.BTN_B:
				print "A + B, exiting"
				wiimote.disable(cwiid.FLAG_MESG_IFC)
				wiimote.rpt_mode = 0
				wiimote.close()
				exit	# Causes a segfault.. (why?)
 
		elif mtype == cwiid.MESG_ERROR:
			for x in dir(cwiid):
				if not x.startswith("ERROR_"): continue
				if eval("cwiid.%s" % x) != m: continue
				print x
				break
 
		else:
			for x in dir(cwiid):
				if not x.startswith("MESG_"): continue
				if eval("cwiid.%s" % x) != mtype: continue
				print x
				break

Next I need to workout how to inject things into X11, probably via /dev/misc/uinput. I think this snippet will be invaluable.

1)
WiiMote Project.com was useful in working out the API
coding/wiimote.txt · Last modified: 2009/06/07 23:58 by robm