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