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 name = 'Transition counter'
35 desc = 'Counts rising/falling edges in the signal.'
37 author = 'Uwe Hermann'
38 email = 'uwe@hermann-uwe.de'
41 outputs = ['transitioncounts']
45 def __init__(self, **kwargs):
46 self.probes = Decoder.probes.copy()
48 # TODO: Don't hardcode the number of channels.
51 self.lastsample = None
52 self.oldbit = [0] * self.channels
53 self.transitions = [0] * self.channels
54 self.rising = [0] * self.channels
55 self.falling = [0] * self.channels
57 def start(self, metadata):
58 self.unitsize = metadata['unitsize']
63 def decode(self, data):
64 """Counts the low->high and high->low transitions in the specified
65 channel(s) of the signal."""
67 # We should accept a list of samples and iterate...
68 for sample in sampleiter(data['data'], self.unitsize):
70 # TODO: Eliminate the need for ord().
73 # Optimization: Skip identical samples (no transitions).
74 if self.lastsample == s:
77 # Upon the first sample, store the initial values.
78 if self.lastsample == None:
80 for i in range(self.channels):
81 self.oldbit[i] = (self.lastsample & (1 << i)) >> i
83 # Iterate over all channels/probes in this sample.
84 # Count rising and falling edges for each channel.
85 for i in range(self.channels):
86 curbit = (s & (1 << i)) >> i
87 # Optimization: Skip identical bits (no transitions).
88 if self.oldbit[i] == curbit:
90 elif (self.oldbit[i] == 0 and curbit == 1):
92 elif (self.oldbit[i] == 1 and curbit == 0):
94 self.oldbit[i] = curbit
96 # Save the current sample as 'lastsample' for the next round.
99 # Total number of transitions = rising + falling edges.
100 for i in range(self.channels):
101 self.transitions[i] = self.rising[i] + self.falling[i]
103 # TODO: Which output format?
104 # TODO: How to only output something after the last chunk of data?
106 for i in range(self.channels):
107 outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
110 # Use psyco (if available) as it results in huge performance improvements.