forked from hologramber/NeuroGame-EMG-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pywiimoteControl.py
executable file
·103 lines (80 loc) · 2.97 KB
/
pywiimoteControl.py
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
#! /usr/bin/python
'''Try to implement the example in python'''
import wiiuse
import sys
import time
import os
nmotes = 2
def handle_event(wmp):
wm = wmp[0]
print '--- EVENT [wiimote id %i] ---' % wm.unid, wm.btns, wm.btns_held, wm.btns_released
if wm.btns:
for name,b in wiiuse.button.items():
if wiiuse.is_pressed(wm, b):
print name,'pressed'
if wiiuse.is_just_pressed(wm, wiiuse.button['-']):
wiiuse.motion_sensing(wmp, 0)
if wiiuse.is_just_pressed(wm, wiiuse.button['+']):
wiiuse.motion_sensing(wmp, 1)
if wiiuse.is_just_pressed(wm, wiiuse.button['B']):
wiiuse.toggle_rumble(wmp)
if wiiuse.is_just_pressed(wm, wiiuse.button['Up']):
wiiuse.set_ir(wmp, 1)
if wiiuse.is_just_pressed(wm, wiiuse.button['Down']):
wiiuse.set_ir(wmp, 0)
if wiiuse.using_acc(wm):
print 'roll = %f' % wm.orient.roll
print 'pitch = %f' % wm.orient.pitch
print 'yaw = %f' % wm.orient.yaw
if wiiuse.using_ir(wm):
for i in range(4):
if wm.ir.dot[i].visible:
print 'IR source %i: (%u, %u)' % (i, wm.ir.dot[i].x, wm.ir.dot[i].y)
print 'IR cursor: (%u, %u)' % (wm.ir.x, wm.ir.y)
print 'IR z distance: %f' % wm.ir.z
if wm.exp.type == wiiuse.EXP_NUNCHUK:
nc = wm.exp.u.nunchuk
for name,b in wiiuse.nunchuk_button.items():
if wiiuse.is_pressed(nc, b):
print 'Nunchuk: %s is pressed' % name
print 'nunchuk roll = %f' % nc.orient.roll
print 'nunchuk pitch = %f' % nc.orient.pitch
print 'nunchuk yaw = %f' % nc.orient.yaw
print 'nunchuk joystick angle: %f' % nc.js.ang
print 'nunchuk joystick magnitude: %f' % nc.js.mag
def handle_ctrl_status(wmp, attachment, speaker, ir, led, battery_level):
wm = wmp[0]
print '--- Controller Status [wiimote id %i] ---' % wm.unid
print 'attachment', attachment
print 'speaker', speaker
print 'ir', ir
print 'leds', led[0], led[1], led[2], led[3]
print 'battery', battery_level
def handle_disconnect(wmp):
print 'disconnect'
if os.name != 'nt': print 'Press 1&2'
wiimotes = wiiuse.init(nmotes, [1,2], handle_event, handle_ctrl_status, handle_disconnect)
found = wiiuse.find(wiimotes, nmotes, 5)
if not found:
print 'not found'
sys.exit(1)
connected = wiiuse.connect(wiimotes, nmotes)
if connected:
print 'Connected to %i wiimotes (of %i found).' % (connected, found)
else:
print 'failed to connect to any wiimote.'
sys.exit(1)
for i in range(nmotes):
wiiuse.set_leds(wiimotes[i], wiiuse.LED[i])
wiiuse.rumble(wiimotes[i], 1)
for i in range(nmotes):
wiiuse.rumble(wiimotes[i], 0)
wiiuse.status(wiimotes[0])
try:
while True:
wiiuse.poll(wiimotes, nmotes)
except KeyboardInterrupt:
for i in range(nmotes):
wiiuse.set_leds(wiimotes[i], 0)
wiiuse.disconnect(wiimotes[i])
print 'done'