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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cwiid
## Configuration
#wiimote_hwaddr = '' # Use your address to speed up the connection proccess
wiimote_hwaddr = '00:1F:C5:5A:8C:69'
def connect(wm_addr = ''):
'''Connects and syncs with a wiimote
wm_addr - Is a string representing the hwaddr of the wiimote we will try to
connect, or none to try to connect with any discoverable device
for example "00:19:1D:5D:5D:DC"'''
pygame.display.flip() # called now because cwiid.Wiimote is a blocking call
# This could be done in a thread to allow pygame to draw while searching
# but this is only a test
try:
return cwiid.Wiimote(wm_addr)
except:
print "Error conectando con wiimote " + str(wm_addr)
def wmplugin_init(id, wiimote_arg):
#global wiimote
#wiimote = 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)], \
[]
def wmplugin_exec(messages, buton=[0]):
'''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_BTN:
# msg is of the form (cwiid.MESG_BTN, cwiid.BTN_*)
buton[0] = msg[1]
if msg[0] == cwiid.MESG_IR:
if screen:
# red dot for the sources
[screen.fill((255, 0, 0), (point, (4, 4))) \
for point in ir_sensor.get_positions_points(msg)]
if buton[0] & cwiid.BTN_B == 0 :
x, y = (0, 0)
return [], (x, y)
if __name__ == '__main__':
import pygame
from pygame.locals import *
runing = 0
def handle_events():
'''Typical event handling via pygame'''
for event in pygame.event.get():
if event.type == QUIT:
return 0
elif event.type == KEYUP:
if event.key == K_ESCAPE:
return 0
elif event.key == K_SPACE:
canvas.fill((0, 0, 0), ((0, 0), canvas.get_size()))
return 1
pygame.init()
pygame.display.set_caption('Wiimote IR test')
window = pygame.display.set_mode((cwiid.IR_X_MAX, cwiid.IR_Y_MAX), DOUBLEBUF)
screen = pygame.display.get_surface()
canvas = pygame.Surface(screen.get_size()) # persistent drawing here
canvas = canvas.convert()
wm = None # our wiimote
clock = pygame.time.Clock()
runing = 1
while(runing):
clock.tick(100)
runing = handle_events()
if not wm:
wm = connect(wiimote_hwaddr)
if not wm:
continue
# each message will contain info about ir and buttons
wm.rpt_mode = cwiid.RPT_IR | cwiid.RPT_BTN # | cwiid.RPT_STATUS
# tell cwiid to use the callback interface and allways send button events
wm.enable(cwiid.FLAG_MESG_IFC
#| cwiid.FLAG_NONBLOCK
| cwiid.FLAG_REPEAT_BTN)
# specify wich function will manage messages AFTER the other settings
wm.mesg_callback = wmplugin_exec
# quick check on the wiimote
print "Got Wiimote!"
st = wm.state
for e in st:
print str(e).ljust(8), ">", st[e]
screen.blit(canvas, (0, 0))
pygame.display.flip()
if wm:
wm.close()
else:
#This code is used as a plugin for cwiid, we import the wminput system
import wmplugin
screen = None
|