summaryrefslogtreecommitdiff
path: root/decoders/nunchuk.py
blob: 77b154eb377cedfb4a55089e1c508d423ea52121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
##
## This file is part of the sigrok project.
##
## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
##

#
# Nintendo Wii Nunchuk decoder
#

# TODO: Description

# FIXME: This is just some example input for testing purposes...
example_packets = [
	# START condition.
	{'type': 'S',  'range': (10, 11), 'data': None, 'ann': ''},

	# Nunchuk init: Write 0x40,0x00 to slave address 0x54.
	{'type': 'AW', 'range': (12, 13), 'data': 0x54, 'ann': ''},
	{'type': 'DW', 'range': (14, 15), 'data': 0x40, 'ann': ''},
	{'type': 'AW', 'range': (16, 17), 'data': 0x54, 'ann': ''},
	{'type': 'DW', 'range': (18, 19), 'data': 0x00, 'ann': ''},

	# Get data: Read 6 bytes of data.
	{'type': 'DR', 'range': (20, 21), 'data': 0x11, 'ann': ''},
	{'type': 'DR', 'range': (22, 23), 'data': 0x22, 'ann': ''},
	{'type': 'DR', 'range': (24, 25), 'data': 0x33, 'ann': ''},
	{'type': 'DR', 'range': (26, 27), 'data': 0x44, 'ann': ''},
	{'type': 'DR', 'range': (28, 29), 'data': 0x55, 'ann': ''},
	{'type': 'DR', 'range': (30, 31), 'data': 0x66, 'ann': ''},

	# STOP condition.
	{'type': 'P',  'range': (32, 33), 'data': None, 'ann': ''},
]

def decode(inbuf):
	"""Nintendo Wii Nunchuk decoder"""

	# FIXME: Get the data in the correct format in the first place.
	inbuf = [ord(x) for x in inbuf]
	out = []
	o = {}

	# TODO: Pass in metadata.

	# States
	IDLE, START, NUNCHUK_SLAVE, INIT, INITIALIZED = range(5)
	state = IDLE # TODO: Can we assume a certain initial state?

	sx = sy = ax = ay = az = bz = bc = 0

	databytecount = 0

	# Loop over all I2C packets.
	for p in example_packets:
		if p['type'] == 'S': # TODO: Handle 'Sr' here, too?
			state = START

		elif p['type'] == 'Sr':
			pass # FIXME

		elif p['type'] == 'AR':
			# TODO: Error/Warning, not supported, I think.
			pass

		elif p['type'] == 'AW':
			# The Wii Nunchuk always has slave address 0x54.
			# TODO: Handle this stuff more correctly.
			if p['data'] == 0x54:
				pass # TODO
			else:
				pass # TODO: What to do here? Ignore? Error?

		elif p['type'] == 'DR' and state == INITIALIZED:
			if databytecount == 0:
				sx = p['data']
			elif databytecount == 1:
				sy = p['data']
			elif databytecount == 2:
				ax = p['data'] << 2
			elif databytecount == 3:
				ay = p['data'] << 2
			elif databytecount == 4:
				az = p['data'] << 2
			elif databytecount == 5:
				bz =  (p['data'] & (1 << 0)) >> 0
				bc =  (p['data'] & (1 << 1)) >> 1
				ax |= (p['data'] & (3 << 2)) >> 2
				ay |= (p['data'] & (3 << 4)) >> 4
				az |= (p['data'] & (3 << 6)) >> 6
				# del o
				o = {'type': 'D', 'range': (0, 0), 'data': []}
			 	o['data'] = [sx, sy, ax, ay, az, bz, bc]
				# sx = sy = ax = ay = az = bz = bc = 0
			else:
				pass # TODO

			if 0 <= databytecount <= 5:
				databytecount += 1

			# TODO: If 6 bytes read -> save and reset

		# TODO
		elif p['type'] == 'DR' and state != INITIALIZED:
			pass

		elif p['type'] == 'DW':
			if p['data'] == 0x40 and state == START:
				state = INIT
			elif p['data'] == 0x00 and state == INIT:
				o = {'type': 'I', 'range': (0, 0), 'data': []}
				o['data'] = [0x40, 0x00]
				out.append(o)
				state = INITIALIZED
			else:
				pass # TODO

		elif p['type'] == 'P':
			out.append(o)
			state = INITIALIZED
			databytecount = 0

	print out

	# FIXME
	return ''

def register():
	return {
		'id': 'nunchuk',
		'name': 'Nunchuk',
		'longname': 'Nintendo Wii Nunchuk decoder',
		'desc': 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.',
		'longdesc': '...',
		'author': 'Uwe Hermann',
		'email': 'uwe@hermann-uwe.de',
		'license': 'gplv2+',
		'in': ['i2c'],
		'out': ['nunchuck'],
		'probes': [
			# TODO
		],
		'options': {
			# TODO
		},
		# 'start': start,
		# 'report': report,
	}

# Use psyco (if available) as it results in huge performance improvements.
try:
	import psyco
	psyco.bind(decode)
except ImportError:
	pass