2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2018 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
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, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
22 class Decoder(srd.Decoder):
26 longname = 'Edge counter'
27 desc = 'Count number of edges.'
32 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
35 {'id': 'reset', 'name': 'Reset', 'desc': 'Reset line'},
38 ('edge_count', 'Edge count'),
39 ('word_count', 'Word count'),
40 ('word_reset', 'Word reset'),
43 ('edge_counts', 'Edges', (0,)),
44 ('word_counts', 'Words', (1,)),
45 ('word_resets', 'Word resets', (2,)),
48 {'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling')},
49 {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0},
58 self.have_reset = None
60 def metadata(self, key, value):
61 if key == srd.SRD_CONF_SAMPLERATE:
62 self.samplerate = value
65 self.out_ann = self.register(srd.OUTPUT_ANN)
66 self.edge = self.options['edge']
67 self.divider = self.options['divider']
71 def putc(self, cls, annlist):
72 self.put(self.samplenum, self.samplenum, self.out_ann, [cls, annlist])
75 condition = [{'rising': {0: 'r'},
77 'any': {0: 'e'},}[self.edge]]
79 if self.has_channel(1):
80 self.have_reset = True
81 condition.append({1: 'f'})
85 if self.have_reset and self.matched[1]:
88 self.putc(2, ['Word reset', 'Reset', 'Rst', 'R'])
93 self.putc(0, [str(self.edge_count)])
94 if self.divider > 0 and (self.edge_count % self.divider) == 0:
96 self.putc(1, [str(self.word_count)])