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