]> sigrok.org Git - libsigrokdecode.git/blob - decoders/adns5020/pd.py
adns5020: Python style nit, fixup whitespace
[libsigrokdecode.git] / decoders / adns5020 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Karl Palsson <karlp@tweak.net.au>
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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 regs = {
23     0: 'Product_ID',
24     1: 'Revision_ID',
25     2: 'Motion',
26     3: 'Delta_X',
27     4: 'Delta_Y',
28     5: 'SQUAL',
29     6: 'Shutter_Upper',
30     7: 'Shutter_Lower',
31     8: 'Maximum_Pixel',
32     9: 'Pixel_Sum',
33     0xa: 'Minimum_Pixel',
34     0xb: 'Pixel_Grab',
35     0xd: 'Mouse_Control',
36     0x3a: 'Chip_Reset',
37     0x3f: 'Inv_Rev_ID',
38     0x63: 'Motion_Burst',
39 }
40
41 class Decoder(srd.Decoder):
42     api_version = 3
43     id = 'adns5020'
44     name = 'ADNS-5020'
45     longname = 'Avago ADNS-5020 optical mouse sensor'
46     desc = 'Bidirectional command and data over an SPI-like protocol.'
47     license = 'gplv2+'
48     inputs = ['spi']
49     outputs = ['adns5020']
50     annotations = (
51         ('read', 'Register read commands'),
52         ('write', 'Register write commands'),
53         ('warning', 'Warnings'),
54     )
55     annotation_rows = (
56         ('read', 'Read', (0,)),
57         ('write', 'Write', (1,)),
58         ('warnings', 'Warnings', (2,)),
59     )
60
61     def __init__(self):
62         self.reset()
63
64     def reset(self):
65         self.ss_cmd, self.es_cmd = 0, 0
66         self.mosi_bytes = []
67
68     def start(self):
69         self.out_ann = self.register(srd.OUTPUT_ANN)
70
71     def putx(self, data):
72         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
73
74     def put_warn(self, pos, msg):
75         self.put(pos[0], pos[1], self.out_ann, [2, [msg]])
76
77     def decode(self, ss, es, data):
78         ptype = data[0]
79         if ptype == 'CS-CHANGE':
80             # If we transition high mid-stream, toss out our data and restart.
81             cs_old, cs_new = data[1:]
82             if cs_old is not None and cs_old == 0 and cs_new == 1:
83                 if len(self.mosi_bytes) not in [0, 2]:
84                     self.put_warn([self.ss_cmd, es], 'Misplaced CS#!')
85                     self.mosi_bytes = []
86             return
87
88         # Don't care about anything else.
89         if ptype != 'DATA':
90             return
91         mosi, miso = data[1:]
92
93         self.ss, self.es = ss, es
94
95         if len(self.mosi_bytes) == 0:
96             self.ss_cmd = ss
97         self.mosi_bytes.append(mosi)
98
99         # Writes/reads are mostly two transfers (burst mode is different).
100         if len(self.mosi_bytes) != 2:
101             return
102
103         self.es_cmd = es
104         cmd, arg = self.mosi_bytes
105         write = cmd & 0x80
106         reg = cmd & 0x7f
107         reg_desc = regs.get(reg, 'Reserved %#x' % reg)
108         if reg > 0x63:
109             reg_desc = 'Unknown'
110         if write:
111             self.putx([1, ['%s: %#x' % (reg_desc, arg)]])
112         else:
113             self.putx([0, ['%s: %d' % (reg_desc, arg)]])
114
115         self.mosi_bytes = []