]>
Commit | Line | Data |
---|---|---|
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 | ## | |
20 | ||
21 | import sigrok | |
22 | ||
23 | class Sample(): | |
24 | def __init__(self, data): | |
25 | self.data = data | |
26 | def probe(self, probe): | |
27 | s = ord(self.data[probe / 8]) & (1 << (probe % 8)) | |
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 | ||
34 | class Decoder(sigrok.Decoder): | |
35 | id = 'transitioncounter' | |
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 | ||
48 | def __init__(self, **kwargs): | |
49 | self.probes = Decoder.probes.copy() | |
50 | ||
51 | # TODO: Don't hardcode the number of channels. | |
52 | self.channels = 8 | |
53 | ||
54 | self.lastsample = None | |
55 | self.oldbit = [0] * self.channels | |
56 | self.transitions = [0] * self.channels | |
57 | self.rising = [0] * self.channels | |
58 | self.falling = [0] * self.channels | |
59 | ||
60 | def start(self, metadata): | |
61 | self.unitsize = metadata['unitsize'] | |
62 | ||
63 | def report(self): | |
64 | pass | |
65 | ||
66 | def decode(self, data): | |
67 | """Counts the low->high and high->low transitions in the specified | |
68 | channel(s) of the signal.""" | |
69 | ||
70 | # We should accept a list of samples and iterate... | |
71 | for sample in sampleiter(data['data'], self.unitsize): | |
72 | ||
73 | # TODO: Eliminate the need for ord(). | |
74 | s = ord(sample.data) | |
75 | ||
76 | # Optimization: Skip identical samples (no transitions). | |
77 | if self.lastsample == s: | |
78 | continue | |
79 | ||
80 | # Upon the first sample, store the initial values. | |
81 | if self.lastsample == None: | |
82 | self.lastsample = s | |
83 | for i in range(self.channels): | |
84 | self.oldbit[i] = (self.lastsample & (1 << i)) >> i | |
85 | ||
86 | # Iterate over all channels/probes in this sample. | |
87 | # Count rising and falling edges for each channel. | |
88 | for i in range(self.channels): | |
89 | curbit = (s & (1 << i)) >> i | |
90 | # Optimization: Skip identical bits (no transitions). | |
91 | if self.oldbit[i] == curbit: | |
92 | continue | |
93 | elif (self.oldbit[i] == 0 and curbit == 1): | |
94 | self.rising[i] += 1 | |
95 | elif (self.oldbit[i] == 1 and curbit == 0): | |
96 | self.falling[i] += 1 | |
97 | self.oldbit[i] = curbit | |
98 | ||
99 | # Save the current sample as 'lastsample' for the next round. | |
100 | self.lastsample = s | |
101 | ||
102 | # Total number of transitions = rising + falling edges. | |
103 | for i in range(self.channels): | |
104 | self.transitions[i] = self.rising[i] + self.falling[i] | |
105 | ||
106 | # TODO: Which output format? | |
107 | # TODO: How to only output something after the last chunk of data? | |
108 | outdata = [] | |
109 | for i in range(self.channels): | |
110 | outdata += [[self.transitions[i], self.rising[i], self.falling[i]]] | |
111 | self.put(outdata) | |
112 |