]> sigrok.org Git - libsigrokdecode.git/blob - decoders/nunchuk.py
Rename the scripts/ directory to decoders/.
[libsigrokdecode.git] / decoders / nunchuk.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2010 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 # TODO: Description
26
27 # FIXME: This is just some example input for testing purposes...
28 example_packets = [
29         # START condition.
30         {'type': 'S',  'range': (10, 11), 'data': None, 'ann': ''},
31
32         # Nunchuk init: Write 0x40,0x00 to slave address 0x54.
33         {'type': 'AW', 'range': (12, 13), 'data': 0x54, 'ann': ''},
34         {'type': 'DW', 'range': (14, 15), 'data': 0x40, 'ann': ''},
35         {'type': 'AW', 'range': (16, 17), 'data': 0x54, 'ann': ''},
36         {'type': 'DW', 'range': (18, 19), 'data': 0x00, 'ann': ''},
37
38         # Get data: Read 6 bytes of data.
39         {'type': 'DR', 'range': (20, 21), 'data': 0x11, 'ann': ''},
40         {'type': 'DR', 'range': (22, 23), 'data': 0x22, 'ann': ''},
41         {'type': 'DR', 'range': (24, 25), 'data': 0x33, 'ann': ''},
42         {'type': 'DR', 'range': (26, 27), 'data': 0x44, 'ann': ''},
43         {'type': 'DR', 'range': (28, 29), 'data': 0x55, 'ann': ''},
44         {'type': 'DR', 'range': (30, 31), 'data': 0x66, 'ann': ''},
45
46         # STOP condition.
47         {'type': 'P',  'range': (32, 33), 'data': None, 'ann': ''},
48 ]
49
50 def decode(inbuf):
51         """Nintendo Wii Nunchuk decoder"""
52
53         # FIXME: Get the data in the correct format in the first place.
54         inbuf = [ord(x) for x in inbuf]
55         out = []
56         o = {}
57
58         # TODO: Pass in metadata.
59
60         # States
61         IDLE, START, NUNCHUK_SLAVE, INIT, INITIALIZED = range(5)
62         state = IDLE # TODO: Can we assume a certain initial state?
63
64         sx = sy = ax = ay = az = bz = bc = 0
65
66         databytecount = 0
67
68         # Loop over all I2C packets.
69         for p in example_packets:
70                 if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
71                         state = START
72
73                 elif p['type'] == 'Sr':
74                         pass # FIXME
75
76                 elif p['type'] == 'AR':
77                         # TODO: Error/Warning, not supported, I think.
78                         pass
79
80                 elif p['type'] == 'AW':
81                         # The Wii Nunchuk always has slave address 0x54.
82                         # TODO: Handle this stuff more correctly.
83                         if p['data'] == 0x54:
84                                 pass # TODO
85                         else:
86                                 pass # TODO: What to do here? Ignore? Error?
87
88                 elif p['type'] == 'DR' and state == INITIALIZED:
89                         if databytecount == 0:
90                                 sx = p['data']
91                         elif databytecount == 1:
92                                 sy = p['data']
93                         elif databytecount == 2:
94                                 ax = p['data'] << 2
95                         elif databytecount == 3:
96                                 ay = p['data'] << 2
97                         elif databytecount == 4:
98                                 az = p['data'] << 2
99                         elif databytecount == 5:
100                                 bz =  (p['data'] & (1 << 0)) >> 0
101                                 bc =  (p['data'] & (1 << 1)) >> 1
102                                 ax |= (p['data'] & (3 << 2)) >> 2
103                                 ay |= (p['data'] & (3 << 4)) >> 4
104                                 az |= (p['data'] & (3 << 6)) >> 6
105                                 # del o
106                                 o = {'type': 'D', 'range': (0, 0), 'data': []}
107                                 o['data'] = [sx, sy, ax, ay, az, bz, bc]
108                                 # sx = sy = ax = ay = az = bz = bc = 0
109                         else:
110                                 pass # TODO
111
112                         if 0 <= databytecount <= 5:
113                                 databytecount += 1
114
115                         # TODO: If 6 bytes read -> save and reset
116
117                 # TODO
118                 elif p['type'] == 'DR' and state != INITIALIZED:
119                         pass
120
121                 elif p['type'] == 'DW':
122                         if p['data'] == 0x40 and state == START:
123                                 state = INIT
124                         elif p['data'] == 0x00 and state == INIT:
125                                 o = {'type': 'I', 'range': (0, 0), 'data': []}
126                                 o['data'] = [0x40, 0x00]
127                                 out.append(o)
128                                 state = INITIALIZED
129                         else:
130                                 pass # TODO
131
132                 elif p['type'] == 'P':
133                         out.append(o)
134                         state = INITIALIZED
135                         databytecount = 0
136
137         print out
138
139         # FIXME
140         return ''
141
142 def register():
143         return {
144                 'id': 'nunchuk',
145                 'name': 'Nunchuk',
146                 'desc': 'Nintendo Wii Nunchuk decoder',
147                 'inputformats': ['i2c'],
148                 'ouputformats': ['nunchuk'],
149         }
150
151 # Use psyco (if available) as it results in huge performance improvements.
152 try:
153         import psyco
154         psyco.bind(decode)
155 except ImportError:
156         pass
157