/
wmdemo.py
executable file
·199 lines (176 loc) · 4.91 KB
/
wmdemo.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/python
import cwiid
import sys
menu = '''1: toggle LED 1
2: toggle LED 2
3: toggle LED 3
4: toggle LED 4
5: toggle rumble
a: toggle accelerometer reporting
b: toggle button reporting
e: toggle extension reporting
i: toggle ir reporting
m: toggle messages
p: print this menu
r: request status message ((t) enables callback output)
s: print current state
t: toggle status reporting
x: exit'''
def main():
led = 0
rpt_mode = 0
rumble = 0
mesg = False
#Connect to address given on command-line, if present
print 'Put Wiimote in discoverable mode now (press 1+2)...'
if len(sys.argv) > 1:
wiimote = cwiid.Wiimote(sys.argv[1])
else:
wiimote = cwiid.Wiimote()
wiimote.mesg_callback = callback
print menu
exit = 0
while not exit:
c = sys.stdin.read(1)
if c == '1':
led ^= cwiid.LED1_ON
wiimote.led = led
elif c == '2':
led ^= cwiid.LED2_ON
wiimote.led = led
elif c == '3':
led ^= cwiid.LED3_ON
wiimote.led = led
elif c == '4':
led ^= cwiid.LED4_ON
wiimote.led = led
elif c == '5':
rumble ^= 1
wiimote.rumble = rumble
elif c == 'a':
rpt_mode ^= cwiid.RPT_ACC
wiimote.rpt_mode = rpt_mode
elif c == 'b':
rpt_mode ^= cwiid.RPT_BTN
wiimote.rpt_mode = rpt_mode
elif c == 'e':
rpt_mode ^= cwiid.RPT_EXT
wiimote.rpt_mode = rpt_mode
elif c == 'i':
rpt_mode ^= cwiid.RPT_IR
wiimote.rpt_mode = rpt_mode
elif c == 'm':
mesg = not mesg
if mesg:
wiimote.enable(cwiid.FLAG_MESG_IFC);
else:
wiimote.disable(cwiid.FLAG_MESG_IFC);
elif c == 'p':
print menu
elif c == 'r':
wiimote.request_status()
elif c == 's':
print_state(wiimote.state)
elif c == 't':
rpt_mode ^= cwiid.RPT_STATUS
wiimote.rpt_mode = rpt_mode
elif c == 'x':
exit = -1;
elif c == '\n':
pass
else:
print 'invalid option'
wiimote.close()
def print_state(state):
print 'Report Mode:',
for r in ['STATUS', 'BTN', 'ACC', 'IR', 'NUNCHUK', 'CLASSIC']:
if state['rpt_mode'] & eval('cwiid.RPT_' + r):
print r,
print
print 'Active LEDs:',
for led in ['1','2','3','4']:
if state['led'] & eval('cwiid.LED' + led + '_ON'):
print led,
print
print 'Rumble:', state['rumble'] and 'On' or 'Off'
print 'Battery:', int(100.0 * state['battery'] / cwiid.BATTERY_MAX)
if 'buttons' in state:
print 'Buttons:', state['buttons']
if 'acc' in state:
print 'Acc: x=%d y=%d z=%d' % (state['acc'][cwiid.X],
state['acc'][cwiid.Y],
state['acc'][cwiid.Z])
if 'ir_src' in state:
valid_src = False
print 'IR:',
for src in state['ir_src']:
if src:
valid_src = True
print src['pos'],
if not valid_src:
print 'no sources detected'
else:
print
if state['ext_type'] == cwiid.EXT_NONE:
print 'No extension'
elif state['ext_type'] == cwiid.EXT_UNKNOWN:
print 'Unknown extension attached'
elif state['ext_type'] == cwiid.EXT_NUNCHUK:
print 'Nunchuk: btns=%.2X stick=%r acc.x=%d acc.y=%d acc.z=%d' % \
(state['nunchuk']['buttons'], state['nunchuk']['stick'],
state['nunchuk']['acc'][cwiid.X],
state['nunchuk']['acc'][cwiid.Y],
state['nunchuk']['acc'][cwiid.Z])
elif state['ext_type'] == cwiid.EXT_CLASSIC:
print 'Classic: btns=%.4X l_stick=%r r_stick=%r l=%d r=%d' % \
(state['classic']['buttons'],
state['classic']['l_stick'], state['classic']['r_stick'],
state['classic']['l'], state['classic']['r'])
def callback(mesg_list, time):
print 'time: %f' % time
for mesg in mesg_list:
if mesg[0] == cwiid.MESG_STATUS:
print 'Status Report: battery=%d extension=' % \
mesg[1]['battery'],
if mesg[1]['ext_type'] == cwiid.EXT_NONE:
print 'none'
elif mesg[1]['ext_type'] == cwiid.EXT_NUNCHUK:
print 'Nunchuk'
elif mesg[1]['ext_type'] == cwiid.EXT_CLASSIC:
print 'Classic Controller'
else:
print 'Unknown Extension'
elif mesg[0] == cwiid.MESG_BTN:
print 'Button Report: %.4X' % mesg[1]
elif mesg[0] == cwiid.MESG_ACC:
print 'Acc Report: x=%d, y=%d, z=%d' % \
(mesg[1][cwiid.X], mesg[1][cwiid.Y], mesg[1][cwiid.Z])
elif mesg[0] == cwiid.MESG_IR:
valid_src = False
print 'IR Report: ',
for src in mesg[1]:
if src:
valid_src = True
print src['pos'],
if not valid_src:
print 'no sources detected'
else:
print
elif mesg[0] == cwiid.MESG_NUNCHUK:
print 'Nunchuk Report: btns=%.2X stick=%r ' + \
'acc.x=%d acc.y=%d acc.z=%d' % (state['nunchuk']['buttons'],
state['nunchuk']['stick'],
state['nunchuk']['acc'][cwiid.X],
state['nunchuk']['acc'][cwiid.Y],
state['nunchuk']['acc'][cwiid.Z])
elif mesg[0] == cwiid.MESG_CLASSIC:
print 'Classic Report: btns=%.4X l_stick=%r r_stick=%r' + \
'l=%d r=%d' % (state['classic']['buttons'],
state['classic']['l_stick'], state['classic']['r_stick'],
state['classic']['l'], state['classic']['r'])
elif mesg[0] == cwiid.MESG_ERROR:
wiimote.close()
exit(-1)
else:
print 'Unknown Report'
main()