2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2015 Paul Evans <leonerd@leonerd.org.uk>
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.
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.
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
22 import sigrokdecode as srd
24 def _decode_intensity(val):
25 intensity = val & 0x0f
34 0x00: ['No-op', lambda _: ''],
35 0x09: ['Decode', lambda v: '0b{:08b}'.format(v)],
36 0x0A: ['Intensity', _decode_intensity],
37 0x0B: ['Scan limit', lambda v: 1 + v],
38 0x0C: ['Shutdown', lambda v: 'off' if v else 'on'],
39 0x0F: ['Display test', lambda v: 'on' if v else 'off']
42 ann_reg, ann_digit, ann_warning = range(3)
44 class Decoder(srd.Decoder):
48 longname = 'Maxim MAX7219/MAX7221'
49 desc = '8-digit LED display driver.'
54 ('register', 'Registers written to the device'),
55 ('digit', 'Digits displayed on the device'),
56 ('warnings', 'Human-readable warnings'),
59 ('commands', 'Commands', (ann_reg, ann_digit)),
60 ('warnings', 'Warnings', (ann_warning,)),
64 self.out_ann = self.register(srd.OUTPUT_ANN)
68 def putreg(self, ss, es, reg, value):
69 self.put(ss, es, self.out_ann, [ann_reg, ['%s: %s' % (reg, value)]])
71 def putdigit(self, ss, es, digit, value):
72 self.put(ss, es, self.out_ann, [ann_digit, ['Digit %d: %02X' % (digit, value)]])
74 def putwarn(self, ss, es, message):
75 self.put(ss, es, self.out_ann, [ann_warning, [message]])
77 def decode(self, ss, es, data):
81 if not self.cs_asserted:
88 if self.addr >= 1 and self.addr <= 8:
89 self.putdigit(self.addr_start, es, self.addr, mosi)
90 elif self.addr in registers:
91 name, decoder = registers[self.addr]
92 self.putreg(self.addr_start, es, name, decoder(mosi))
94 self.putwarn(self.addr_start, es,
95 'Unknown register %02X' % (self.addr))
98 elif ptype == 'CS-CHANGE':
99 self.cs_asserted = mosi
105 # Don't warn if pos=0 so that CS# glitches don't appear
106 # as spurious warnings.
107 self.putwarn(self.cs_start, es, 'Short write')
109 self.putwarn(self.cs_start, es, 'Overlong write')