Ajout icône pour l'analyseur des logs.
[minwii.git] / src / pywiiuse / example.py
1 #! /usr/bin/python
2
3 '''Try to implement the example in python'''
4
5 import PyWiiUse as wiiuse
6
7 import sys
8 import time
9 import os
10
11 nmotes = 2
12
13 def handle_event(wm):
14 print '--- EVENT [wiimote id %i] ---' % wm.unid, wm.btns, wm.btns_held, wm.btns_released
15 if wm.btns:
16 for name,b in wiiuse.button.items():
17 if wiiuse.is_pressed(wm, b):
18 print name,'pressed'
19
20 if wiiuse.is_just_pressed(wm, wiiuse.button['-']):
21 wiiuse.motion_sensing(wmp, 0)
22 if wiiuse.is_just_pressed(wm, wiiuse.button['+']):
23 wiiuse.motion_sensing(wmp, 1)
24 if wiiuse.is_just_pressed(wm, wiiuse.button['B']):
25 wiiuse.toggle_rumble(wmp)
26 if wiiuse.is_just_pressed(wm, wiiuse.button['Up']):
27 wiiuse.set_ir(wmp, 1)
28 if wiiuse.is_just_pressed(wm, wiiuse.button['Down']):
29 wiiuse.set_ir(wmp, 0)
30
31 if wiiuse.using_acc(wm):
32 print 'roll = %f' % wm.orient.roll
33 print 'pitch = %f' % wm.orient.pitch
34 print 'yaw = %f' % wm.orient.yaw
35
36 if wiiuse.using_ir(wm):
37 for i in range(4):
38 if wm.ir.dot[i].visible:
39 print 'IR source %i: (%u, %u)' % (i, wm.ir.dot[i].x, wm.ir.dot[i].y)
40 print 'IR cursor: (%u, %u)' % (wm.ir.x, wm.ir.y)
41 print 'IR z distance: %f' % wm.ir.z
42
43 if wm.exp.type == wiiuse.EXP_NUNCHUK:
44 nc = wm.exp.u.nunchuk
45
46 for name,b in wiiuse.nunchuk_button.items():
47 if wiiuse.is_pressed(nc, b):
48 print 'Nunchuk: %s is pressed' % name
49
50 print 'nunchuk roll = %f' % nc.orient.roll
51 print 'nunchuk pitch = %f' % nc.orient.pitch
52 print 'nunchuk yaw = %f' % nc.orient.yaw
53 print 'nunchuk joystick angle: %f' % nc.js.ang
54 print 'nunchuk joystick magnitude: %f' % nc.js.mag
55
56
57 def handle_ctrl_status(wmp, attachment, speaker, ir, led, battery_level):
58 wm = wmp[0]
59 print '--- Controller Status [wiimote id %i] ---' % wm.unid
60 print 'attachment', attachment
61 print 'speaker', speaker
62 print 'ir', ir
63 print 'leds', led[0], led[1], led[2], led[3]
64 print 'battery', battery_level
65
66 def handle_disconnect(wmp):
67 print 'disconnect'
68
69 if os.name != 'nt': print 'Press 1&2'
70
71 wiimotes = wiiuse.init(nmotes)
72
73 found = wiiuse.find(wiimotes, nmotes, 5)
74 if not found:
75 print 'not found'
76 sys.exit(1)
77
78 connected = wiiuse.connect(wiimotes, nmotes)
79 if connected:
80 print 'Connected to %i wiimotes (of %i found).' % (connected, found)
81 else:
82 print 'failed to connect to any wiimote.'
83 sys.exit(1)
84
85 for i in range(nmotes):
86 wiiuse.set_leds(wiimotes[i], wiiuse.LED[i])
87 #wiiuse.rumble(wiimotes[i], 1)
88
89 # for i in range(nmotes):
90 # wiiuse.rumble(wiimotes[i], 0)
91
92 #wiiuse.status(wiimotes[0])
93
94 wiiuse.motion_sensing(wiimotes[0], 1)
95
96 fp = file('mydata.txt', 'w')
97 t0 = time.time()
98 try:
99 while True:
100 if wiiuse.poll(wiimotes, nmotes):
101 for i in range(nmotes):
102 m = wiimotes[i][0]
103 if wiimotes[i][0].event == wiiuse.EVENT:
104 #handle_event(wiimotes[i][0])
105 wm = wiimotes[i][0]
106 t = time.time() - t0
107 print >>fp, i, t, wm.gforce.x, wm.gforce.y, wm.gforce.z
108 except KeyboardInterrupt:
109 for i in range(nmotes):
110 wiiuse.set_leds(wiimotes[i], 0)
111 #wiiuse.disconnect(wiimotes[i])
112
113 fp.close()
114 print 'done'