]>
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 | |
1c8ac5bf UH |
21 | import sigrok |
22 | ||
b41ae47f UH |
23 | class Sample(): |
24 | def __init__(self, data): | |
25 | self.data = data | |
26 | def probe(self, probe): | |
fb53ee5e | 27 | s = self.data[int(probe / 8)] & (1 << (probe % 8)) |
b41ae47f UH |
28 | return True if s else False |
29 | ||
30 | def sampleiter(data, unitsize): | |
31 | for i in range(0, len(data), unitsize): | |
32 | yield(Sample(data[i:i+unitsize])) | |
33 | ||
1c8ac5bf | 34 | class Decoder(sigrok.Decoder): |
2b7d0e2b | 35 | id = 'transitioncounter' |
b41ae47f UH |
36 | name = 'Transition counter' |
37 | longname = '...' | |
38 | desc = 'Counts rising/falling edges in the signal.' | |
39 | longdesc = '...' | |
40 | author = 'Uwe Hermann' | |
41 | email = 'uwe@hermann-uwe.de' | |
42 | license = 'gplv2+' | |
43 | inputs = ['logic'] | |
44 | outputs = ['transitioncounts'] | |
45 | probes = {} | |
46 | options = {} | |
47 | ||
3643fc3f | 48 | def __init__(self, **kwargs): |
b41ae47f | 49 | self.probes = Decoder.probes.copy() |
862f6f27 UH |
50 | self.output_protocol = None |
51 | self.output_annotation = None | |
b41ae47f UH |
52 | |
53 | # TODO: Don't hardcode the number of channels. | |
54 | self.channels = 8 | |
55 | ||
56 | self.lastsample = None | |
57 | self.oldbit = [0] * self.channels | |
58 | self.transitions = [0] * self.channels | |
59 | self.rising = [0] * self.channels | |
60 | self.falling = [0] * self.channels | |
61 | ||
3643fc3f | 62 | def start(self, metadata): |
e100d51e | 63 | self.unitsize = metadata['unitsize'] |
862f6f27 UH |
64 | # self.output_protocol = self.output_new(2) |
65 | self.output_annotation = self.output_new(1) | |
3643fc3f | 66 | |
b41ae47f UH |
67 | def report(self): |
68 | pass | |
69 | ||
862f6f27 | 70 | def decode(self, timeoffset, duration, data): |
b41ae47f | 71 | # We should accept a list of samples and iterate... |
862f6f27 | 72 | for sample in sampleiter(data, self.unitsize): |
b41ae47f UH |
73 | |
74 | # TODO: Eliminate the need for ord(). | |
75 | s = ord(sample.data) | |
76 | ||
77 | # Optimization: Skip identical samples (no transitions). | |
78 | if self.lastsample == s: | |
79 | continue | |
80 | ||
81 | # Upon the first sample, store the initial values. | |
82 | if self.lastsample == None: | |
83 | self.lastsample = s | |
84 | for i in range(self.channels): | |
85 | self.oldbit[i] = (self.lastsample & (1 << i)) >> i | |
86 | ||
87 | # Iterate over all channels/probes in this sample. | |
88 | # Count rising and falling edges for each channel. | |
89 | for i in range(self.channels): | |
ad2dc0de UH |
90 | curbit = (s & (1 << i)) >> i |
91 | # Optimization: Skip identical bits (no transitions). | |
b41ae47f | 92 | if self.oldbit[i] == curbit: |
ad2dc0de | 93 | continue |
b41ae47f UH |
94 | elif (self.oldbit[i] == 0 and curbit == 1): |
95 | self.rising[i] += 1 | |
96 | elif (self.oldbit[i] == 1 and curbit == 0): | |
97 | self.falling[i] += 1 | |
98 | self.oldbit[i] = curbit | |
99 | ||
100 | # Save the current sample as 'lastsample' for the next round. | |
101 | self.lastsample = s | |
102 | ||
103 | # Total number of transitions = rising + falling edges. | |
104 | for i in range(self.channels): | |
105 | self.transitions[i] = self.rising[i] + self.falling[i] | |
106 | ||
107 | # TODO: Which output format? | |
108 | # TODO: How to only output something after the last chunk of data? | |
109 | outdata = [] | |
110 | for i in range(self.channels): | |
111 | outdata += [[self.transitions[i], self.rising[i], self.falling[i]]] | |
862f6f27 UH |
112 | |
113 | if outdata != []: | |
114 | # self.put(self.output_protocol, 0, 0, out_proto) | |
115 | self.put(self.output_annotation, 0, 0, outdata) | |
2b7d0e2b | 116 |