]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/counter/pd.py
counter: add support for user specified initial counter values
[libsigrokdecode.git] / decoders / counter / pd.py
... / ...
CommitLineData
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
22PIN_DATA, PIN_RESET = range(2)
23ROW_EDGE, ROW_WORD, ROW_RESET = range(3)
24
25class Decoder(srd.Decoder):
26 api_version = 3
27 id = 'counter'
28 name = 'Counter'
29 longname = 'Edge counter'
30 desc = 'Count number of edges.'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = []
34 channels = (
35 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
36 )
37 optional_channels = (
38 {'id': 'reset', 'name': 'Reset', 'desc': 'Reset line'},
39 )
40 annotations = (
41 ('edge_count', 'Edge count'),
42 ('word_count', 'Word count'),
43 ('word_reset', 'Word reset'),
44 )
45 annotation_rows = (
46 ('edge_counts', 'Edges', (ROW_EDGE,)),
47 ('word_counts', 'Words', (ROW_WORD,)),
48 ('word_resets', 'Word resets', (ROW_RESET,)),
49 )
50 options = (
51 {'id': 'data_edge', 'desc': 'Edges to count (data)', 'default': 'any',
52 'values': ('any', 'rising', 'falling')},
53 {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0},
54 {'id': 'reset_edge', 'desc': 'Edge which clears counters (reset)',
55 'default': 'falling', 'values': ('rising', 'falling')},
56 {'id': 'edge_off', 'desc': 'Initial edge counter value', 'default': 0},
57 {'id': 'word_off', 'desc': 'Initial word counter value', 'default': 0},
58 )
59
60 def __init__(self):
61 self.reset()
62
63 def reset(self):
64 pass
65
66 def metadata(self, key, value):
67 if key == srd.SRD_CONF_SAMPLERATE:
68 self.samplerate = value
69
70 def start(self):
71 self.out_ann = self.register(srd.OUTPUT_ANN)
72
73 def putc(self, cls, ss, annlist):
74 self.put(ss, self.samplenum, self.out_ann, [cls, annlist])
75
76 def decode(self):
77 opt_edge_map = {'rising': 'r', 'falling': 'f', 'any': 'e'}
78
79 data_edge = self.options['data_edge']
80 divider = self.options['divider']
81 if divider < 0:
82 divider = 0
83 reset_edge = self.options['reset_edge']
84
85 condition = [{PIN_DATA: opt_edge_map[data_edge]}]
86 have_reset = self.has_channel(PIN_RESET)
87 if have_reset:
88 cond_reset = len(condition)
89 condition.append({PIN_RESET: opt_edge_map[reset_edge]})
90
91 edge_count = int(self.options['edge_off'])
92 edge_start = None
93 word_count = int(self.options['word_off'])
94 word_start = None
95 while True:
96 self.wait(condition)
97 now = self.samplenum
98
99 if have_reset and self.matched[cond_reset]:
100 edge_count = 0
101 edge_start = now
102 word_count = 0
103 word_start = now
104 self.putc(ROW_RESET, now, ['Word reset', 'Reset', 'Rst', 'R'])
105 continue
106
107 # Implementation note: In the absence of a RESET condition
108 # before the first data edge, any arbitrary choice of where
109 # to start the annotation is valid. One may choose to emit a
110 # narrow annotation (where ss=es), or assume that the cycle
111 # which corresponds to the counter value started at sample
112 # number 0. We decided to go with the latter here, to avoid
113 # narrow annotations (see bug #1210). None of this matters in
114 # the presence of a RESET condition in the input stream.
115 if edge_start is None:
116 edge_start = 0
117 if word_start is None:
118 word_start = 0
119
120 edge_count += 1
121 self.putc(ROW_EDGE, edge_start, ["{:d}".format(edge_count)])
122 edge_start = now
123
124 if divider and (edge_count % divider) == 0:
125 word_count += 1
126 self.putc(ROW_WORD, word_start, ["{:d}".format(word_count)])
127 word_start = now