summaryrefslogtreecommitdiff
path: root/decoders/nunchuk/nunchuk.py
blob: 1bcba23db99aedb14bb7dde3925f40b7c7b8d6c7 (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
##
## This file is part of the sigrok project.
##
## Copyright (C) 2010-2012 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 protocol decoder

import sigrokdecode as srd

class Decoder(srd.Decoder):
    api_version = 1
    id = 'nunchuk'
    name = 'Nunchuk'
    longname = 'Nintendo Wii Nunchuk'
    desc = 'Nintendo Wii Nunchuk controller protocol.'
    license = 'gplv2+'
    inputs = ['i2c']
    outputs = ['nunchuck']
    probes = []
    optional_probes = [] # TODO
    options = {}
    annotations = [
        ['Text', 'Human-readable text'],
    ]

    def __init__(self, **kwargs):
        self.state = 'IDLE' # TODO: Can we assume a certain initial state?
        self.sx = self.sy = self.ax = self.ay = self.az = self.bz = self.bc = 0
        self.databytecount = 0

    def start(self, metadata):
        # self.out_proto = self.add(srd.OUTPUT_PROTO, 'nunchuk')
        self.out_ann = self.add(srd.OUTPUT_ANN, 'nunchuk')

    def report(self):
        pass

    def decode(self, ss, es, data):

        cmd, databyte = data

        if cmd == 'START': # TODO: Handle 'Sr' here, too?
            self.state = 'START'

        elif cmd == 'START REPEAT':
            pass # FIXME

        elif cmd == 'ADDRESS READ':
            # TODO: Error/Warning, not supported, I think.
            pass

        elif cmd == 'ADDRESS WRITE':
            # The Wii Nunchuk always has slave address 0x54.
            # TODO: Handle this stuff more correctly.
            if databyte == 0x54:
                pass # TODO
            else:
                pass # TODO: What to do here? Ignore? Error?

        elif cmd == 'DATA READ' and self.state == 'INITIALIZED':
            if self.databytecount == 0:
                self.sx = databyte
            elif self.databytecount == 1:
                self.sy = databyte
            elif self.databytecount == 2:
                self.ax = databyte << 2
            elif self.databytecount == 3:
                self.ay = databyte << 2
            elif self.databytecount == 4:
                self.az = databyte << 2
            elif self.databytecount == 5:
                self.bz =  (databyte & (1 << 0)) >> 0
                self.bc =  (databyte & (1 << 1)) >> 1
                self.ax |= (databyte & (3 << 2)) >> 2
                self.ay |= (databyte & (3 << 4)) >> 4
                self.az |= (databyte & (3 << 6)) >> 6

                d = 'sx = 0x%02x, sy = 0x%02x, ax = 0x%02x, ay = 0x%02x, ' \
                    'az = 0x%02x, bz = 0x%02x, bc = 0x%02x' % (self.sx, \
                    self.sy, self.ax, self.ay, self.az, self.bz, self.bc)
                self.put(ss, es, self.out_ann, [0, [d]])

                self.sx = self.sy = self.ax = self.ay = self.az = 0
                self.bz = self.bc = 0
            else:
                pass # TODO

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

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

        # TODO
        elif cmd == 'DATA READ' and self.state != 'INITIALIZED':
            pass

        elif cmd == 'DATA WRITE':
            if self.state == 'IDLE':
                self.state = 'INITIALIZED'
            return

            if databyte == 0x40 and self.state == 'START':
                self.state = 'INIT'
            elif databyte == 0x00 and self.state == 'INIT':
                self.put(ss, es, self.out_ann, [0, ['Initialize nunchuk']])
                self.state = 'INITIALIZED'
            else:
                pass # TODO

        elif cmd == 'STOP':
            self.state = 'INITIALIZED'
            self.databytecount = 0