]> sigrok.org Git - libsigrokdecode.git/blame - decoders/nunchuk.py
srd: decoders: Metadata consistency fixes/updates.
[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
677d597b 33import sigrokdecode as srd
1c8ac5bf 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
677d597b 65class Decoder(srd.Decoder):
2b7d0e2b 66 id = 'nunchuk'
012cfd0d
UH
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']
1f24514c 76 probes = [] # TODO
012cfd0d 77 options = {}
9a12a6e7 78 annotations = []
012cfd0d
UH
79
80 def __init__(self, **kwargs):
bffd9bc0 81 self.state = IDLE # TODO: Can we assume a certain initial state?
012cfd0d
UH
82
83 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
84
85 self.databytecount = 0
86
87 def start(self, metadata):
56202222
UH
88 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
89 self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
012cfd0d
UH
90
91 def report(self):
92 pass
93
f6555179 94 def decode(self, timeoffset, duration, data):
012cfd0d
UH
95 out = []
96 o = {}
97
98 # We should accept a list of samples and iterate...
1f24514c 99 for p in example_packets: # TODO
012cfd0d
UH
100
101 # TODO: Eliminate the need for ord().
102 # s = ord(sample.data)
103
104 if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
bffd9bc0 105 self.state = START
012cfd0d
UH
106
107 elif p['type'] == 'Sr':
108 pass # FIXME
109
110 elif p['type'] == 'AR':
111 # TODO: Error/Warning, not supported, I think.
112 pass
113
114 elif p['type'] == 'AW':
115 # The Wii Nunchuk always has slave address 0x54.
116 # TODO: Handle this stuff more correctly.
117 if p['data'] == 0x54:
118 pass # TODO
119 else:
120 pass # TODO: What to do here? Ignore? Error?
121
bffd9bc0 122 elif p['type'] == 'DR' and self.state == INITIALIZED:
012cfd0d
UH
123 if self.databytecount == 0:
124 self.sx = p['data']
125 elif self.databytecount == 1:
126 self.sy = p['data']
127 elif self.databytecount == 2:
128 self.ax = p['data'] << 2
129 elif self.databytecount == 3:
130 self.ay = p['data'] << 2
131 elif self.databytecount == 4:
132 self.az = p['data'] << 2
133 elif self.databytecount == 5:
134 self.bz = (p['data'] & (1 << 0)) >> 0
135 self.bc = (p['data'] & (1 << 1)) >> 1
136 self.ax |= (p['data'] & (3 << 2)) >> 2
137 self.ay |= (p['data'] & (3 << 4)) >> 4
138 self.az |= (p['data'] & (3 << 6)) >> 6
139 # del o
140 o = {'type': 'D', 'range': (0, 0), 'data': []}
141 o['data'] = [self.sx, self.sy, self.ax, self.ay, \
142 self.az, self.bz, self.bc]
143 # sx = sy = ax = ay = az = bz = bc = 0
144 else:
145 pass # TODO
146
147 if 0 <= self.databytecount <= 5:
148 self.databytecount += 1
149
150 # TODO: If 6 bytes read -> save and reset
151
152 # TODO
bffd9bc0 153 elif p['type'] == 'DR' and self.state != INITIALIZED:
012cfd0d
UH
154 pass
155
156 elif p['type'] == 'DW':
bffd9bc0
UH
157 if p['data'] == 0x40 and self.state == START:
158 self.state = INIT
159 elif p['data'] == 0x00 and self.state == INIT:
012cfd0d
UH
160 o = {'type': 'I', 'range': (0, 0), 'data': []}
161 o['data'] = [0x40, 0x00]
162 out.append(o)
bffd9bc0 163 self.state = INITIALIZED
012cfd0d
UH
164 else:
165 pass # TODO
166
167 elif p['type'] == 'P':
ad2dc0de 168 out.append(o)
bffd9bc0 169 self.state = INITIALIZED
012cfd0d
UH
170 self.databytecount = 0
171
f6555179 172 if out != []:
c9b24fc3
UH
173 # self.put(0, 0, self.out_proto, out_proto)
174 self.put(0, 0, self.out_ann, out)
2b7d0e2b 175