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