2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2010 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
22 # Nintendo Wii Nunchuk decoder
28 # http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck
29 # http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
30 # https://www.sparkfun.com/products/9281
42 # FIXME: This is just some example input for testing purposes...
45 {'type': 'S', 'range': (10, 11), 'data': None, 'ann': ''},
47 # Nunchuk init: Write 0x40,0x00 to slave address 0x54.
48 {'type': 'AW', 'range': (12, 13), 'data': 0x54, 'ann': ''},
49 {'type': 'DW', 'range': (14, 15), 'data': 0x40, 'ann': ''},
50 {'type': 'AW', 'range': (16, 17), 'data': 0x54, 'ann': ''},
51 {'type': 'DW', 'range': (18, 19), 'data': 0x00, 'ann': ''},
53 # Get data: Read 6 bytes of data.
54 {'type': 'DR', 'range': (20, 21), 'data': 0x11, 'ann': ''},
55 {'type': 'DR', 'range': (22, 23), 'data': 0x22, 'ann': ''},
56 {'type': 'DR', 'range': (24, 25), 'data': 0x33, 'ann': ''},
57 {'type': 'DR', 'range': (26, 27), 'data': 0x44, 'ann': ''},
58 {'type': 'DR', 'range': (28, 29), 'data': 0x55, 'ann': ''},
59 {'type': 'DR', 'range': (30, 31), 'data': 0x66, 'ann': ''},
62 {'type': 'P', 'range': (32, 33), 'data': None, 'ann': ''},
66 def __init__(self, data):
68 def probe(self, probe):
69 s = self.data[int(probe / 8)] & (1 << (probe % 8))
70 return True if s else False
72 def sampleiter(data, unitsize):
73 for i in range(0, len(data), unitsize):
74 yield(Sample(data[i:i+unitsize]))
76 class Decoder(sigrok.Decoder):
79 longname = 'Nintendo Wii Nunchuk decoder'
80 desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
82 author = 'Uwe Hermann'
83 email = 'uwe@hermann-uwe.de'
86 outputs = ['nunchuck']
90 def __init__(self, **kwargs):
91 self.probes = Decoder.probes.copy()
92 self.output_protocol = None
93 self.output_annotation = None
95 # TODO: Don't hardcode the number of channels.
98 self.state = IDLE # TODO: Can we assume a certain initial state?
100 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
102 self.databytecount = 0
104 def start(self, metadata):
105 self.unitsize = metadata['unitsize']
106 # self.output_protocol = self.output_new(2)
107 self.output_annotation = self.output_new(1)
112 def decode(self, timeoffset, duration, data):
116 # We should accept a list of samples and iterate...
117 # for sample in sampleiter(data['data'], self.unitsize):
118 for p in example_packets:
120 # TODO: Eliminate the need for ord().
121 # s = ord(sample.data)
123 if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
126 elif p['type'] == 'Sr':
129 elif p['type'] == 'AR':
130 # TODO: Error/Warning, not supported, I think.
133 elif p['type'] == 'AW':
134 # The Wii Nunchuk always has slave address 0x54.
135 # TODO: Handle this stuff more correctly.
136 if p['data'] == 0x54:
139 pass # TODO: What to do here? Ignore? Error?
141 elif p['type'] == 'DR' and self.state == INITIALIZED:
142 if self.databytecount == 0:
144 elif self.databytecount == 1:
146 elif self.databytecount == 2:
147 self.ax = p['data'] << 2
148 elif self.databytecount == 3:
149 self.ay = p['data'] << 2
150 elif self.databytecount == 4:
151 self.az = p['data'] << 2
152 elif self.databytecount == 5:
153 self.bz = (p['data'] & (1 << 0)) >> 0
154 self.bc = (p['data'] & (1 << 1)) >> 1
155 self.ax |= (p['data'] & (3 << 2)) >> 2
156 self.ay |= (p['data'] & (3 << 4)) >> 4
157 self.az |= (p['data'] & (3 << 6)) >> 6
159 o = {'type': 'D', 'range': (0, 0), 'data': []}
160 o['data'] = [self.sx, self.sy, self.ax, self.ay, \
161 self.az, self.bz, self.bc]
162 # sx = sy = ax = ay = az = bz = bc = 0
166 if 0 <= self.databytecount <= 5:
167 self.databytecount += 1
169 # TODO: If 6 bytes read -> save and reset
172 elif p['type'] == 'DR' and self.state != INITIALIZED:
175 elif p['type'] == 'DW':
176 if p['data'] == 0x40 and self.state == START:
178 elif p['data'] == 0x00 and self.state == INIT:
179 o = {'type': 'I', 'range': (0, 0), 'data': []}
180 o['data'] = [0x40, 0x00]
182 self.state = INITIALIZED
186 elif p['type'] == 'P':
188 self.state = INITIALIZED
189 self.databytecount = 0
192 # self.put(self.output_protocol, 0, 0, out_proto)
193 self.put(self.output_annotation, 0, 0, out)