]>
Commit | Line | Data |
---|---|---|
1c6fa20f UH |
1 | ## |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.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, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
ad2dc0de | 20 | |
f9b3b245 | 21 | import sigrokdecode |
1c8ac5bf | 22 | |
f9b3b245 | 23 | class Decoder(sigrokdecode.Decoder): |
2b7d0e2b | 24 | id = 'transitioncounter' |
b41ae47f UH |
25 | name = 'Transition counter' |
26 | longname = '...' | |
27 | desc = 'Counts rising/falling edges in the signal.' | |
28 | longdesc = '...' | |
29 | author = 'Uwe Hermann' | |
30 | email = 'uwe@hermann-uwe.de' | |
31 | license = 'gplv2+' | |
32 | inputs = ['logic'] | |
33 | outputs = ['transitioncounts'] | |
f9b3b245 UH |
34 | probes = [ |
35 | ] # TODO? | |
b41ae47f UH |
36 | options = {} |
37 | ||
3643fc3f | 38 | def __init__(self, **kwargs): |
862f6f27 UH |
39 | self.output_protocol = None |
40 | self.output_annotation = None | |
f9b3b245 | 41 | self.channels = -1 |
b41ae47f | 42 | self.lastsample = None |
b41ae47f | 43 | |
3643fc3f | 44 | def start(self, metadata): |
862f6f27 UH |
45 | # self.output_protocol = self.output_new(2) |
46 | self.output_annotation = self.output_new(1) | |
3643fc3f | 47 | |
b41ae47f UH |
48 | def report(self): |
49 | pass | |
50 | ||
862f6f27 | 51 | def decode(self, timeoffset, duration, data): |
b41ae47f | 52 | |
f9b3b245 UH |
53 | for (samplenum, s) in data: |
54 | ||
55 | # ... | |
56 | if self.channels == -1: | |
57 | self.channels = len(s) | |
58 | self.oldbit = [0] * self.channels | |
59 | self.transitions = [0] * self.channels | |
60 | self.rising = [0] * self.channels | |
61 | self.falling = [0] * self.channels | |
b41ae47f UH |
62 | |
63 | # Optimization: Skip identical samples (no transitions). | |
64 | if self.lastsample == s: | |
65 | continue | |
66 | ||
67 | # Upon the first sample, store the initial values. | |
68 | if self.lastsample == None: | |
69 | self.lastsample = s | |
70 | for i in range(self.channels): | |
f9b3b245 | 71 | self.oldbit[i] = self.lastsample[i] |
b41ae47f UH |
72 | |
73 | # Iterate over all channels/probes in this sample. | |
74 | # Count rising and falling edges for each channel. | |
75 | for i in range(self.channels): | |
f9b3b245 | 76 | curbit = s[i] |
ad2dc0de | 77 | # Optimization: Skip identical bits (no transitions). |
b41ae47f | 78 | if self.oldbit[i] == curbit: |
ad2dc0de | 79 | continue |
b41ae47f UH |
80 | elif (self.oldbit[i] == 0 and curbit == 1): |
81 | self.rising[i] += 1 | |
82 | elif (self.oldbit[i] == 1 and curbit == 0): | |
83 | self.falling[i] += 1 | |
84 | self.oldbit[i] = curbit | |
85 | ||
86 | # Save the current sample as 'lastsample' for the next round. | |
87 | self.lastsample = s | |
88 | ||
89 | # Total number of transitions = rising + falling edges. | |
90 | for i in range(self.channels): | |
91 | self.transitions[i] = self.rising[i] + self.falling[i] | |
92 | ||
93 | # TODO: Which output format? | |
94 | # TODO: How to only output something after the last chunk of data? | |
95 | outdata = [] | |
96 | for i in range(self.channels): | |
97 | outdata += [[self.transitions[i], self.rising[i], self.falling[i]]] | |
862f6f27 UH |
98 | |
99 | if outdata != []: | |
f9b3b245 UH |
100 | # self.put(0, 0, self.output_protocol, out_proto) |
101 | self.put(0, 0, self.output_annotation, outdata) | |
2b7d0e2b | 102 |