]> sigrok.org Git - libsigrokdecode.git/blame - decoders/counter/pd.py
Add a trivial "counter" decoder
[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'),
40 )
41 annotation_rows = (
42 ('edge_counts', 'Edges', (0,)),
43 ('word_counts', 'Words', (1,)),
44 )
45 options = (
46 {'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling')},
47 {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0},
48 )
49
50 def __init__(self):
51 self.reset()
52
53 def reset(self):
54 self.edge_count = 0
55 self.word_count = 0
56 self.have_reset = None
57
58 def metadata(self, key, value):
59 if key == srd.SRD_CONF_SAMPLERATE:
60 self.samplerate = value
61
62 def start(self):
63 self.out_ann = self.register(srd.OUTPUT_ANN)
64 self.edge = self.options['edge']
65 self.divider = self.options['divider']
66 if self.divider < 0:
67 self.divider = 0
68
69 def put_count(self, ann_class, count):
70 self.put(self.samplenum, self.samplenum, self.out_ann,
71 [ann_class, [str(count)]])
72
73 def decode(self):
74 condition = [{'rising': {0: 'r'},
75 'falling': {0: 'f'},
76 'any': {0: 'e'},}[self.edge]]
77
78 if self.has_channel(1):
79 self.have_reset = True
80 condition.append({1: 'f'})
81
82 while True:
83 self.wait(condition)
84 if self.have_reset and self.matched[1]:
85 self.edge_count = 0
86 self.word_count = 0
87 self.put_count(1, 'R')
88 continue
89
90 self.edge_count += 1
91
92 self.put_count(0, self.edge_count)
93 if self.divider > 0 and (self.edge_count % self.divider) == 0:
94 self.word_count += 1
95 self.put_count(1, self.word_count)