]> sigrok.org Git - libsigrokdecode.git/blame - decoders/transitioncounter.py
Partial implementation of the streaming PD 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##
922763ab 20import sigrok
1c6fa20f 21
922763ab
KS
22def decode(l):
23 """Counts the low->high and high->low transitions in the specified
24 channel(s) of the signal."""
25
26 print(l)
27 sigrok.put(l)
28
29def decode2(inbuf):
1c6fa20f
UH
30 """Counts the low->high and high->low transitions in the specified
31 channel(s) of the signal."""
32
d0f3d677
UH
33 outbuf = ''
34
c11d3c15
UH
35 # FIXME: Get the data in the correct format in the first place.
36 inbuf = [ord(x) for x in inbuf]
37
d0f3d677
UH
38 # TODO: Don't hardcode the number of channels.
39 channels = 8
40
41 oldbit = [0] * channels
42 transitions = [0] * channels
43 rising = [0] * channels
44 falling = [0] * channels
1c6fa20f 45
a156f09e 46 # Initial values.
c11d3c15 47 oldbyte = inbuf[0]
a5fdab45 48 for i in range(channels):
930bd9d9 49 oldbit[i] = (oldbyte & (1 << i)) >> i
d0f3d677
UH
50
51 # Loop over all samples.
52 # TODO: Handle LAs with more/less than 8 channels.
53 for s in inbuf:
c11d3c15
UH
54 # Optimization: Skip identical bytes (no transitions).
55 if oldbyte == s:
56 continue
a5fdab45 57 for i in range(channels):
930bd9d9 58 curbit = (s & (1 << i)) >> i
c11d3c15
UH
59 # Optimization: Skip identical bits (no transitions).
60 if oldbit[i] == curbit:
61 continue
62 elif (oldbit[i] == 0 and curbit == 1):
d0f3d677
UH
63 rising[i] += 1
64 elif (oldbit[i] == 1 and curbit == 0):
65 falling[i] += 1
66 oldbit[i] = curbit
a156f09e 67 oldbyte = s
d0f3d677
UH
68
69 # Total number of transitions is the sum of rising and falling edges.
a5fdab45 70 for i in range(channels):
d0f3d677
UH
71 transitions[i] = rising[i] + falling[i]
72
73 outbuf += "Rising edges: "
a5fdab45 74 for i in range(channels):
d0f3d677
UH
75 outbuf += str(rising[i]) + " "
76 outbuf += "\nFalling edges: "
a5fdab45 77 for i in range(channels):
d0f3d677
UH
78 outbuf += str(falling[i]) + " "
79 outbuf += "\nTransitions: "
a5fdab45 80 for i in range(channels):
d0f3d677
UH
81 outbuf += str(transitions[i]) + " "
82 outbuf += "\n"
1c6fa20f 83
1c6fa20f
UH
84 return outbuf
85
922763ab
KS
86register = {
87 'id': 'transitioncounter',
88 'name': 'Transition counter',
89 'longname': '...',
90 'desc': 'Counts rising/falling edges in the signal.',
91 'longdesc': '...',
92 'author': 'Uwe Hermann',
93 'email': 'uwe@hermann-uwe.de',
94 'license': 'gplv2+',
95 'in': ['logic'],
96 'out': ['transitioncounts'],
97 'probes': [
98 # All probes.
99 ],
100 'options': {
101 # No options so far.
102 },
103 # 'start': start,
104 # 'report': report,
105}
6fe63193 106
887d6cfa
UH
107# Use psyco (if available) as it results in huge performance improvements.
108try:
109 import psyco
5c55017c 110 psyco.bind(decode)
887d6cfa
UH
111except ImportError:
112 pass
113