]> sigrok.org Git - libsigrokdecode.git/blame - decoders/max7219/pd.py
log: Use human-readable output type name everywhere.
[libsigrokdecode.git] / decoders / max7219 / pd.py
CommitLineData
979d5dd0
PLE
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
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
979d5dd0
PLE
18##
19
20import re
21import sigrokdecode as srd
22
23def _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
32registers = {
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
41ann_reg, ann_digit, ann_warning = range(3)
42
43class Decoder(srd.Decoder):
b197383c 44 api_version = 3
979d5dd0
PLE
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
34f18d2f
GS
62 def __init__(self):
63 self.reset()
64
65 def reset(self):
66 pass
67
979d5dd0
PLE
68 def start(self):
69 self.out_ann = self.register(srd.OUTPUT_ANN)
70 self.pos = 0
71 self.cs_start = 0
72
73 def putreg(self, ss, es, reg, value):
74 self.put(ss, es, self.out_ann, [ann_reg, ['%s: %s' % (reg, value)]])
75
76 def putdigit(self, ss, es, digit, value):
77 self.put(ss, es, self.out_ann, [ann_digit, ['Digit %d: %02X' % (digit, value)]])
78
79 def putwarn(self, ss, es, message):
80 self.put(ss, es, self.out_ann, [ann_warning, [message]])
81
82 def decode(self, ss, es, data):
83 ptype, mosi, _ = data
84
85 if ptype == 'DATA':
86 if not self.cs_asserted:
87 return
88
89 if self.pos == 0:
90 self.addr = mosi
91 self.addr_start = ss
92 elif self.pos == 1:
93 if self.addr >= 1 and self.addr <= 8:
94 self.putdigit(self.addr_start, es, self.addr, mosi)
95 elif self.addr in registers:
96 name, decoder = registers[self.addr]
97 self.putreg(self.addr_start, es, name, decoder(mosi))
98 else:
99 self.putwarn(self.addr_start, es,
100 'Unknown register %02X' % (self.addr))
101
102 self.pos += 1
103 elif ptype == 'CS-CHANGE':
104 self.cs_asserted = mosi
105 if self.cs_asserted:
106 self.pos = 0
107 self.cs_start = ss
108 else:
109 if self.pos == 1:
110 # Don't warn if pos=0 so that CS# glitches don't appear
111 # as spurious warnings.
112 self.putwarn(self.cs_start, es, 'Short write')
113 elif self.pos > 2:
114 self.putwarn(self.cs_start, es, 'Overlong write')