]> sigrok.org Git - libsigrokdecode.git/blame - decoders/counter/pd.py
counter: explicit option text to .wait() edge mapping
[libsigrokdecode.git] / decoders / counter / pd.py
CommitLineData
6ca7aa50
SB
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de>
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
20import sigrokdecode as srd
21
22class Decoder(srd.Decoder):
23 api_version = 3
24 id = 'counter'
25 name = 'Counter'
26 longname = 'Edge counter'
27 desc = 'Count number of edges.'
28 license = 'gplv2+'
29 inputs = ['logic']
30 outputs = []
31 channels = (
32 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
33 )
34 optional_channels = (
35 {'id': 'reset', 'name': 'Reset', 'desc': 'Reset line'},
36 )
37 annotations = (
38 ('edge_count', 'Edge count'),
39 ('word_count', 'Word count'),
42dcd8ef 40 ('word_reset', 'Word reset'),
6ca7aa50
SB
41 )
42 annotation_rows = (
43 ('edge_counts', 'Edges', (0,)),
44 ('word_counts', 'Words', (1,)),
42dcd8ef 45 ('word_resets', 'Word resets', (2,)),
6ca7aa50
SB
46 )
47 options = (
e8b53818
GS
48 {'id': 'data_edge', 'desc': 'Edges to count (data)', 'default': 'any',
49 'values': ('any', 'rising', 'falling')},
6ca7aa50 50 {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0},
e8b53818
GS
51 {'id': 'reset_edge', 'desc': 'Edge which clears counters (reset)',
52 'default': 'falling', 'values': ('rising', 'falling')},
6ca7aa50
SB
53 )
54
55 def __init__(self):
56 self.reset()
57
58 def reset(self):
59 self.edge_count = 0
60 self.word_count = 0
61 self.have_reset = None
62
63 def metadata(self, key, value):
64 if key == srd.SRD_CONF_SAMPLERATE:
65 self.samplerate = value
66
67 def start(self):
68 self.out_ann = self.register(srd.OUTPUT_ANN)
e8b53818 69 self.edge = self.options['data_edge']
6ca7aa50
SB
70 self.divider = self.options['divider']
71 if self.divider < 0:
72 self.divider = 0
73
42dcd8ef
UH
74 def putc(self, cls, annlist):
75 self.put(self.samplenum, self.samplenum, self.out_ann, [cls, annlist])
6ca7aa50
SB
76
77 def decode(self):
d11290fe
GS
78 opt_edge_map = {'rising': 'r', 'falling': 'f', 'any': 'e'}
79
80 condition = [{0: opt_edge_map[self.edge]}]
6ca7aa50
SB
81
82 if self.has_channel(1):
83 self.have_reset = True
d11290fe 84 condition.append({1: opt_edge_map[self.options['reset_edge']]})
6ca7aa50
SB
85
86 while True:
87 self.wait(condition)
88 if self.have_reset and self.matched[1]:
89 self.edge_count = 0
90 self.word_count = 0
42dcd8ef 91 self.putc(2, ['Word reset', 'Reset', 'Rst', 'R'])
6ca7aa50
SB
92 continue
93
94 self.edge_count += 1
95
42dcd8ef 96 self.putc(0, [str(self.edge_count)])
6ca7aa50
SB
97 if self.divider > 0 and (self.edge_count % self.divider) == 0:
98 self.word_count += 1
42dcd8ef 99 self.putc(1, [str(self.word_count)])