]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | import sigrokdecode as srd | |
21 | ||
f2120b9e GS |
22 | (PIN_DATA, PIN_RESET) = range(2) |
23 | (ROW_EDGE, ROW_WORD, ROW_RESET) = range(3) | |
24 | ||
6ca7aa50 SB |
25 | class 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'), | |
42dcd8ef | 43 | ('word_reset', 'Word reset'), |
6ca7aa50 SB |
44 | ) |
45 | annotation_rows = ( | |
f2120b9e GS |
46 | ('edge_counts', 'Edges', (ROW_EDGE,)), |
47 | ('word_counts', 'Words', (ROW_WORD,)), | |
48 | ('word_resets', 'Word resets', (ROW_RESET,)), | |
6ca7aa50 SB |
49 | ) |
50 | options = ( | |
e8b53818 GS |
51 | {'id': 'data_edge', 'desc': 'Edges to count (data)', 'default': 'any', |
52 | 'values': ('any', 'rising', 'falling')}, | |
6ca7aa50 | 53 | {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0}, |
e8b53818 GS |
54 | {'id': 'reset_edge', 'desc': 'Edge which clears counters (reset)', |
55 | 'default': 'falling', 'values': ('rising', 'falling')}, | |
6ca7aa50 SB |
56 | ) |
57 | ||
58 | def __init__(self): | |
59 | self.reset() | |
60 | ||
61 | def reset(self): | |
deabbb70 | 62 | pass |
6ca7aa50 SB |
63 | |
64 | def metadata(self, key, value): | |
65 | if key == srd.SRD_CONF_SAMPLERATE: | |
66 | self.samplerate = value | |
67 | ||
68 | def start(self): | |
69 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
6ca7aa50 | 70 | |
42dcd8ef UH |
71 | def putc(self, cls, annlist): |
72 | self.put(self.samplenum, self.samplenum, self.out_ann, [cls, annlist]) | |
6ca7aa50 SB |
73 | |
74 | def decode(self): | |
d11290fe GS |
75 | opt_edge_map = {'rising': 'r', 'falling': 'f', 'any': 'e'} |
76 | ||
deabbb70 GS |
77 | self.edge = self.options['data_edge'] |
78 | self.divider = self.options['divider'] | |
79 | if self.divider < 0: | |
80 | self.divider = 0 | |
81 | ||
f2120b9e GS |
82 | condition = [{PIN_DATA: opt_edge_map[self.edge]}] |
83 | self.have_reset = self.has_channel(PIN_RESET) | |
84 | if self.have_reset: | |
85 | cond_reset = len(condition) | |
86 | condition.append({PIN_RESET: opt_edge_map[self.options['reset_edge']]}) | |
6ca7aa50 | 87 | |
deabbb70 GS |
88 | self.edge_count = 0 |
89 | self.word_count = 0 | |
6ca7aa50 SB |
90 | while True: |
91 | self.wait(condition) | |
f2120b9e GS |
92 | |
93 | if self.have_reset and self.matched[cond_reset]: | |
6ca7aa50 SB |
94 | self.edge_count = 0 |
95 | self.word_count = 0 | |
f2120b9e | 96 | self.putc(ROW_RESET, ['Word reset', 'Reset', 'Rst', 'R']) |
6ca7aa50 SB |
97 | continue |
98 | ||
99 | self.edge_count += 1 | |
f2120b9e | 100 | self.putc(ROW_EDGE, [str(self.edge_count)]) |
6ca7aa50 | 101 | |
6ca7aa50 SB |
102 | if self.divider > 0 and (self.edge_count % self.divider) == 0: |
103 | self.word_count += 1 | |
f2120b9e | 104 | self.putc(ROW_WORD, [str(self.word_count)]) |