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