]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2010-2012 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 | # Nintendo Wii Nunchuk protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
25 | class Decoder(srd.Decoder): | |
26 | api_version = 1 | |
27 | id = 'nunchuk' | |
28 | name = 'Nunchuk' | |
29 | longname = 'Nintendo Wii Nunchuk' | |
30 | desc = 'Nintendo Wii Nunchuk controller protocol.' | |
31 | license = 'gplv2+' | |
32 | inputs = ['i2c'] | |
33 | outputs = ['nunchuck'] | |
34 | probes = [] | |
35 | optional_probes = [] | |
36 | options = {} | |
37 | annotations = [ | |
38 | ['Text (verbose)', 'Human-readable text (verbose)'], | |
39 | ['Text', 'Human-readable text'], | |
40 | ['Warnings', 'Human-readable warnings'], | |
41 | ] | |
42 | ||
43 | def __init__(self, **kwargs): | |
44 | self.state = 'IDLE' | |
45 | self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = -1 | |
46 | self.databytecount = 0 | |
47 | self.reg = 0x00 | |
48 | self.init_seq = [] | |
49 | ||
50 | def start(self): | |
51 | # self.out_proto = self.register(srd.OUTPUT_PYTHON) | |
52 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
53 | ||
54 | def putx(self, data): | |
55 | # Helper for annotations which span exactly one I2C packet. | |
56 | self.put(self.ss, self.es, self.out_ann, data) | |
57 | ||
58 | def putb(self, data): | |
59 | # Helper for annotations which span a block of I2C packets. | |
60 | self.put(self.block_start_sample, self.block_end_sample, | |
61 | self.out_ann, data) | |
62 | ||
63 | def handle_reg_0x00(self, databyte): | |
64 | self.sx = databyte | |
65 | self.putx([0, ['Analog stick X position: 0x%02x' % self.sx]]) | |
66 | self.putx([1, ['SX: 0x%02x' % self.sx]]) | |
67 | ||
68 | def handle_reg_0x01(self, databyte): | |
69 | self.sy = databyte | |
70 | self.putx([0, ['Analog stick Y position: 0x%02x' % self.sy]]) | |
71 | self.putx([1, ['SY: 0x%02x' % self.sy]]) | |
72 | ||
73 | def handle_reg_0x02(self, databyte): | |
74 | self.ax = databyte << 2 | |
75 | self.putx([0, ['Accelerometer X value bits[9:2]: 0x%03x' % self.ax]]) | |
76 | self.putx([1, ['AX[9:2]: 0x%03x' % self.ax]]) | |
77 | ||
78 | def handle_reg_0x03(self, databyte): | |
79 | self.ay = databyte << 2 | |
80 | self.putx([0, ['Accelerometer Y value bits[9:2]: 0x%03x' % self.ay]]) | |
81 | self.putx([1, ['AY[9:2]: 0x%x' % self.ay]]) | |
82 | ||
83 | def handle_reg_0x04(self, databyte): | |
84 | self.az = databyte << 2 | |
85 | self.putx([0, ['Accelerometer Z value bits[9:2]: 0x%03x' % self.az]]) | |
86 | self.putx([1, ['AZ[9:2]: 0x%x' % self.az]]) | |
87 | ||
88 | # TODO: Bit-exact annotations. | |
89 | def handle_reg_0x05(self, databyte): | |
90 | self.bz = (databyte & (1 << 0)) >> 0 # Bits[0:0] | |
91 | self.bc = (databyte & (1 << 1)) >> 1 # Bits[1:1] | |
92 | ax_rest = (databyte & (3 << 2)) >> 2 # Bits[3:2] | |
93 | ay_rest = (databyte & (3 << 4)) >> 4 # Bits[5:4] | |
94 | az_rest = (databyte & (3 << 6)) >> 6 # Bits[7:6] | |
95 | self.ax |= ax_rest | |
96 | self.ay |= ay_rest | |
97 | self.az |= az_rest | |
98 | ||
99 | s = '' if (self.bz == 0) else 'not ' | |
100 | self.putx([0, ['Z button: %spressed' % s]]) | |
101 | self.putx([1, ['BZ: %d' % self.bz]]) | |
102 | ||
103 | s = '' if (self.bc == 0) else 'not ' | |
104 | self.putx([0, ['C button: %spressed' % s]]) | |
105 | self.putx([1, ['BC: %d' % self.bc]]) | |
106 | ||
107 | self.putx([0, ['Accelerometer X value bits[1:0]: 0x%x' % ax_rest]]) | |
108 | self.putx([1, ['AX[1:0]: 0x%x' % ax_rest]]) | |
109 | ||
110 | self.putx([0, ['Accelerometer Y value bits[1:0]: 0x%x' % ay_rest]]) | |
111 | self.putx([1, ['AY[1:0]: 0x%x' % ay_rest]]) | |
112 | ||
113 | self.putx([0, ['Accelerometer Z value bits[1:0]: 0x%x' % az_rest]]) | |
114 | self.putx([1, ['AZ[1:0]: 0x%x' % az_rest]]) | |
115 | ||
116 | def output_full_block_if_possible(self): | |
117 | # For now, only output summary annotation if all values are available. | |
118 | t = (self.sx, self.sy, self.ax, self.ay, self.az, self.bz, self.bc) | |
119 | if -1 in t: | |
120 | return | |
121 | ||
122 | s = 'Analog stick X position: 0x%02x\n' % self.sx | |
123 | s += 'Analog stick Y position: 0x%02x\n' % self.sy | |
124 | s += 'Z button: %spressed\n' % ('' if (self.bz == 0) else 'not ') | |
125 | s += 'C button: %spressed\n' % ('' if (self.bc == 0) else 'not ') | |
126 | s += 'Accelerometer X value: 0x%03x\n' % self.ax | |
127 | s += 'Accelerometer Y value: 0x%03x\n' % self.ay | |
128 | s += 'Accelerometer Z value: 0x%03x\n' % self.az | |
129 | self.put(self.block_start_sample, self.block_end_sample, | |
130 | self.out_ann, [0, [s]]) | |
131 | ||
132 | s = 'SX = 0x%02x, SY = 0x%02x, AX = 0x%02x, AY = 0x%02x, ' \ | |
133 | 'AZ = 0x%02x, BZ = 0x%02x, BC = 0x%02x' % (self.sx, \ | |
134 | self.sy, self.ax, self.ay, self.az, self.bz, self.bc) | |
135 | self.put(self.block_start_sample, self.block_end_sample, | |
136 | self.out_ann, [1, [s]]) | |
137 | ||
138 | def handle_reg_write(self, databyte): | |
139 | self.putx([0, ['Nunchuk write: 0x%02x' % databyte]]) | |
140 | if len(self.init_seq) < 2: | |
141 | self.init_seq.append(databyte) | |
142 | ||
143 | def output_init_seq(self): | |
144 | if len(self.init_seq) != 2: | |
145 | self.putb([2, ['Init sequence was %d bytes long (2 expected)' % \ | |
146 | len(self.init_seq)]]) | |
147 | ||
148 | if self.init_seq != (0x40, 0x00): | |
149 | self.putb([2, ['Unknown init sequence (expected: 0x40 0x00)']]) | |
150 | ||
151 | # TODO: Detect Nunchuk clones (they have different init sequences). | |
152 | s = 'Initialized Nintendo Wii Nunchuk' | |
153 | ||
154 | self.putb([0, [s]]) | |
155 | self.putb([1, ['INIT']]) | |
156 | ||
157 | def decode(self, ss, es, data): | |
158 | cmd, databyte = data | |
159 | ||
160 | # Store the start/end samples of this I2C packet. | |
161 | self.ss, self.es = ss, es | |
162 | ||
163 | # State machine. | |
164 | if self.state == 'IDLE': | |
165 | # Wait for an I2C START condition. | |
166 | if cmd != 'START': | |
167 | return | |
168 | self.state = 'GET SLAVE ADDR' | |
169 | self.block_start_sample = ss | |
170 | elif self.state == 'GET SLAVE ADDR': | |
171 | # Wait for an address read/write operation. | |
172 | if cmd == 'ADDRESS READ': | |
173 | self.state = 'READ REGS' | |
174 | elif cmd == 'ADDRESS WRITE': | |
175 | self.state = 'WRITE REGS' | |
176 | elif self.state == 'READ REGS': | |
177 | if cmd == 'DATA READ': | |
178 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) | |
179 | handle_reg(databyte) | |
180 | self.reg += 1 | |
181 | elif cmd == 'STOP': | |
182 | self.block_end_sample = es | |
183 | self.output_full_block_if_possible() | |
184 | self.sx = self.sy = self.ax = self.ay = self.az = -1 | |
185 | self.bz = self.bc = -1 | |
186 | self.state = 'IDLE' | |
187 | else: | |
188 | # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]]) | |
189 | pass | |
190 | elif self.state == 'WRITE REGS': | |
191 | if cmd == 'DATA WRITE': | |
192 | self.handle_reg_write(databyte) | |
193 | elif cmd == 'STOP': | |
194 | self.block_end_sample = es | |
195 | self.output_init_seq() | |
196 | self.init_seq = [] | |
197 | self.state = 'IDLE' | |
198 | else: | |
199 | # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]]) | |
200 | pass | |
201 | else: | |
202 | raise Exception('Invalid state: %s' % self.state) | |
203 |