]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/nunchuk/nunchuk.py
srd: Move all protocol docs to __init__.py files.
[libsigrokdecode.git] / decoders / nunchuk / nunchuk.py
... / ...
CommitLineData
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2010-2012 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# Nintendo Wii Nunchuk protocol decoder
22
23import sigrokdecode as srd
24
25# States
26IDLE = 0
27START = 1
28NUNCHUK_SLAVE = 2
29INIT = 3
30INITIALIZED = 4
31
32class Decoder(srd.Decoder):
33 api_version = 1
34 id = 'nunchuk'
35 name = 'Nunchuk'
36 longname = 'Nintendo Wii Nunchuk'
37 desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
38 longdesc = '...'
39 license = 'gplv2+'
40 inputs = ['i2c']
41 outputs = ['nunchuck']
42 probes = []
43 optional_probes = [] # TODO
44 options = {}
45 annotations = [
46 ['TODO', 'TODO'],
47 ]
48
49 def __init__(self, **kwargs):
50 self.state = IDLE # TODO: Can we assume a certain initial state?
51 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
52 self.databytecount = 0
53
54 def start(self, metadata):
55 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
56 self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
57
58 def report(self):
59 pass
60
61 def decode(self, ss, es, data):
62
63 cmd, databyte, ack_bit = data
64
65 if cmd == 'START': # TODO: Handle 'Sr' here, too?
66 self.state = START
67
68 elif cmd == 'START REPEAT':
69 pass # FIXME
70
71 elif cmd == 'ADDRESS READ':
72 # TODO: Error/Warning, not supported, I think.
73 pass
74
75 elif cmd == 'ADDRESS WRITE':
76 # The Wii Nunchuk always has slave address 0x54.
77 # TODO: Handle this stuff more correctly.
78 if databyte == 0x54:
79 pass # TODO
80 else:
81 pass # TODO: What to do here? Ignore? Error?
82
83 elif cmd == 'DATA READ' and self.state == INITIALIZED:
84 if self.databytecount == 0:
85 self.sx = databyte
86 elif self.databytecount == 1:
87 self.sy = databyte
88 elif self.databytecount == 2:
89 self.ax = databyte << 2
90 elif self.databytecount == 3:
91 self.ay = databyte << 2
92 elif self.databytecount == 4:
93 self.az = databyte << 2
94 elif self.databytecount == 5:
95 self.bz = (databyte & (1 << 0)) >> 0
96 self.bc = (databyte & (1 << 1)) >> 1
97 self.ax |= (databyte & (3 << 2)) >> 2
98 self.ay |= (databyte & (3 << 4)) >> 4
99 self.az |= (databyte & (3 << 6)) >> 6
100
101 d = 'sx = 0x%02x, sy = 0x%02x, ax = 0x%02x, ay = 0x%02x, ' \
102 'az = 0x%02x, bz = 0x%02x, bc = 0x%02x' % (self.sx, \
103 self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
104 self.put(ss, es, self.out_ann, [0, [d]])
105
106 self.sx = self.sy = self.ax = self.ay = self.az = 0
107 self.bz = self.bc = 0
108 else:
109 pass # TODO
110
111 if 0 <= self.databytecount <= 5:
112 self.databytecount += 1
113
114 # TODO: If 6 bytes read -> save and reset
115
116 # TODO
117 elif cmd == 'DATA READ' and self.state != INITIALIZED:
118 pass
119
120 elif cmd == 'DATA WRITE':
121 if self.state == IDLE:
122 self.state = INITIALIZED
123 return
124
125 if databyte == 0x40 and self.state == START:
126 self.state = INIT
127 elif databyte == 0x00 and self.state == INIT:
128 self.put(ss, es, self.out_ann, [0, ['Initialize nunchuk']])
129 self.state = INITIALIZED
130 else:
131 pass # TODO
132
133 elif cmd == 'STOP':
134 self.state = INITIALIZED
135 self.databytecount = 0
136