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