]> sigrok.org Git - libsigrokdecode.git/blob - decoders/nunchuk.py
Decoder.output_new() now takes an extra protocol_id argument.
[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 import sigrokdecode
34
35 # States
36 IDLE = 0
37 START = 1
38 NUNCHUK_SLAVE = 2
39 INIT = 3
40 INITIALIZED = 4
41
42 # FIXME: This is just some example input for testing purposes...
43 example_packets = [
44     # START condition.
45     {'type': 'S',  'range': (10, 11), 'data': None, 'ann': ''},
46
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': ''},
52
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': ''},
60
61     # STOP condition.
62     {'type': 'P',  'range': (32, 33), 'data': None, 'ann': ''},
63 ]
64
65 class Decoder(sigrokdecode.Decoder):
66     id = 'nunchuk'
67     name = 'Nunchuk'
68     longname = 'Nintendo Wii Nunchuk decoder'
69     desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
70     longdesc = '...'
71     author = 'Uwe Hermann'
72     email = 'uwe@hermann-uwe.de'
73     license = 'gplv2+'
74     inputs = ['i2c']
75     outputs = ['nunchuck']
76     probes = [] # TODO
77     options = {}
78
79     def __init__(self, **kwargs):
80         self.output_protocol = None
81         self.output_annotation = None
82
83         self.state = 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.output_protocol = self.output_new(2)
91         self.output_annotation = self.output_new(1)
92
93     def report(self):
94         pass
95
96     def decode(self, timeoffset, duration, data):
97         out = []
98         o = {}
99
100         # We should accept a list of samples and iterate...
101         for p in example_packets: # TODO
102
103             # TODO: Eliminate the need for ord().
104             # s = ord(sample.data)
105
106             if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
107                 self.state = START
108
109             elif p['type'] == 'Sr':
110                 pass # FIXME
111
112             elif p['type'] == 'AR':
113                 # TODO: Error/Warning, not supported, I think.
114                 pass
115
116             elif p['type'] == 'AW':
117                 # The Wii Nunchuk always has slave address 0x54.
118                 # TODO: Handle this stuff more correctly.
119                 if p['data'] == 0x54:
120                     pass # TODO
121                 else:
122                     pass # TODO: What to do here? Ignore? Error?
123
124             elif p['type'] == 'DR' and self.state == INITIALIZED:
125                 if self.databytecount == 0:
126                     self.sx = p['data']
127                 elif self.databytecount == 1:
128                     self.sy = p['data']
129                 elif self.databytecount == 2:
130                     self.ax = p['data'] << 2
131                 elif self.databytecount == 3:
132                     self.ay = p['data'] << 2
133                 elif self.databytecount == 4:
134                     self.az = p['data'] << 2
135                 elif self.databytecount == 5:
136                     self.bz =  (p['data'] & (1 << 0)) >> 0
137                     self.bc =  (p['data'] & (1 << 1)) >> 1
138                     self.ax |= (p['data'] & (3 << 2)) >> 2
139                     self.ay |= (p['data'] & (3 << 4)) >> 4
140                     self.az |= (p['data'] & (3 << 6)) >> 6
141                     # del o
142                     o = {'type': 'D', 'range': (0, 0), 'data': []}
143                     o['data'] = [self.sx, self.sy, self.ax, self.ay, \
144                                  self.az, self.bz, self.bc]
145                     # sx = sy = ax = ay = az = bz = bc = 0
146                 else:
147                     pass # TODO
148
149                 if 0 <= self.databytecount <= 5:
150                     self.databytecount += 1
151
152                 # TODO: If 6 bytes read -> save and reset
153
154             # TODO
155             elif p['type'] == 'DR' and self.state != INITIALIZED:
156                 pass
157
158             elif p['type'] == 'DW':
159                 if p['data'] == 0x40 and self.state == START:
160                     self.state = INIT
161                 elif p['data'] == 0x00 and self.state == INIT:
162                     o = {'type': 'I', 'range': (0, 0), 'data': []}
163                     o['data'] = [0x40, 0x00]
164                     out.append(o)
165                     self.state = INITIALIZED
166                 else:
167                     pass # TODO
168
169             elif p['type'] == 'P':
170                 out.append(o)
171                 self.state = INITIALIZED
172                 self.databytecount = 0
173
174         if out != []:
175             # self.put(0, 0, self.output_protocol, out_proto)
176             self.put(0, 0, self.output_annotation, out)
177