.. -*- mode: rst -*- .. -*- coding: utf-8 -*- Controling the wiimote (I) ########################## :date: 2009-03-11 :tags: Programmation, Wiimote, Python :slug: controling-the-wiimote-i :lang: en :translation: true Creating the plugin for wminput ------------------------------- There are a lot of tutorials about how to configure cwiid. I let you follow them for getting a functionnal system for your wiimote. You can read the links at the end of this article. Be sure your system works well before continuing. This is a code that we'll use as template for the creation of our driver. Used as main, it use pygame for displaying the infrared sources the wiimote can detect, but it is also compatible as plugin for wminput ( even if it does anything for now ). You can get it here : wm_control.py_ ( This code is licenced under GPL 3+ ) .. _wm_control.py: {filename}/resources/ir_control_0.py About the code : .. code-block:: python import wmplugin This import does not exist, but is created by wminput when executed. It provide the connexion with the wminput core. Just put ( or link ) the script in the wminput plugin path ( on my debian this is the /usr/lib/cwiid/plugins/ ) .. code-block:: python def wmplugin_init(id, wiimote_arg): wmplugin.set_rpt_mode(id, cwiid.RPT_IR | cwiid.RPT_BTN) return def wmplugin_info(): return [], [("X", wmplugin.REL | wmplugin.ABS, 1024, 0, 0, 0), ("Y", wmplugin.REL | wmplugin.ABS, 768, 0, 0, 0)], [] We instanciate the wiimote object here and configure it in the IR mode. The name is choosen by wminput if we want to use it as plugin. We define that the plugin return the coordonates X and Y in a relative system ( you can get the signification of all the parameters here : `actions list `_) If we want to define new butons, we just have to name them in the first list, the'll be accessible in the configuration file as plugin.[buton\_name] = ACTION .. code-block:: python def wmplugin_exec(messages): '''Wiimote callback managing method Recieves a message list, each element is different, see the libcwiid docs''' x = y = 0 for msg in messages: if msg[0] == cwiid.MESG_IR: x, y = ir_sensor.get_movement(msg) return [], (x, y) Here is the core of our driver. The name is choosen by wminput too, as the format value we return. We have in parameter the list of the messages the wiimote has sent. If we have defined buton we need to return their state here. It is a boolean saying if the buton is pressed ( True ) or not ( False ). This method doesn't send any others parameters, and this is a problem when we need to store data between two calls ( ie for saving the cursor position ). One way for passing throught is to use global variables. But it is unelegant and can cause problems if we want to use our code in imports. So we'ill use a list as default parameter and let python save it as you can see here : .. code-block:: python >>> def meth(a=[]): ... a.append(1) ... print a ... >>> meth() [1] >>> meth() [1, 1] >>> meth() [1, 1, 1] So the ir\_sensor.get\_movement method is defined with each parameter we want to save as an optional list .. code-block:: python def get_movement(msg, _old_points=[], _old_position = [0, 0]): return 0, 0 The get\_movement method need to return the difference between the old position of the cursor and the new one in tuple : (0, 0) mean that the cursor didn't move, (-10, 0) mean a deplacement to the left. For now, the plugin doesn't move the cursor, and doesn't read what the wiimote has sent. But you know everything for creating your own plugin for controlling your wiimote. You can use all the cwiid method for setting the led, activating the differents modes of your wiimote ( IR, Acc, rumble ... ), and define here your own actions. I'll explain the core of the movement analysis in IR mode in the next article. Links : `The cwiid project `_ `Install Cwiid ( Ubuntu Documentation ) `_