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