2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 import sigrokdecode as srd
23 class Decoder(srd.Decoder):
27 longname = 'Nintendo Wii Nunchuk'
28 desc = 'Nintendo Wii Nunchuk controller protocol.'
31 outputs = ['nunchuck']
33 ['text-verbose', 'Human-readable text (verbose)'],
34 ['text', 'Human-readable text'],
35 ['warnings', 'Human-readable warnings'],
38 def __init__(self, **kwargs):
40 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = -1
41 self.databytecount = 0
46 # self.out_python = self.register(srd.OUTPUT_PYTHON)
47 self.out_ann = self.register(srd.OUTPUT_ANN)
50 # Helper for annotations which span exactly one I²C packet.
51 self.put(self.ss, self.es, self.out_ann, data)
54 # Helper for annotations which span a block of I²C packets.
55 self.put(self.block_start_sample, self.block_end_sample,
58 def handle_reg_0x00(self, databyte):
60 self.putx([0, ['Analog stick X position: 0x%02x' % self.sx]])
61 self.putx([1, ['SX: 0x%02x' % self.sx]])
63 def handle_reg_0x01(self, databyte):
65 self.putx([0, ['Analog stick Y position: 0x%02x' % self.sy]])
66 self.putx([1, ['SY: 0x%02x' % self.sy]])
68 def handle_reg_0x02(self, databyte):
69 self.ax = databyte << 2
70 self.putx([0, ['Accelerometer X value bits[9:2]: 0x%03x' % self.ax]])
71 self.putx([1, ['AX[9:2]: 0x%03x' % self.ax]])
73 def handle_reg_0x03(self, databyte):
74 self.ay = databyte << 2
75 self.putx([0, ['Accelerometer Y value bits[9:2]: 0x%03x' % self.ay]])
76 self.putx([1, ['AY[9:2]: 0x%x' % self.ay]])
78 def handle_reg_0x04(self, databyte):
79 self.az = databyte << 2
80 self.putx([0, ['Accelerometer Z value bits[9:2]: 0x%03x' % self.az]])
81 self.putx([1, ['AZ[9:2]: 0x%x' % self.az]])
83 # TODO: Bit-exact annotations.
84 def handle_reg_0x05(self, databyte):
85 self.bz = (databyte & (1 << 0)) >> 0 # Bits[0:0]
86 self.bc = (databyte & (1 << 1)) >> 1 # Bits[1:1]
87 ax_rest = (databyte & (3 << 2)) >> 2 # Bits[3:2]
88 ay_rest = (databyte & (3 << 4)) >> 4 # Bits[5:4]
89 az_rest = (databyte & (3 << 6)) >> 6 # Bits[7:6]
94 s = '' if (self.bz == 0) else 'not '
95 self.putx([0, ['Z button: %spressed' % s]])
96 self.putx([1, ['BZ: %d' % self.bz]])
98 s = '' if (self.bc == 0) else 'not '
99 self.putx([0, ['C button: %spressed' % s]])
100 self.putx([1, ['BC: %d' % self.bc]])
102 self.putx([0, ['Accelerometer X value bits[1:0]: 0x%x' % ax_rest]])
103 self.putx([1, ['AX[1:0]: 0x%x' % ax_rest]])
105 self.putx([0, ['Accelerometer Y value bits[1:0]: 0x%x' % ay_rest]])
106 self.putx([1, ['AY[1:0]: 0x%x' % ay_rest]])
108 self.putx([0, ['Accelerometer Z value bits[1:0]: 0x%x' % az_rest]])
109 self.putx([1, ['AZ[1:0]: 0x%x' % az_rest]])
111 def output_full_block_if_possible(self):
112 # For now, only output summary annotation if all values are available.
113 t = (self.sx, self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
117 s = 'Analog stick X position: 0x%02x\n' % self.sx
118 s += 'Analog stick Y position: 0x%02x\n' % self.sy
119 s += 'Z button: %spressed\n' % ('' if (self.bz == 0) else 'not ')
120 s += 'C button: %spressed\n' % ('' if (self.bc == 0) else 'not ')
121 s += 'Accelerometer X value: 0x%03x\n' % self.ax
122 s += 'Accelerometer Y value: 0x%03x\n' % self.ay
123 s += 'Accelerometer Z value: 0x%03x\n' % self.az
124 self.put(self.block_start_sample, self.block_end_sample,
125 self.out_ann, [0, [s]])
127 s = 'SX = 0x%02x, SY = 0x%02x, AX = 0x%02x, AY = 0x%02x, ' \
128 'AZ = 0x%02x, BZ = 0x%02x, BC = 0x%02x' % (self.sx, \
129 self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
130 self.put(self.block_start_sample, self.block_end_sample,
131 self.out_ann, [1, [s]])
133 def handle_reg_write(self, databyte):
134 self.putx([0, ['Nunchuk write: 0x%02x' % databyte]])
135 if len(self.init_seq) < 2:
136 self.init_seq.append(databyte)
138 def output_init_seq(self):
139 if len(self.init_seq) != 2:
140 self.putb([2, ['Init sequence was %d bytes long (2 expected)' % \
141 len(self.init_seq)]])
143 if self.init_seq != (0x40, 0x00):
144 self.putb([2, ['Unknown init sequence (expected: 0x40 0x00)']])
146 # TODO: Detect Nunchuk clones (they have different init sequences).
147 s = 'Initialized Nintendo Wii Nunchuk'
150 self.putb([1, ['INIT']])
152 def decode(self, ss, es, data):
155 # Store the start/end samples of this I²C packet.
156 self.ss, self.es = ss, es
159 if self.state == 'IDLE':
160 # Wait for an I²C START condition.
163 self.state = 'GET SLAVE ADDR'
164 self.block_start_sample = ss
165 elif self.state == 'GET SLAVE ADDR':
166 # Wait for an address read/write operation.
167 if cmd == 'ADDRESS READ':
168 self.state = 'READ REGS'
169 elif cmd == 'ADDRESS WRITE':
170 self.state = 'WRITE REGS'
171 elif self.state == 'READ REGS':
172 if cmd == 'DATA READ':
173 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
177 self.block_end_sample = es
178 self.output_full_block_if_possible()
179 self.sx = self.sy = self.ax = self.ay = self.az = -1
180 self.bz = self.bc = -1
183 # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]])
185 elif self.state == 'WRITE REGS':
186 if cmd == 'DATA WRITE':
187 self.handle_reg_write(databyte)
189 self.block_end_sample = es
190 self.output_init_seq()
194 # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]])
197 raise Exception('Invalid state: %s' % self.state)