]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
21 | # | |
22 | # Nintendo Wii Nunchuk decoder | |
23 | # | |
24 | ||
25 | # TODO: Description | |
26 | ||
27 | # FIXME: This is just some example input for testing purposes... | |
28 | example_packets = [ | |
29 | # START condition. | |
30 | {'type': 'S', 'range': (10, 11), 'data': None, 'ann': ''}, | |
31 | ||
32 | # Nunchuk init: Write 0x40,0x00 to slave address 0x54. | |
33 | {'type': 'AW', 'range': (12, 13), 'data': 0x54, 'ann': ''}, | |
34 | {'type': 'DW', 'range': (14, 15), 'data': 0x40, 'ann': ''}, | |
35 | {'type': 'AW', 'range': (16, 17), 'data': 0x54, 'ann': ''}, | |
36 | {'type': 'DW', 'range': (18, 19), 'data': 0x00, 'ann': ''}, | |
37 | ||
38 | # Get data: Read 6 bytes of data. | |
39 | {'type': 'DR', 'range': (20, 21), 'data': 0x11, 'ann': ''}, | |
40 | {'type': 'DR', 'range': (22, 23), 'data': 0x22, 'ann': ''}, | |
41 | {'type': 'DR', 'range': (24, 25), 'data': 0x33, 'ann': ''}, | |
42 | {'type': 'DR', 'range': (26, 27), 'data': 0x44, 'ann': ''}, | |
43 | {'type': 'DR', 'range': (28, 29), 'data': 0x55, 'ann': ''}, | |
44 | {'type': 'DR', 'range': (30, 31), 'data': 0x66, 'ann': ''}, | |
45 | ||
46 | # STOP condition. | |
47 | {'type': 'P', 'range': (32, 33), 'data': None, 'ann': ''}, | |
48 | ] | |
49 | ||
50 | def decode(l): | |
51 | print(l) | |
52 | sigrok.put(l) | |
53 | ||
54 | ||
55 | def decode2(inbuf): | |
56 | """Nintendo Wii Nunchuk decoder""" | |
57 | ||
58 | # FIXME: Get the data in the correct format in the first place. | |
59 | inbuf = [ord(x) for x in inbuf] | |
60 | out = [] | |
61 | o = {} | |
62 | ||
63 | # TODO: Pass in metadata. | |
64 | ||
65 | # States | |
66 | IDLE, START, NUNCHUK_SLAVE, INIT, INITIALIZED = range(5) | |
67 | state = IDLE # TODO: Can we assume a certain initial state? | |
68 | ||
69 | sx = sy = ax = ay = az = bz = bc = 0 | |
70 | ||
71 | databytecount = 0 | |
72 | ||
73 | # Loop over all I2C packets. | |
74 | for p in example_packets: | |
75 | if p['type'] == 'S': # TODO: Handle 'Sr' here, too? | |
76 | state = START | |
77 | ||
78 | elif p['type'] == 'Sr': | |
79 | pass # FIXME | |
80 | ||
81 | elif p['type'] == 'AR': | |
82 | # TODO: Error/Warning, not supported, I think. | |
83 | pass | |
84 | ||
85 | elif p['type'] == 'AW': | |
86 | # The Wii Nunchuk always has slave address 0x54. | |
87 | # TODO: Handle this stuff more correctly. | |
88 | if p['data'] == 0x54: | |
89 | pass # TODO | |
90 | else: | |
91 | pass # TODO: What to do here? Ignore? Error? | |
92 | ||
93 | elif p['type'] == 'DR' and state == INITIALIZED: | |
94 | if databytecount == 0: | |
95 | sx = p['data'] | |
96 | elif databytecount == 1: | |
97 | sy = p['data'] | |
98 | elif databytecount == 2: | |
99 | ax = p['data'] << 2 | |
100 | elif databytecount == 3: | |
101 | ay = p['data'] << 2 | |
102 | elif databytecount == 4: | |
103 | az = p['data'] << 2 | |
104 | elif databytecount == 5: | |
105 | bz = (p['data'] & (1 << 0)) >> 0 | |
106 | bc = (p['data'] & (1 << 1)) >> 1 | |
107 | ax |= (p['data'] & (3 << 2)) >> 2 | |
108 | ay |= (p['data'] & (3 << 4)) >> 4 | |
109 | az |= (p['data'] & (3 << 6)) >> 6 | |
110 | # del o | |
111 | o = {'type': 'D', 'range': (0, 0), 'data': []} | |
112 | o['data'] = [sx, sy, ax, ay, az, bz, bc] | |
113 | # sx = sy = ax = ay = az = bz = bc = 0 | |
114 | else: | |
115 | pass # TODO | |
116 | ||
117 | if 0 <= databytecount <= 5: | |
118 | databytecount += 1 | |
119 | ||
120 | # TODO: If 6 bytes read -> save and reset | |
121 | ||
122 | # TODO | |
123 | elif p['type'] == 'DR' and state != INITIALIZED: | |
124 | pass | |
125 | ||
126 | elif p['type'] == 'DW': | |
127 | if p['data'] == 0x40 and state == START: | |
128 | state = INIT | |
129 | elif p['data'] == 0x00 and state == INIT: | |
130 | o = {'type': 'I', 'range': (0, 0), 'data': []} | |
131 | o['data'] = [0x40, 0x00] | |
132 | out.append(o) | |
133 | state = INITIALIZED | |
134 | else: | |
135 | pass # TODO | |
136 | ||
137 | elif p['type'] == 'P': | |
138 | out.append(o) | |
139 | state = INITIALIZED | |
140 | databytecount = 0 | |
141 | ||
142 | print out | |
143 | ||
144 | # FIXME | |
145 | return '' | |
146 | ||
147 | register = { | |
148 | 'id': 'nunchuk', | |
149 | 'name': 'Nunchuk', | |
150 | 'longname': 'Nintendo Wii Nunchuk decoder', | |
151 | 'desc': 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.', | |
152 | 'longdesc': '...', | |
153 | 'author': 'Uwe Hermann', | |
154 | 'email': 'uwe@hermann-uwe.de', | |
155 | 'license': 'gplv2+', | |
156 | 'in': ['i2c'], | |
157 | 'out': ['nunchuck'], | |
158 | 'probes': [ | |
159 | # TODO | |
160 | ], | |
161 | 'options': { | |
162 | # TODO | |
163 | }, | |
164 | # 'start': start, | |
165 | # 'report': report, | |
166 | } | |
167 | ||
168 | # Use psyco (if available) as it results in huge performance improvements. | |
169 | try: | |
170 | import psyco | |
171 | psyco.bind(decode) | |
172 | except ImportError: | |
173 | pass | |
174 |