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