]> sigrok.org Git - libsigrokdecode.git/blame - decoders/nunchuk/nunchuk.py
srd: nunchuk: Add verbose summary annotation.
[libsigrokdecode.git] / decoders / nunchuk / nunchuk.py
CommitLineData
cd0fc8c5
UH
1##
2## This file is part of the sigrok project.
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
156509ca 21# Nintendo Wii Nunchuk protocol decoder
cd0fc8c5 22
677d597b 23import sigrokdecode as srd
1c8ac5bf 24
677d597b 25class Decoder(srd.Decoder):
a2c2afd9 26 api_version = 1
2b7d0e2b 27 id = 'nunchuk'
012cfd0d 28 name = 'Nunchuk'
3d3da57d 29 longname = 'Nintendo Wii Nunchuk'
a465436e 30 desc = 'Nintendo Wii Nunchuk controller protocol.'
012cfd0d
UH
31 license = 'gplv2+'
32 inputs = ['i2c']
33 outputs = ['nunchuck']
decde15e 34 probes = []
5ea8b024 35 optional_probes = []
012cfd0d 36 options = {}
c0d7b38e 37 annotations = [
5ea8b024 38 ['Text (verbose)', 'Human-readable text (verbose)'],
ee3e279c 39 ['Text', 'Human-readable text'],
c0d7b38e 40 ]
012cfd0d
UH
41
42 def __init__(self, **kwargs):
5ea8b024 43 self.state = 'IDLE'
11860e5a 44 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = -1
012cfd0d 45 self.databytecount = 0
5ea8b024 46 self.reg = 0x00
012cfd0d
UH
47
48 def start(self, metadata):
56202222
UH
49 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
50 self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
012cfd0d
UH
51
52 def report(self):
53 pass
54
739f1b73
UH
55 def putx(self, data):
56 # Helper for annotations which span exactly one I2C packet.
57 self.put(self.ss, self.es, self.out_ann, data)
58
5ea8b024
UH
59 def handle_reg_0x00(self, databyte):
60 self.sx = databyte
739f1b73
UH
61 self.putx([0, ['Analog stick X position: 0x%02x' % self.sx]])
62 self.putx([1, ['SX: 0x%02x' % self.sx]])
5ea8b024
UH
63
64 def handle_reg_0x01(self, databyte):
65 self.sy = databyte
739f1b73
UH
66 self.putx([0, ['Analog stick Y position: 0x%02x' % self.sy]])
67 self.putx([1, ['SY: 0x%02x' % self.sy]])
5ea8b024
UH
68
69 def handle_reg_0x02(self, databyte):
70 self.ax = databyte << 2
739f1b73
UH
71 self.putx([0, ['Accelerometer X value bits[9:2]: 0x%03x' % self.ax]])
72 self.putx([1, ['AX[9:2]: 0x%03x' % self.ax]])
5ea8b024
UH
73
74 def handle_reg_0x03(self, databyte):
75 self.ay = databyte << 2
739f1b73
UH
76 self.putx([0, ['Accelerometer Y value bits[9:2]: 0x%03x' % self.ay]])
77 self.putx([1, ['AY[9:2]: 0x%x' % self.ay]])
5ea8b024
UH
78
79 def handle_reg_0x04(self, databyte):
80 self.az = databyte << 2
739f1b73
UH
81 self.putx([0, ['Accelerometer Z value bits[9:2]: 0x%03x' % self.az]])
82 self.putx([1, ['AZ[9:2]: 0x%x' % self.az]])
5ea8b024
UH
83
84 # TODO: Bit-exact annotations.
85 def handle_reg_0x05(self, databyte):
86 self.bz = (databyte & (1 << 0)) >> 0 # Bits[0:0]
87 self.bc = (databyte & (1 << 1)) >> 1 # Bits[1:1]
88 ax_rest = (databyte & (3 << 2)) >> 2 # Bits[3:2]
89 ay_rest = (databyte & (3 << 4)) >> 4 # Bits[5:4]
90 az_rest = (databyte & (3 << 6)) >> 6 # Bits[7:6]
91 self.ax |= ax_rest
92 self.ay |= ay_rest
93 self.az |= az_rest
94
95 s = '' if (self.bz == 0) else 'not '
739f1b73
UH
96 self.putx([0, ['Z button: %spressed' % s]])
97 self.putx([1, ['BZ: %d' % self.bz]])
5ea8b024
UH
98
99 s = '' if (self.bc == 0) else 'not '
739f1b73
UH
100 self.putx([0, ['C button: %spressed' % s]])
101 self.putx([1, ['BC: %d' % self.bc]])
5ea8b024 102
739f1b73
UH
103 self.putx([0, ['Accelerometer X value bits[1:0]: 0x%x' % ax_rest]])
104 self.putx([1, ['AX[1:0]: 0x%x' % ax_rest]])
5ea8b024 105
739f1b73
UH
106 self.putx([0, ['Accelerometer Y value bits[1:0]: 0x%x' % ay_rest]])
107 self.putx([1, ['AY[1:0]: 0x%x' % ay_rest]])
5ea8b024 108
739f1b73
UH
109 self.putx([0, ['Accelerometer Z value bits[1:0]: 0x%x' % az_rest]])
110 self.putx([1, ['AZ[1:0]: 0x%x' % az_rest]])
c0d7b38e 111
11860e5a 112 def output_full_block_if_possible(self):
11860e5a
UH
113 # For now, only output summary annotation if all values are available.
114 t = (self.sx, self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
115 if -1 in t:
116 return
117
d628cdb5
UH
118 s = 'Analog stick X position: 0x%02x\n' % self.sx
119 s += 'Analog stick Y position: 0x%02x\n' % self.sy
120 s += 'Z button: %spressed\n' % ('' if (self.bz == 0) else 'not ')
121 s += 'C button: %spressed\n' % ('' if (self.bc == 0) else 'not ')
122 s += 'Accelerometer X value: 0x%03x\n' % self.ax
123 s += 'Accelerometer Y value: 0x%03x\n' % self.ay
124 s += 'Accelerometer Z value: 0x%03x\n' % self.az
125 self.put(self.block_start_sample, self.block_end_sample,
126 self.out_ann, [0, [s]])
127
128 s = 'SX = 0x%02x, SY = 0x%02x, AX = 0x%02x, AY = 0x%02x, ' \
11860e5a
UH
129 'AZ = 0x%02x, BZ = 0x%02x, BC = 0x%02x' % (self.sx, \
130 self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
131 self.put(self.block_start_sample, self.block_end_sample,
d628cdb5 132 self.out_ann, [1, [s]])
11860e5a 133
5ea8b024 134 def decode(self, ss, es, data):
1b75abfd 135 cmd, databyte = data
c0d7b38e 136
5ea8b024
UH
137 # Store the start/end samples of this I2C packet.
138 self.ss, self.es = ss, es
139
140 # State machine.
141 if self.state == 'IDLE':
142 # Wait for an I2C START condition.
143 if cmd != 'START':
144 return
145 self.state = 'GET SLAVE ADDR'
146 self.block_start_sample = ss
147 elif self.state == 'GET SLAVE ADDR':
148 # Wait for an address read operation.
149 if cmd != 'ADDRESS READ':
150 return
151 self.state = 'READ REGS'
152 elif self.state == 'READ REGS':
153 if cmd == 'DATA READ':
154 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
155 handle_reg(databyte)
156 self.reg += 1
157 elif cmd == 'STOP':
158 self.block_end_sample = es
11860e5a 159 self.output_full_block_if_possible()
11860e5a
UH
160 self.sx = self.sy = self.ax = self.ay = self.az = -1
161 self.bz = self.bc = -1
5ea8b024 162 self.state = 'IDLE'
c0d7b38e 163 else:
739f1b73 164 # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]])
5ea8b024
UH
165 pass
166 else:
167 raise Exception('Invalid state: %s' % self.state)
2b7d0e2b 168