]> sigrok.org Git - libsigrokdecode.git/blob - decoders/nunchuk.py
e53d0de15683665bb38ebd1d6748e1aeefceae2e
[libsigrokdecode.git] / decoders / nunchuk.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2010 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 #
22 # Nintendo Wii Nunchuk decoder
23 #
24
25 #
26 # TODO: Description
27 #
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
31 #
32
33 # FIXME: This is just some example input for testing purposes...
34 example_packets = [
35     # START condition.
36     {'type': 'S',  'range': (10, 11), 'data': None, 'ann': ''},
37
38     # Nunchuk init: Write 0x40,0x00 to slave address 0x54.
39     {'type': 'AW', 'range': (12, 13), 'data': 0x54, 'ann': ''},
40     {'type': 'DW', 'range': (14, 15), 'data': 0x40, 'ann': ''},
41     {'type': 'AW', 'range': (16, 17), 'data': 0x54, 'ann': ''},
42     {'type': 'DW', 'range': (18, 19), 'data': 0x00, 'ann': ''},
43
44     # Get data: Read 6 bytes of data.
45     {'type': 'DR', 'range': (20, 21), 'data': 0x11, 'ann': ''},
46     {'type': 'DR', 'range': (22, 23), 'data': 0x22, 'ann': ''},
47     {'type': 'DR', 'range': (24, 25), 'data': 0x33, 'ann': ''},
48     {'type': 'DR', 'range': (26, 27), 'data': 0x44, 'ann': ''},
49     {'type': 'DR', 'range': (28, 29), 'data': 0x55, 'ann': ''},
50     {'type': 'DR', 'range': (30, 31), 'data': 0x66, 'ann': ''},
51
52     # STOP condition.
53     {'type': 'P',  'range': (32, 33), 'data': None, 'ann': ''},
54 ]
55
56 class Sample():
57     def __init__(self, data):
58         self.data = data
59     def probe(self, probe):
60         s = ord(self.data[probe / 8]) & (1 << (probe % 8))
61         return True if s else False
62
63 def sampleiter(data, unitsize):
64     for i in range(0, len(data), unitsize):
65         yield(Sample(data[i:i+unitsize]))
66
67 class Decoder():
68     id = 'nunchuk'
69     name = 'Nunchuk'
70     longname = 'Nintendo Wii Nunchuk decoder'
71     desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
72     longdesc = '...'
73     author = 'Uwe Hermann'
74     email = 'uwe@hermann-uwe.de'
75     license = 'gplv2+'
76     inputs = ['i2c']
77     outputs = ['nunchuck']
78     probes = {}
79     options = {}
80
81     def __init__(self, **kwargs):
82         self.probes = Decoder.probes.copy()
83
84         # TODO: Don't hardcode the number of channels.
85         self.channels = 8
86
87         self.IDLE, self.START, self.NUNCHUK_SLAVE, self.INIT, \
88         self.INITIALIZED = range(5)
89
90         self.state = self.IDLE # TODO: Can we assume a certain initial state?
91
92         self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
93
94         self.databytecount = 0
95
96     def start(self, metadata):
97         self.unitsize = metadata['unitsize']
98
99     def report(self):
100         pass
101
102     def decode(self, data):
103         """Nintendo Wii Nunchuk decoder"""
104
105         out = []
106         o = {}
107
108         # We should accept a list of samples and iterate...
109         # for sample in sampleiter(data['data'], self.unitsize):
110         for p in example_packets:
111
112             # TODO: Eliminate the need for ord().
113             # s = ord(sample.data)
114
115             if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
116                 self.state = self.START
117
118             elif p['type'] == 'Sr':
119                 pass # FIXME
120
121             elif p['type'] == 'AR':
122                 # TODO: Error/Warning, not supported, I think.
123                 pass
124
125             elif p['type'] == 'AW':
126                 # The Wii Nunchuk always has slave address 0x54.
127                 # TODO: Handle this stuff more correctly.
128                 if p['data'] == 0x54:
129                     pass # TODO
130                 else:
131                     pass # TODO: What to do here? Ignore? Error?
132
133             elif p['type'] == 'DR' and self.state == self.INITIALIZED:
134                 if self.databytecount == 0:
135                     self.sx = p['data']
136                 elif self.databytecount == 1:
137                     self.sy = p['data']
138                 elif self.databytecount == 2:
139                     self.ax = p['data'] << 2
140                 elif self.databytecount == 3:
141                     self.ay = p['data'] << 2
142                 elif self.databytecount == 4:
143                     self.az = p['data'] << 2
144                 elif self.databytecount == 5:
145                     self.bz =  (p['data'] & (1 << 0)) >> 0
146                     self.bc =  (p['data'] & (1 << 1)) >> 1
147                     self.ax |= (p['data'] & (3 << 2)) >> 2
148                     self.ay |= (p['data'] & (3 << 4)) >> 4
149                     self.az |= (p['data'] & (3 << 6)) >> 6
150                     # del o
151                     o = {'type': 'D', 'range': (0, 0), 'data': []}
152                     o['data'] = [self.sx, self.sy, self.ax, self.ay, \
153                                  self.az, self.bz, self.bc]
154                     # sx = sy = ax = ay = az = bz = bc = 0
155                 else:
156                     pass # TODO
157
158                 if 0 <= self.databytecount <= 5:
159                     self.databytecount += 1
160
161                 # TODO: If 6 bytes read -> save and reset
162
163             # TODO
164             elif p['type'] == 'DR' and self.state != self.INITIALIZED:
165                 pass
166
167             elif p['type'] == 'DW':
168                 if p['data'] == 0x40 and self.state == self.START:
169                     self.state = self.INIT
170                 elif p['data'] == 0x00 and self.state == self.INIT:
171                     o = {'type': 'I', 'range': (0, 0), 'data': []}
172                     o['data'] = [0x40, 0x00]
173                     out.append(o)
174                     self.state = self.INITIALIZED
175                 else:
176                     pass # TODO
177
178             elif p['type'] == 'P':
179                 out.append(o)
180                 self.state = self.INITIALIZED
181                 self.databytecount = 0
182
183         sigrok.put(out)
184
185 import sigrok
186
187 sigrok.register(Decoder)
188