Mise à jour du wrapper python pour gérer la version modifiée par nos soins de wiiuse.
[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(wmp):
14 wm = wmp[0]
15 print '--- EVENT [wiimote id %i] ---' % wm.unid
16 if wm.btns:
17 for name,b in wiiuse.button.items():
18 if wiiuse.is_pressed(wm, b):
19 print name,'pressed'
20
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']):
28 wiiuse.set_ir(wmp, 1)
29 if wiiuse.is_just_pressed(wm, wiiuse.button['Down']):
30 wiiuse.set_ir(wmp, 0)
31
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
36
37 if wiiuse.using_ir(wm):
38 for i in range(4):
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
43
44 if wm.exp.type == wiiuse.EXP_NUNCHUK:
45 nc = wm.exp.u.nunchuk
46
47 for name,b in wiiuse.nunchuk_button.items():
48 if wiiuse.is_pressed(nc, b):
49 print 'Nunchuk: %s is pressed' % name
50
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
56
57
58 def handle_ctrl_status(wmp, attachment, speaker, ir, led, battery_level):
59 wm = wmp[0]
60 print '--- Controller Status [wiimote id %i] ---' % wm.unid
61 print 'attachment', attachment
62 print 'speaker', speaker
63 print 'ir', ir
64 print 'leds', led[0], led[1], led[2], led[3]
65 print 'battery', battery_level
66
67 def handle_disconnect(wmp):
68 print 'disconnect'
69
70 if os.name != 'nt': print 'Press 1&2'
71
72 wiimotes = wiiuse.init(nmotes)
73
74 found = wiiuse.find(wiimotes, nmotes, 5)
75 if not found:
76 print 'not found'
77 sys.exit(1)
78
79 connected = wiiuse.connect(wiimotes, nmotes)
80 if connected:
81 print 'Connected to %i wiimotes (of %i found).' % (connected, found)
82 else:
83 print 'failed to connect to any wiimote.'
84 sys.exit(1)
85
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)
90
91 # for i in range(nmotes):
92 # wiiuse.rumble(wiimotes[i], 0)
93
94 #wiiuse.status(wiimotes[0])
95
96 wiiuse.motion_sensing(wiimotes[0], 1)
97
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])
105 except KeyboardInterrupt:
106 for i in range(nmotes):
107 wiiuse.set_leds(wiimotes[i], 0)
108 #wiiuse.disconnect(wiimotes[i])
109
110 print 'done'