]> sigrok.org Git - libsigrokdecode.git/blame - decoders/transitioncounter.py
srd: i2c.py: Mark Repeated Start condition as 'Sr'.
[libsigrokdecode.git] / decoders / transitioncounter.py
CommitLineData
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
b41ae47f
UH
21class Sample():
22 def __init__(self, data):
23 self.data = data
24 def probe(self, probe):
25 s = ord(self.data[probe / 8]) & (1 << (probe % 8))
26 return True if s else False
27
28def sampleiter(data, unitsize):
29 for i in range(0, len(data), unitsize):
30 yield(Sample(data[i:i+unitsize]))
31
32class Decoder():
33 name = 'Transition counter'
34 longname = '...'
35 desc = 'Counts rising/falling edges in the signal.'
36 longdesc = '...'
37 author = 'Uwe Hermann'
38 email = 'uwe@hermann-uwe.de'
39 license = 'gplv2+'
40 inputs = ['logic']
41 outputs = ['transitioncounts']
42 probes = {}
43 options = {}
44
3643fc3f 45 def __init__(self, **kwargs):
b41ae47f
UH
46 self.probes = Decoder.probes.copy()
47
48 # TODO: Don't hardcode the number of channels.
49 self.channels = 8
50
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
56
3643fc3f
GM
57 def start(self, metadata):
58 self.unitsize = metadata["unitsize"]
59
b41ae47f
UH
60 def report(self):
61 pass
62
63 def decode(self, data):
64 """Counts the low->high and high->low transitions in the specified
65 channel(s) of the signal."""
1c6fa20f 66
b41ae47f
UH
67 # We should accept a list of samples and iterate...
68 for sample in sampleiter(data["data"], self.unitsize):
69
70 # TODO: Eliminate the need for ord().
71 s = ord(sample.data)
72
73 # Optimization: Skip identical samples (no transitions).
74 if self.lastsample == s:
75 continue
76
77 # Upon the first sample, store the initial values.
78 if self.lastsample == None:
79 self.lastsample = s
80 for i in range(self.channels):
81 self.oldbit[i] = (self.lastsample & (1 << i)) >> i
82
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):
ad2dc0de
UH
86 curbit = (s & (1 << i)) >> i
87 # Optimization: Skip identical bits (no transitions).
b41ae47f 88 if self.oldbit[i] == curbit:
ad2dc0de 89 continue
b41ae47f
UH
90 elif (self.oldbit[i] == 0 and curbit == 1):
91 self.rising[i] += 1
92 elif (self.oldbit[i] == 1 and curbit == 0):
93 self.falling[i] += 1
94 self.oldbit[i] = curbit
95
96 # Save the current sample as 'lastsample' for the next round.
97 self.lastsample = s
98
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]
102
103 # TODO: Which output format?
104 # TODO: How to only output something after the last chunk of data?
105 outdata = []
106 for i in range(self.channels):
107 outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
108 sigrok.put(outdata)
6fe63193 109
887d6cfa
UH
110# Use psyco (if available) as it results in huge performance improvements.
111try:
ad2dc0de
UH
112 import psyco
113 psyco.bind(decode)
887d6cfa 114except ImportError:
ad2dc0de 115 pass
887d6cfa 116
b41ae47f
UH
117import sigrok
118