2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 def __init__(self, data):
26 def probe(self, probe):
27 s = ord(self.data[int(probe / 8)]) & (1 << (probe % 8))
28 return True if s else False
30 def sampleiter(data, unitsize):
31 for i in range(0, len(data), unitsize):
32 yield(Sample(data[i:i+unitsize]))
34 class Decoder(sigrok.Decoder):
35 id = 'transitioncounter'
36 name = 'Transition counter'
38 desc = 'Counts rising/falling edges in the signal.'
40 author = 'Uwe Hermann'
41 email = 'uwe@hermann-uwe.de'
44 outputs = ['transitioncounts']
48 def __init__(self, **kwargs):
49 self.probes = Decoder.probes.copy()
50 self.output_protocol = None
51 self.output_annotation = None
53 # TODO: Don't hardcode the number of channels.
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
62 def start(self, metadata):
63 self.unitsize = metadata['unitsize']
64 # self.output_protocol = self.output_new(2)
65 self.output_annotation = self.output_new(1)
70 def decode(self, timeoffset, duration, data):
71 """Counts the low->high and high->low transitions in the specified
72 channel(s) of the signal."""
74 # We should accept a list of samples and iterate...
75 for sample in sampleiter(data, self.unitsize):
77 # TODO: Eliminate the need for ord().
80 # Optimization: Skip identical samples (no transitions).
81 if self.lastsample == s:
84 # Upon the first sample, store the initial values.
85 if self.lastsample == None:
87 for i in range(self.channels):
88 self.oldbit[i] = (self.lastsample & (1 << i)) >> i
90 # Iterate over all channels/probes in this sample.
91 # Count rising and falling edges for each channel.
92 for i in range(self.channels):
93 curbit = (s & (1 << i)) >> i
94 # Optimization: Skip identical bits (no transitions).
95 if self.oldbit[i] == curbit:
97 elif (self.oldbit[i] == 0 and curbit == 1):
99 elif (self.oldbit[i] == 1 and curbit == 0):
101 self.oldbit[i] = curbit
103 # Save the current sample as 'lastsample' for the next round.
106 # Total number of transitions = rising + falling edges.
107 for i in range(self.channels):
108 self.transitions[i] = self.rising[i] + self.falling[i]
110 # TODO: Which output format?
111 # TODO: How to only output something after the last chunk of data?
113 for i in range(self.channels):
114 outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
117 # self.put(self.output_protocol, 0, 0, out_proto)
118 self.put(self.output_annotation, 0, 0, outdata)