]> sigrok.org Git - libsigrokdecode.git/blame - scripts/transitioncounter.py
Python: Use range instead of xrange.
[libsigrokdecode.git] / scripts / 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##
20
1c6fa20f
UH
21def sigrokdecode_count_transitions(inbuf):
22 """Counts the low->high and high->low transitions in the specified
23 channel(s) of the signal."""
24
d0f3d677
UH
25 outbuf = ''
26
c11d3c15
UH
27 # FIXME: Get the data in the correct format in the first place.
28 inbuf = [ord(x) for x in inbuf]
29
d0f3d677
UH
30 # TODO: Don't hardcode the number of channels.
31 channels = 8
32
33 oldbit = [0] * channels
34 transitions = [0] * channels
35 rising = [0] * channels
36 falling = [0] * channels
1c6fa20f
UH
37
38 # print len(inbuf)
39 # print type(inbuf)
40
d0f3d677 41 # Presets...
c11d3c15 42 oldbyte = inbuf[0]
a5fdab45 43 for i in range(channels):
c11d3c15 44 oldbit[i] = (oldbyte & (1 << i)) != 0
d0f3d677
UH
45
46 # Loop over all samples.
47 # TODO: Handle LAs with more/less than 8 channels.
48 for s in inbuf:
c11d3c15
UH
49 # Optimization: Skip identical bytes (no transitions).
50 if oldbyte == s:
51 continue
a5fdab45 52 for i in range(channels):
d0f3d677 53 curbit = (s & (1 << i) != 0)
c11d3c15
UH
54 # Optimization: Skip identical bits (no transitions).
55 if oldbit[i] == curbit:
56 continue
57 elif (oldbit[i] == 0 and curbit == 1):
d0f3d677
UH
58 rising[i] += 1
59 elif (oldbit[i] == 1 and curbit == 0):
60 falling[i] += 1
61 oldbit[i] = curbit
62
63 # Total number of transitions is the sum of rising and falling edges.
a5fdab45 64 for i in range(channels):
d0f3d677
UH
65 transitions[i] = rising[i] + falling[i]
66
67 outbuf += "Rising edges: "
a5fdab45 68 for i in range(channels):
d0f3d677
UH
69 outbuf += str(rising[i]) + " "
70 outbuf += "\nFalling edges: "
a5fdab45 71 for i in range(channels):
d0f3d677
UH
72 outbuf += str(falling[i]) + " "
73 outbuf += "\nTransitions: "
a5fdab45 74 for i in range(channels):
d0f3d677
UH
75 outbuf += str(transitions[i]) + " "
76 outbuf += "\n"
1c6fa20f 77
1c6fa20f
UH
78 return outbuf
79
887d6cfa
UH
80# Use psyco (if available) as it results in huge performance improvements.
81try:
82 import psyco
83 psyco.bind(sigrokdecode_count_transitions)
84except ImportError:
85 pass
86