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