1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
.. -*- 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
<http://abstrakraft.org/cwiid/browser/trunk/wminput/action_enum.txt>`_)
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 <http://abstrakraft.org/cwiid/>`_
`Install Cwiid ( Ubuntu Documentation
) <https://help.ubuntu.com/community/CWiiD>`_
|