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