]> sigrok.org Git - libsigrokdecode.git/blame - scripts/transitioncounter.py
Various small decoder script fixes.
[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
5c55017c 21def decode(inbuf):
1c6fa20f
UH
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 37
a156f09e 38 # Initial values.
c11d3c15 39 oldbyte = inbuf[0]
a5fdab45 40 for i in range(channels):
930bd9d9 41 oldbit[i] = (oldbyte & (1 << i)) >> i
d0f3d677
UH
42
43 # Loop over all samples.
44 # TODO: Handle LAs with more/less than 8 channels.
45 for s in inbuf:
c11d3c15
UH
46 # Optimization: Skip identical bytes (no transitions).
47 if oldbyte == s:
48 continue
a5fdab45 49 for i in range(channels):
930bd9d9 50 curbit = (s & (1 << i)) >> i
c11d3c15
UH
51 # Optimization: Skip identical bits (no transitions).
52 if oldbit[i] == curbit:
53 continue
54 elif (oldbit[i] == 0 and curbit == 1):
d0f3d677
UH
55 rising[i] += 1
56 elif (oldbit[i] == 1 and curbit == 0):
57 falling[i] += 1
58 oldbit[i] = curbit
a156f09e 59 oldbyte = s
d0f3d677
UH
60
61 # Total number of transitions is the sum of rising and falling edges.
a5fdab45 62 for i in range(channels):
d0f3d677
UH
63 transitions[i] = rising[i] + falling[i]
64
65 outbuf += "Rising edges: "
a5fdab45 66 for i in range(channels):
d0f3d677
UH
67 outbuf += str(rising[i]) + " "
68 outbuf += "\nFalling edges: "
a5fdab45 69 for i in range(channels):
d0f3d677
UH
70 outbuf += str(falling[i]) + " "
71 outbuf += "\nTransitions: "
a5fdab45 72 for i in range(channels):
d0f3d677
UH
73 outbuf += str(transitions[i]) + " "
74 outbuf += "\n"
1c6fa20f 75
1c6fa20f
UH
76 return outbuf
77
6fe63193 78def register():
79 return {
80 'id': 'transitioncounter',
81 'name': 'Transition counter',
a156f09e 82 'desc': 'Count rising/falling edges',
6fe63193 83 'inputformats': ['raw'],
84 'signalnames': {}, # FIXME
85 'outputformats': ['transitioncounts'],
86 }
87
887d6cfa
UH
88# Use psyco (if available) as it results in huge performance improvements.
89try:
90 import psyco
5c55017c 91 psyco.bind(decode)
887d6cfa
UH
92except ImportError:
93 pass
94