]> sigrok.org Git - libsigrokdecode.git/blame - decoders/nunchuk/nunchuk.py
srd: Remove TODOs from annotation format names.
[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
156509ca 21# Nintendo Wii Nunchuk protocol decoder
cd0fc8c5 22
677d597b 23import sigrokdecode as srd
1c8ac5bf 24
677d597b 25class Decoder(srd.Decoder):
a2c2afd9 26 api_version = 1
2b7d0e2b 27 id = 'nunchuk'
012cfd0d 28 name = 'Nunchuk'
3d3da57d 29 longname = 'Nintendo Wii Nunchuk'
012cfd0d 30 desc = 'Decodes the Nintendo Wii Nunchuk I2C-based protocol.'
012cfd0d
UH
31 license = 'gplv2+'
32 inputs = ['i2c']
33 outputs = ['nunchuck']
decde15e 34 probes = []
b77614bc 35 optional_probes = [] # TODO
012cfd0d 36 options = {}
c0d7b38e 37 annotations = [
ee3e279c 38 ['Text', 'Human-readable text'],
c0d7b38e 39 ]
012cfd0d
UH
40
41 def __init__(self, **kwargs):
2b716038 42 self.state = 'IDLE' # TODO: Can we assume a certain initial state?
012cfd0d 43 self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
012cfd0d
UH
44 self.databytecount = 0
45
46 def start(self, metadata):
56202222
UH
47 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
48 self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')
012cfd0d
UH
49
50 def report(self):
51 pass
52
2b9837d9 53 def decode(self, ss, es, data):
c0d7b38e 54
1b75abfd 55 cmd, databyte = data
c0d7b38e
UH
56
57 if cmd == 'START': # TODO: Handle 'Sr' here, too?
2b716038 58 self.state = 'START'
c0d7b38e 59
a2d2aff2 60 elif cmd == 'START REPEAT':
c0d7b38e
UH
61 pass # FIXME
62
a2d2aff2 63 elif cmd == 'ADDRESS READ':
c0d7b38e
UH
64 # TODO: Error/Warning, not supported, I think.
65 pass
66
a2d2aff2 67 elif cmd == 'ADDRESS WRITE':
c0d7b38e
UH
68 # The Wii Nunchuk always has slave address 0x54.
69 # TODO: Handle this stuff more correctly.
70 if databyte == 0x54:
71 pass # TODO
72 else:
73 pass # TODO: What to do here? Ignore? Error?
74
2b716038 75 elif cmd == 'DATA READ' and self.state == 'INITIALIZED':
c0d7b38e
UH
76 if self.databytecount == 0:
77 self.sx = databyte
78 elif self.databytecount == 1:
79 self.sy = databyte
80 elif self.databytecount == 2:
81 self.ax = databyte << 2
82 elif self.databytecount == 3:
83 self.ay = databyte << 2
84 elif self.databytecount == 4:
85 self.az = databyte << 2
86 elif self.databytecount == 5:
87 self.bz = (databyte & (1 << 0)) >> 0
88 self.bc = (databyte & (1 << 1)) >> 1
89 self.ax |= (databyte & (3 << 2)) >> 2
90 self.ay |= (databyte & (3 << 4)) >> 4
91 self.az |= (databyte & (3 << 6)) >> 6
92
93 d = 'sx = 0x%02x, sy = 0x%02x, ax = 0x%02x, ay = 0x%02x, ' \
94 'az = 0x%02x, bz = 0x%02x, bc = 0x%02x' % (self.sx, \
95 self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
96 self.put(ss, es, self.out_ann, [0, [d]])
97
98 self.sx = self.sy = self.ax = self.ay = self.az = 0
99 self.bz = self.bc = 0
100 else:
101 pass # TODO
102
103 if 0 <= self.databytecount <= 5:
104 self.databytecount += 1
105
106 # TODO: If 6 bytes read -> save and reset
107
108 # TODO
2b716038 109 elif cmd == 'DATA READ' and self.state != 'INITIALIZED':
c0d7b38e
UH
110 pass
111
a2d2aff2 112 elif cmd == 'DATA WRITE':
2b716038
UH
113 if self.state == 'IDLE':
114 self.state = 'INITIALIZED'
c0d7b38e 115 return
e4f82268 116
2b716038
UH
117 if databyte == 0x40 and self.state == 'START':
118 self.state = 'INIT'
119 elif databyte == 0x00 and self.state == 'INIT':
c0d7b38e 120 self.put(ss, es, self.out_ann, [0, ['Initialize nunchuk']])
2b716038 121 self.state = 'INITIALIZED'
c0d7b38e
UH
122 else:
123 pass # TODO
012cfd0d 124
c0d7b38e 125 elif cmd == 'STOP':
2b716038 126 self.state = 'INITIALIZED'
c0d7b38e 127 self.databytecount = 0
2b7d0e2b 128