]> sigrok.org Git - libsigrokdecode.git/blob - decoders/max7219/pd.py
9589c8f4828e6c4a3cc1f416e60541f96d46a51e
[libsigrokdecode.git] / decoders / max7219 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Paul Evans <leonerd@leonerd.org.uk>
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 re
21 import sigrokdecode as srd
22
23 def _decode_intensity(val):
24     intensity = val & 0x0f
25     if intensity == 0:
26         return 'min'
27     elif intensity == 15:
28         return 'max'
29     else:
30         return intensity
31
32 registers = {
33     0x00: ['No-op', lambda _: ''],
34     0x09: ['Decode', lambda v: '0b{:08b}'.format(v)],
35     0x0A: ['Intensity', _decode_intensity],
36     0x0B: ['Scan limit', lambda v: 1 + v],
37     0x0C: ['Shutdown', lambda v: 'off' if v else 'on'],
38     0x0F: ['Display test', lambda v: 'on' if v else 'off']
39 }
40
41 ann_reg, ann_digit, ann_warning = range(3)
42
43 class Decoder(srd.Decoder):
44     api_version = 2
45     id = 'max7219'
46     name = 'MAX7219'
47     longname = 'Maxim MAX7219/MAX7221'
48     desc = '8-digit LED display driver.'
49     license = 'gplv2+'
50     inputs = ['spi']
51     outputs = ['max7219']
52     annotations = (
53         ('register', 'Registers written to the device'),
54         ('digit', 'Digits displayed on the device'),
55         ('warnings', 'Human-readable warnings'),
56     )
57     annotation_rows = (
58         ('commands', 'Commands', (ann_reg, ann_digit)),
59         ('warnings', 'Warnings', (ann_warning,)),
60     )
61
62     def start(self):
63         self.out_ann = self.register(srd.OUTPUT_ANN)
64         self.pos = 0
65         self.cs_start = 0
66
67     def putreg(self, ss, es, reg, value):
68         self.put(ss, es, self.out_ann, [ann_reg, ['%s: %s' % (reg, value)]])
69
70     def putdigit(self, ss, es, digit, value):
71         self.put(ss, es, self.out_ann, [ann_digit, ['Digit %d: %02X' % (digit, value)]])
72
73     def putwarn(self, ss, es, message):
74         self.put(ss, es, self.out_ann, [ann_warning, [message]])
75
76     def decode(self, ss, es, data):
77         ptype, mosi, _ = data
78
79         if ptype == 'DATA':
80             if not self.cs_asserted:
81                 return
82
83             if self.pos == 0:
84                 self.addr = mosi
85                 self.addr_start = ss
86             elif self.pos == 1:
87                 if self.addr >= 1 and self.addr <= 8:
88                     self.putdigit(self.addr_start, es, self.addr, mosi)
89                 elif self.addr in registers:
90                     name, decoder = registers[self.addr]
91                     self.putreg(self.addr_start, es, name, decoder(mosi))
92                 else:
93                     self.putwarn(self.addr_start, es,
94                         'Unknown register %02X' % (self.addr))
95
96             self.pos += 1
97         elif ptype == 'CS-CHANGE':
98             self.cs_asserted = mosi
99             if self.cs_asserted:
100                 self.pos = 0
101                 self.cs_start = ss
102             else:
103                 if self.pos == 1:
104                     # Don't warn if pos=0 so that CS# glitches don't appear
105                     # as spurious warnings.
106                     self.putwarn(self.cs_start, es, 'Short write')
107                 elif self.pos > 2:
108                     self.putwarn(self.cs_start, es, 'Overlong write')