2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
23 """Counts the low->high and high->low transitions in the specified
24 channel(s) of the signal."""
30 """Counts the low->high and high->low transitions in the specified
31 channel(s) of the signal."""
35 # FIXME: Get the data in the correct format in the first place.
36 inbuf = [ord(x) for x in inbuf]
38 # TODO: Don't hardcode the number of channels.
41 oldbit = [0] * channels
42 transitions = [0] * channels
43 rising = [0] * channels
44 falling = [0] * channels
48 for i in range(channels):
49 oldbit[i] = (oldbyte & (1 << i)) >> i
51 # Loop over all samples.
52 # TODO: Handle LAs with more/less than 8 channels.
54 # Optimization: Skip identical bytes (no transitions).
57 for i in range(channels):
58 curbit = (s & (1 << i)) >> i
59 # Optimization: Skip identical bits (no transitions).
60 if oldbit[i] == curbit:
62 elif (oldbit[i] == 0 and curbit == 1):
64 elif (oldbit[i] == 1 and curbit == 0):
69 # Total number of transitions is the sum of rising and falling edges.
70 for i in range(channels):
71 transitions[i] = rising[i] + falling[i]
73 outbuf += "Rising edges: "
74 for i in range(channels):
75 outbuf += str(rising[i]) + " "
76 outbuf += "\nFalling edges: "
77 for i in range(channels):
78 outbuf += str(falling[i]) + " "
79 outbuf += "\nTransitions: "
80 for i in range(channels):
81 outbuf += str(transitions[i]) + " "
87 'id': 'transitioncounter',
88 'name': 'Transition counter',
90 'desc': 'Counts rising/falling edges in the signal.',
92 'author': 'Uwe Hermann',
93 'email': 'uwe@hermann-uwe.de',
96 'out': ['transitioncounts'],
107 # Use psyco (if available) as it results in huge performance improvements.