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