|
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
2 <PyFile> |
|
3 <variables> |
|
4 <variable name="WiiNunchuckStickX" type="INT"/> |
|
5 <variable name="WiiNunchuckStickY" type="INT"/> |
|
6 <variable name="WiiNunchuckAccX" type="INT"/> |
|
7 <variable name="WiiNunchuckAccY" type="INT"/> |
|
8 <variable name="WiiNunchuckAccZ" type="INT"/> |
|
9 <variable name="WiiNunchuckButtons" type="WORD"/> |
|
10 <variable name="WiiButtons" type="WORD"/> |
|
11 </variables> |
|
12 <globals> |
|
13 <![CDATA[ |
|
14 import cwiid,commands,sys,re,os,time |
|
15 |
|
16 wiimote = None |
|
17 WIIMOTE_ADDR_MODEL = re.compile("((?:[0-9A-F]{2})(?::[0-9A-F]{2}){5})\s*Nintendo") |
|
18 nunchuckzero = None |
|
19 |
|
20 def Wiimote_cback(messages, time): |
|
21 global nunchuckzero |
|
22 state = dict(messages) |
|
23 bts = state.get(cwiid.MESG_BTN, None) |
|
24 if bts is not None: |
|
25 PLCGlobals.WiiButtons = bts |
|
26 nunchuck = state.get(cwiid.MESG_NUNCHUK, None) |
|
27 if nunchuck is not None: |
|
28 PLCGlobals.WiiNunchuckButtons = nunchuck['buttons'] |
|
29 X,Y = nunchuck['stick'] |
|
30 PLCGlobals.WiiNunchuckAccX = nunchuck['acc'][cwiid.X] |
|
31 PLCGlobals.WiiNunchuckAccY = nunchuck['acc'][cwiid.Y] |
|
32 PLCGlobals.WiiNunchuckAccZ = nunchuck['acc'][cwiid.Z] |
|
33 if nunchuckzero is None: |
|
34 nunchuckzero = X,Y |
|
35 (PLCGlobals.WiiNunchuckStickX, |
|
36 PLCGlobals.WiiNunchuckStickY) = X-nunchuckzero[0],Y-nunchuckzero[1] |
|
37 |
|
38 def Connect_Wiimote(connected_callback): |
|
39 global wiimote,nunchuckzero |
|
40 mac_addr = '' |
|
41 try: |
|
42 mac_addr = file("wiimac.txt","rt").read() |
|
43 except: |
|
44 PLCObject.LogMessage("Wiimote MAC unknown, scanning bluetooth") |
|
45 output = commands.getoutput("hcitool scan") |
|
46 result = WIIMOTE_ADDR_MODEL.search(output) |
|
47 if result is not None: |
|
48 mac_addr = result.group(1) |
|
49 PLCObject.LogMessage("Found Wiimote with MAC %s"%mac_addr) |
|
50 file("wiimac.txt","wt").write(mac_addr) |
|
51 |
|
52 # Connect to wiimote |
|
53 if not mac_addr: |
|
54 PLCObject.LogMessage("Connection to unknown Wiimote...") |
|
55 wiimote = cwiid.Wiimote() |
|
56 else: |
|
57 PLCObject.LogMessage("Connection to Wiimote %s..."%mac_addr) |
|
58 wiimote = cwiid.Wiimote(mac_addr) |
|
59 |
|
60 if wiimote is not None: |
|
61 nunchuckzero = None |
|
62 wiimote.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_EXT |
|
63 # use the callback interface |
|
64 wiimote.mesg_callback = Wiimote_cback |
|
65 wiimote.enable(cwiid.FLAG_MESG_IFC) |
|
66 connected_callback(mac_addr) |
|
67 PLCObject.LogMessage("Wiimote %s Connected"%mac_addr) |
|
68 else: |
|
69 PLCObject.LogMessage("Wiimote %s not found"%mac_addr) |
|
70 os.remove("wiimac.txt") |
|
71 connected_callback(None) |
|
72 |
|
73 def Disconnect_Wiimote(): |
|
74 global wiimote |
|
75 if wiimote is not None: |
|
76 wiimote.disable(cwiid.FLAG_MESG_IFC) |
|
77 time.sleep(0.1) |
|
78 wiimote.close() |
|
79 wiimote = None |
|
80 PLCObject.LogMessage("Wiimote disconnected") |
|
81 |
|
82 ]]> |
|
83 </globals> |
|
84 <init> |
|
85 <![CDATA[ |
|
86 ]]> |
|
87 </init> |
|
88 <cleanup> |
|
89 <![CDATA[ |
|
90 Disconnect_Wiimote() |
|
91 |
|
92 ]]> |
|
93 </cleanup> |
|
94 <start> |
|
95 <![CDATA[ |
|
96 ]]> |
|
97 </start> |
|
98 <stop> |
|
99 <![CDATA[ |
|
100 ]]> |
|
101 </stop> |
|
102 </PyFile> |