]> sigrok.org Git - libsigrokdecode.git/blob - decoders/nunchuk/nunchuk.py
srd: Wii Nunchuk: Complete rewrite, PD works now.
[libsigrokdecode.git] / decoders / nunchuk / nunchuk.py
1 ##
2 ## This file is part of the sigrok 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     ]
41
42     def __init__(self, **kwargs):
43         self.state = 'IDLE'
44         self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
45         self.databytecount = 0
46         self.reg = 0x00
47
48     def start(self, metadata):
49         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
50         self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
51
52     def report(self):
53         pass
54
55     def handle_reg_0x00(self, databyte):
56         self.sx = databyte
57         self.put(0, 0, self.out_ann,
58                  [0, ['Analog stick X position: 0x%02x' % self.sx]])
59         self.put(0, 0, self.out_ann, [1, ['SX: 0x%02x' % self.sx]])
60
61     def handle_reg_0x01(self, databyte):
62         self.sy = databyte
63         self.put(0, 0, self.out_ann,
64                  [0, ['Analog stick Y position: 0x%02x' % self.sy]])
65         self.put(0, 0, self.out_ann, [1, ['SY: 0x%02x' % self.sy]])
66
67     def handle_reg_0x02(self, databyte):
68         self.ax = databyte << 2
69         self.put(0, 0, self.out_ann,
70                  [0, ['Accelerometer X value bits[9:2]: 0x%03x' % self.ax]])
71         self.put(0, 0, self.out_ann, [1, ['AX[9:2]: 0x%03x' % self.ax]])
72
73     def handle_reg_0x03(self, databyte):
74         self.ay = databyte << 2
75         self.put(0, 0, self.out_ann,
76                  [0, ['Accelerometer Y value bits[9:2]: 0x%03x' % self.ay]])
77         self.put(0, 0, self.out_ann, [1, ['AY[9:2]: 0x%x' % self.ay]])
78
79     def handle_reg_0x04(self, databyte):
80         self.az = databyte << 2
81         self.put(0, 0, self.out_ann,
82                  [0, ['Accelerometer Z value bits[9:2]: 0x%03x' % self.az]])
83         self.put(0, 0, self.out_ann, [1, ['AZ[9:2]: 0x%x' % self.az]])
84
85     # TODO: Bit-exact annotations.
86     def handle_reg_0x05(self, databyte):
87         self.bz = (databyte & (1 << 0)) >> 0 # Bits[0:0]
88         self.bc = (databyte & (1 << 1)) >> 1 # Bits[1:1]
89         ax_rest = (databyte & (3 << 2)) >> 2 # Bits[3:2]
90         ay_rest = (databyte & (3 << 4)) >> 4 # Bits[5:4]
91         az_rest = (databyte & (3 << 6)) >> 6 # Bits[7:6]
92         self.ax |= ax_rest
93         self.ay |= ay_rest
94         self.az |= az_rest
95
96         s = '' if (self.bz == 0) else 'not '
97         self.put(0, 0, self.out_ann, [0, ['Z button: %spressed' % s]])
98         self.put(0, 0, self.out_ann, [1, ['BZ: %d' % self.bz]])
99
100         s = '' if (self.bc == 0) else 'not '
101         self.put(0, 0, self.out_ann, [0, ['C button: %spressed' % s]])
102         self.put(0, 0, self.out_ann, [1, ['BC: %d' % self.bc]])
103
104         self.put(0, 0, self.out_ann,
105                  [0, ['Accelerometer X value bits[1:0]: 0x%03x' % ax_rest]])
106         self.put(0, 0, self.out_ann, [1, ['AX[1:0]: 0x%x' % ax_rest]])
107
108         self.put(0, 0, self.out_ann,
109                  [0, ['Accelerometer Y value bits[1:0]: 0x%03x' % ay_rest]])
110         self.put(0, 0, self.out_ann, [1, ['AY[1:0]: 0x%x' % ay_rest]])
111
112         self.put(0, 0, self.out_ann,
113                  [0, ['Accelerometer Z value bits[1:0]: 0x%03x' % az_rest]])
114         self.put(0, 0, self.out_ann, [1, ['AZ[1:0]: 0x%x' % az_rest]])
115
116     def decode(self, ss, es, data):
117         cmd, databyte = data
118
119         # Store the start/end samples of this I2C packet.
120         self.ss, self.es = ss, es
121
122         # State machine.
123         if self.state == 'IDLE':
124             # Wait for an I2C START condition.
125             if cmd != 'START':
126                 return
127             self.state = 'GET SLAVE ADDR'
128             self.block_start_sample = ss
129         elif self.state == 'GET SLAVE ADDR':
130             # Wait for an address read operation.
131             if cmd != 'ADDRESS READ':
132                 return
133             self.state = 'READ REGS'
134         elif self.state == 'READ REGS':
135             if cmd == 'DATA READ':
136                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
137                 handle_reg(databyte)
138                 self.reg += 1
139             elif cmd == 'STOP':
140                 self.block_end_sample = es
141
142                 # TODO: Only works if host reads _all_ regs (0x00 - 0x05).
143                 d = 'SX = 0x%02x, SY = 0x%02x, AX = 0x%02x, AY = 0x%02x, ' \
144                     'AZ = 0x%02x, BZ = 0x%02x, BC = 0x%02x' % (self.sx, \
145                     self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
146                 self.put(self.block_start_sample, self.block_end_sample,
147                          self.out_ann, [0, [d]])
148
149                 self.sx = self.sy = self.ax = self.ay = self.az = 0
150                 self.bz = self.bc = 0
151
152                 self.state = 'IDLE'
153             else:
154                 # self.put(0, 0, self.out_ann,
155                 #          [0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]])
156                 pass
157         else:
158             raise Exception('Invalid state: %s' % self.state)
159