]> sigrok.org Git - libsigrokdecode.git/blame - scripts/transitioncounter.py
Implement a simple transition counter script.
[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
27 # TODO: Don't hardcode the number of channels.
28 channels = 8
29
30 oldbit = [0] * channels
31 transitions = [0] * channels
32 rising = [0] * channels
33 falling = [0] * channels
1c6fa20f
UH
34
35 # print len(inbuf)
36 # print type(inbuf)
37
d0f3d677
UH
38 # Presets...
39 s = ord(inbuf[0])
40 for i in xrange(channels):
41 curbit = (s & (1 << i) != 0)
42 oldbit[i] = curbit
43
44 # Loop over all samples.
45 # TODO: Handle LAs with more/less than 8 channels.
46 for s in inbuf:
47 s = ord(s) # FIXME
48 for i in xrange(channels):
49 curbit = (s & (1 << i) != 0)
50 if (oldbit[i] == 0 and curbit == 1):
51 rising[i] += 1
52 elif (oldbit[i] == 1 and curbit == 0):
53 falling[i] += 1
54 oldbit[i] = curbit
55
56 # Total number of transitions is the sum of rising and falling edges.
57 for i in xrange(channels):
58 transitions[i] = rising[i] + falling[i]
59
60 outbuf += "Rising edges: "
61 for i in xrange(channels):
62 outbuf += str(rising[i]) + " "
63 outbuf += "\nFalling edges: "
64 for i in xrange(channels):
65 outbuf += str(falling[i]) + " "
66 outbuf += "\nTransitions: "
67 for i in xrange(channels):
68 outbuf += str(transitions[i]) + " "
69 outbuf += "\n"
1c6fa20f 70
1c6fa20f
UH
71 return outbuf
72