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
22 def __init__(self, data):
24 def probe(self, probe):
25 s = ord(self.data[probe / 8]) & (1 << (probe % 8))
26 return True if s else False
28 def sampleiter(data, unitsize):
29 for i in range(0, len(data), unitsize):
30 yield(Sample(data[i:i+unitsize]))
33 id = 'transitioncounter'
34 name = 'Transition counter'
36 desc = 'Counts rising/falling edges in the signal.'
38 author = 'Uwe Hermann'
39 email = 'uwe@hermann-uwe.de'
42 outputs = ['transitioncounts']
46 def __init__(self, **kwargs):
47 self.probes = Decoder.probes.copy()
49 # TODO: Don't hardcode the number of channels.
52 self.lastsample = None
53 self.oldbit = [0] * self.channels
54 self.transitions = [0] * self.channels
55 self.rising = [0] * self.channels
56 self.falling = [0] * self.channels
58 def start(self, metadata):
59 self.unitsize = metadata['unitsize']
64 def decode(self, data):
65 """Counts the low->high and high->low transitions in the specified
66 channel(s) of the signal."""
68 # We should accept a list of samples and iterate...
69 for sample in sampleiter(data['data'], self.unitsize):
71 # TODO: Eliminate the need for ord().
74 # Optimization: Skip identical samples (no transitions).
75 if self.lastsample == s:
78 # Upon the first sample, store the initial values.
79 if self.lastsample == None:
81 for i in range(self.channels):
82 self.oldbit[i] = (self.lastsample & (1 << i)) >> i
84 # Iterate over all channels/probes in this sample.
85 # Count rising and falling edges for each channel.
86 for i in range(self.channels):
87 curbit = (s & (1 << i)) >> i
88 # Optimization: Skip identical bits (no transitions).
89 if self.oldbit[i] == curbit:
91 elif (self.oldbit[i] == 0 and curbit == 1):
93 elif (self.oldbit[i] == 1 and curbit == 0):
95 self.oldbit[i] = curbit
97 # Save the current sample as 'lastsample' for the next round.
100 # Total number of transitions = rising + falling edges.
101 for i in range(self.channels):
102 self.transitions[i] = self.rising[i] + self.falling[i]
104 # TODO: Which output format?
105 # TODO: How to only output something after the last chunk of data?
107 for i in range(self.channels):
108 outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
113 sigrok.register(Decoder)