2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 # Nintendo Wii Nunchuk decoder
27 # FIXME: This is just some example input for testing purposes...
30 {'type': 'S', 'range': (10, 11), 'data': None, 'ann': ''},
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': ''},
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': ''},
47 {'type': 'P', 'range': (32, 33), 'data': None, 'ann': ''},
56 """Nintendo Wii Nunchuk decoder"""
58 # FIXME: Get the data in the correct format in the first place.
59 inbuf = [ord(x) for x in inbuf]
63 # TODO: Pass in metadata.
66 IDLE, START, NUNCHUK_SLAVE, INIT, INITIALIZED = range(5)
67 state = IDLE # TODO: Can we assume a certain initial state?
69 sx = sy = ax = ay = az = bz = bc = 0
73 # Loop over all I2C packets.
74 for p in example_packets:
75 if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
78 elif p['type'] == 'Sr':
81 elif p['type'] == 'AR':
82 # TODO: Error/Warning, not supported, I think.
85 elif p['type'] == 'AW':
86 # The Wii Nunchuk always has slave address 0x54.
87 # TODO: Handle this stuff more correctly.
91 pass # TODO: What to do here? Ignore? Error?
93 elif p['type'] == 'DR' and state == INITIALIZED:
94 if databytecount == 0:
96 elif databytecount == 1:
98 elif databytecount == 2:
100 elif databytecount == 3:
102 elif databytecount == 4:
104 elif databytecount == 5:
105 bz = (p['data'] & (1 << 0)) >> 0
106 bc = (p['data'] & (1 << 1)) >> 1
107 ax |= (p['data'] & (3 << 2)) >> 2
108 ay |= (p['data'] & (3 << 4)) >> 4
109 az |= (p['data'] & (3 << 6)) >> 6
111 o = {'type': 'D', 'range': (0, 0), 'data': []}
112 o['data'] = [sx, sy, ax, ay, az, bz, bc]
113 # sx = sy = ax = ay = az = bz = bc = 0
117 if 0 <= databytecount <= 5:
120 # TODO: If 6 bytes read -> save and reset
123 elif p['type'] == 'DR' and state != INITIALIZED:
126 elif p['type'] == 'DW':
127 if p['data'] == 0x40 and state == START:
129 elif p['data'] == 0x00 and state == INIT:
130 o = {'type': 'I', 'range': (0, 0), 'data': []}
131 o['data'] = [0x40, 0x00]
137 elif p['type'] == 'P':
150 'longname': 'Nintendo Wii Nunchuk decoder',
151 'desc': 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.',
153 'author': 'Uwe Hermann',
154 'email': 'uwe@hermann-uwe.de',
168 # Use psyco (if available) as it results in huge performance improvements.