]> sigrok.org Git - libsigrokdecode.git/blame - decoders/transitioncounter.py
transitioncounter.py: Convert to new API.
[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
45 def __init__(self, unitsize, **kwargs):
46 # Metadata comes in here, we don't care for now.
47 # print kwargs
48 self.unitsize = unitsize
49
50 self.probes = Decoder.probes.copy()
51
52 # TODO: Don't hardcode the number of channels.
53 self.channels = 8
54
55 self.lastsample = None
56 self.oldbit = [0] * self.channels
57 self.transitions = [0] * self.channels
58 self.rising = [0] * self.channels
59 self.falling = [0] * self.channels
60
61 def report(self):
62 pass
63
64 def decode(self, data):
65 """Counts the low->high and high->low transitions in the specified
66 channel(s) of the signal."""
1c6fa20f 67
b41ae47f
UH
68 # We should accept a list of samples and iterate...
69 for sample in sampleiter(data["data"], self.unitsize):
70
71 # TODO: Eliminate the need for ord().
72 s = ord(sample.data)
73
74 # Optimization: Skip identical samples (no transitions).
75 if self.lastsample == s:
76 continue
77
78 # Upon the first sample, store the initial values.
79 if self.lastsample == None:
80 self.lastsample = s
81 for i in range(self.channels):
82 self.oldbit[i] = (self.lastsample & (1 << i)) >> i
83
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):
ad2dc0de
UH
87 curbit = (s & (1 << i)) >> i
88 # Optimization: Skip identical bits (no transitions).
b41ae47f 89 if self.oldbit[i] == curbit:
ad2dc0de 90 continue
b41ae47f
UH
91 elif (self.oldbit[i] == 0 and curbit == 1):
92 self.rising[i] += 1
93 elif (self.oldbit[i] == 1 and curbit == 0):
94 self.falling[i] += 1
95 self.oldbit[i] = curbit
96
97 # Save the current sample as 'lastsample' for the next round.
98 self.lastsample = s
99
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]
103
104 # TODO: Which output format?
105 # TODO: How to only output something after the last chunk of data?
106 outdata = []
107 for i in range(self.channels):
108 outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
109 sigrok.put(outdata)
6fe63193 110
887d6cfa
UH
111# Use psyco (if available) as it results in huge performance improvements.
112try:
ad2dc0de
UH
113 import psyco
114 psyco.bind(decode)
887d6cfa 115except ImportError:
ad2dc0de 116 pass
887d6cfa 117
b41ae47f
UH
118import sigrok
119