3 '''Try to implement the example in python'''
5 import PyWiiUse
as wiiuse
13 def handle_event(wmp
):
15 print '--- EVENT [wiimote id %i] ---' % wm
.unid
17 for name
,b
in wiiuse
.button
.items():
18 if wiiuse
.is_pressed(wm
, b
):
21 if wiiuse
.is_just_pressed(wm
, wiiuse
.button
['-']):
22 wiiuse
.motion_sensing(wmp
, 0)
23 if wiiuse
.is_just_pressed(wm
, wiiuse
.button
['+']):
24 wiiuse
.motion_sensing(wmp
, 1)
25 if wiiuse
.is_just_pressed(wm
, wiiuse
.button
['B']):
26 wiiuse
.toggle_rumble(wmp
)
27 if wiiuse
.is_just_pressed(wm
, wiiuse
.button
['Up']):
29 if wiiuse
.is_just_pressed(wm
, wiiuse
.button
['Down']):
32 if wiiuse
.using_acc(wm
):
33 print 'roll = %f' % wm
.orient
.roll
34 print 'pitch = %f' % wm
.orient
.pitch
35 print 'yaw = %f' % wm
.orient
.yaw
37 if wiiuse
.using_ir(wm
):
39 if wm
.ir
.dot
[i
].visible
:
40 print 'IR source %i: (%u, %u)' % (i
, wm
.ir
.dot
[i
].x
, wm
.ir
.dot
[i
].y
)
41 print 'IR cursor: (%u, %u)' % (wm
.ir
.x
, wm
.ir
.y
)
42 print 'IR z distance: %f' % wm
.ir
.z
44 if wm
.exp
.type == wiiuse
.EXP_NUNCHUK
:
47 for name
,b
in wiiuse
.nunchuk_button
.items():
48 if wiiuse
.is_pressed(nc
, b
):
49 print 'Nunchuk: %s is pressed' % name
51 print 'nunchuk roll = %f' % nc
.orient
.roll
52 print 'nunchuk pitch = %f' % nc
.orient
.pitch
53 print 'nunchuk yaw = %f' % nc
.orient
.yaw
54 print 'nunchuk joystick angle: %f' % nc
.js
.ang
55 print 'nunchuk joystick magnitude: %f' % nc
.js
.mag
58 def handle_ctrl_status(wmp
, attachment
, speaker
, ir
, led
, battery_level
):
60 print '--- Controller Status [wiimote id %i] ---' % wm
.unid
61 print 'attachment', attachment
62 print 'speaker', speaker
64 print 'leds', led
[0], led
[1], led
[2], led
[3]
65 print 'battery', battery_level
67 def handle_disconnect(wmp
):
70 if os
.name
!= 'nt': print 'Press 1&2'
72 wiimotes
= wiiuse
.init(nmotes
)
74 found
= wiiuse
.find(wiimotes
, nmotes
, 5)
79 connected
= wiiuse
.connect(wiimotes
, nmotes
)
81 print 'Connected to %i wiimotes (of %i found).' % (connected
, found
)
83 print 'failed to connect to any wiimote.'
86 for i
in range(nmotes
):
87 wiiuse
.set_leds(wiimotes
[i
], wiiuse
.LED
[i
])
88 print 'adr:', wiimotes
[i
][0].bdaddr_str
89 #wiiuse.rumble(wiimotes[i], 1)
91 # for i in range(nmotes):
92 # wiiuse.rumble(wiimotes[i], 0)
94 #wiiuse.status(wiimotes[0])
96 wiiuse
.motion_sensing(wiimotes
[0], 1)
100 if wiiuse
.poll(wiimotes
, nmotes
):
101 for i
in range(nmotes
):
103 if wiimotes
[i
][0].event
== wiiuse
.EVENT
:
104 handle_event(wiimotes
[i
])
105 except KeyboardInterrupt:
106 for i
in range(nmotes
):
107 wiiuse
.set_leds(wiimotes
[i
], 0)
108 #wiiuse.disconnect(wiimotes[i])