]> sigrok.org Git - libsigrokdecode.git/blob - decoders/nunchuk/nunchuk.py
srd: Each PD now has its own subdirectory.
[libsigrokdecode.git] / decoders / nunchuk / 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'
46     desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
47     longdesc = '...'
48     license = 'gplv2+'
49     inputs = ['i2c']
50     outputs = ['nunchuck']
51     probes = [] # TODO
52     options = {}
53     annotations = [
54         ['TODO', 'TODO'], 
55     ]
56
57     def __init__(self, **kwargs):
58         self.state = IDLE # TODO: Can we assume a certain initial state?
59         self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
60         self.databytecount = 0
61
62     def start(self, metadata):
63         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
64         self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
65
66     def report(self):
67         pass
68
69     def decode(self, ss, es, data):
70
71         cmd, databyte, ack_bit = data
72
73         if cmd == 'START': # TODO: Handle 'Sr' here, too?
74             self.state = START
75
76         elif cmd == 'START REPEAT':
77             pass # FIXME
78
79         elif cmd == 'ADDRESS READ':
80             # TODO: Error/Warning, not supported, I think.
81             pass
82
83         elif cmd == 'ADDRESS WRITE':
84             # The Wii Nunchuk always has slave address 0x54.
85             # TODO: Handle this stuff more correctly.
86             if databyte == 0x54:
87                 pass # TODO
88             else:
89                 pass # TODO: What to do here? Ignore? Error?
90
91         elif cmd == 'DATA READ' and self.state == INITIALIZED:
92             if self.databytecount == 0:
93                 self.sx = databyte
94             elif self.databytecount == 1:
95                 self.sy = databyte
96             elif self.databytecount == 2:
97                 self.ax = databyte << 2
98             elif self.databytecount == 3:
99                 self.ay = databyte << 2
100             elif self.databytecount == 4:
101                 self.az = databyte << 2
102             elif self.databytecount == 5:
103                 self.bz =  (databyte & (1 << 0)) >> 0
104                 self.bc =  (databyte & (1 << 1)) >> 1
105                 self.ax |= (databyte & (3 << 2)) >> 2
106                 self.ay |= (databyte & (3 << 4)) >> 4
107                 self.az |= (databyte & (3 << 6)) >> 6
108
109                 d = 'sx = 0x%02x, sy = 0x%02x, ax = 0x%02x, ay = 0x%02x, ' \
110                     'az = 0x%02x, bz = 0x%02x, bc = 0x%02x' % (self.sx, \
111                     self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
112                 self.put(ss, es, self.out_ann, [0, [d]])
113
114                 self.sx = self.sy = self.ax = self.ay = self.az = 0
115                 self.bz = self.bc = 0
116             else:
117                 pass # TODO
118
119             if 0 <= self.databytecount <= 5:
120                 self.databytecount += 1
121
122             # TODO: If 6 bytes read -> save and reset
123
124         # TODO
125         elif cmd == 'DATA READ' and self.state != INITIALIZED:
126             pass
127
128         elif cmd == 'DATA WRITE':
129             if self.state == IDLE:
130                 self.state = INITIALIZED
131             return
132     
133             if databyte == 0x40 and self.state == START:
134                 self.state = INIT
135             elif databyte == 0x00 and self.state == INIT:
136                 self.put(ss, es, self.out_ann, [0, ['Initialize nunchuk']])
137                 self.state = INITIALIZED
138             else:
139                 pass # TODO
140
141         elif cmd == 'STOP':
142             self.state = INITIALIZED
143             self.databytecount = 0
144