]> sigrok.org Git - libsigrokdecode.git/blame - decoders/nunchuk/nunchuk.py
srd: Add 'api_version = 1' to all PDs.
[libsigrokdecode.git] / decoders / nunchuk / nunchuk.py
CommitLineData
cd0fc8c5
UH
1##
2## This file is part of the sigrok project.
3##
c0d7b38e 4## Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
cd0fc8c5
UH
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
16ba97cf 25#
cd0fc8c5 26# TODO: Description
16ba97cf
UH
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#
cd0fc8c5 32
677d597b 33import sigrokdecode as srd
1c8ac5bf 34
bffd9bc0
UH
35# States
36IDLE = 0
37START = 1
38NUNCHUK_SLAVE = 2
39INIT = 3
40INITIALIZED = 4
41
677d597b 42class Decoder(srd.Decoder):
a2c2afd9 43 api_version = 1
2b7d0e2b 44 id = 'nunchuk'
012cfd0d 45 name = 'Nunchuk'
3d3da57d 46 longname = 'Nintendo Wii Nunchuk'
012cfd0d
UH
47 desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
48 longdesc = '...'
012cfd0d
UH
49 license = 'gplv2+'
50 inputs = ['i2c']
51 outputs = ['nunchuck']
1f24514c 52 probes = [] # TODO
012cfd0d 53 options = {}
c0d7b38e
UH
54 annotations = [
55 ['TODO', 'TODO'],
56 ]
012cfd0d
UH
57
58 def __init__(self, **kwargs):
bffd9bc0 59 self.state = IDLE # TODO: Can we assume a certain initial state?
012cfd0d 60 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
012cfd0d
UH
61 self.databytecount = 0
62
63 def start(self, metadata):
56202222
UH
64 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
65 self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
012cfd0d
UH
66
67 def report(self):
68 pass
69
2b9837d9 70 def decode(self, ss, es, data):
c0d7b38e
UH
71
72 cmd, databyte, ack_bit = data
73
74 if cmd == 'START': # TODO: Handle 'Sr' here, too?
75 self.state = START
76
a2d2aff2 77 elif cmd == 'START REPEAT':
c0d7b38e
UH
78 pass # FIXME
79
a2d2aff2 80 elif cmd == 'ADDRESS READ':
c0d7b38e
UH
81 # TODO: Error/Warning, not supported, I think.
82 pass
83
a2d2aff2 84 elif cmd == 'ADDRESS WRITE':
c0d7b38e
UH
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
a2d2aff2 92 elif cmd == 'DATA READ' and self.state == INITIALIZED:
c0d7b38e
UH
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
a2d2aff2 126 elif cmd == 'DATA READ' and self.state != INITIALIZED:
c0d7b38e
UH
127 pass
128
a2d2aff2 129 elif cmd == 'DATA WRITE':
c0d7b38e
UH
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']])
bffd9bc0 138 self.state = INITIALIZED
c0d7b38e
UH
139 else:
140 pass # TODO
012cfd0d 141
c0d7b38e
UH
142 elif cmd == 'STOP':
143 self.state = INITIALIZED
144 self.databytecount = 0
2b7d0e2b 145