]> sigrok.org Git - libsigrokdecode.git/blobdiff - scripts/transitioncounter.py
Various small decoder script fixes.
[libsigrokdecode.git] / scripts / transitioncounter.py
index 05a07e378435062bcae05234296f1e659c465e4e..a6d331e85fc1af676d87edbebdce96c28d343a0a 100644 (file)
@@ -18,7 +18,7 @@
 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 ##
 
-def sigrokdecode_count_transitions(inbuf):
+def decode(inbuf):
        """Counts the low->high and high->low transitions in the specified
           channel(s) of the signal."""
 
@@ -35,13 +35,10 @@ def sigrokdecode_count_transitions(inbuf):
        rising = [0] * channels
        falling = [0] * channels
 
-       # print len(inbuf)
-       # print type(inbuf)
-
-       # Presets...
+       # Initial values.
        oldbyte = inbuf[0]
-       for i in xrange(channels):
-               oldbit[i] = (oldbyte & (1 << i)) != 0
+       for i in range(channels):
+               oldbit[i] = (oldbyte & (1 << i)) >> i
 
        # Loop over all samples.
        # TODO: Handle LAs with more/less than 8 channels.
@@ -49,8 +46,8 @@ def sigrokdecode_count_transitions(inbuf):
                # Optimization: Skip identical bytes (no transitions).
                if oldbyte == s:
                        continue
-               for i in xrange(channels):
-                       curbit = (s & (1 << i) != 0)
+               for i in range(channels):
+                       curbit = (s & (1 << i)) >> i
                        # Optimization: Skip identical bits (no transitions).
                        if oldbit[i] == curbit:
                                continue
@@ -59,28 +56,39 @@ def sigrokdecode_count_transitions(inbuf):
                        elif (oldbit[i] == 1 and curbit == 0):
                                falling[i] += 1
                        oldbit[i] = curbit
+               oldbyte = s
 
        # Total number of transitions is the sum of rising and falling edges.
-       for i in xrange(channels):
+       for i in range(channels):
                transitions[i] = rising[i] + falling[i]
 
        outbuf += "Rising edges:  "
-       for i in xrange(channels):
+       for i in range(channels):
                outbuf += str(rising[i]) + " "
        outbuf += "\nFalling edges: "
-       for i in xrange(channels):
+       for i in range(channels):
                outbuf += str(falling[i]) + " "
        outbuf += "\nTransitions:   "
-       for i in xrange(channels):
+       for i in range(channels):
                outbuf += str(transitions[i]) + " "
        outbuf += "\n"
 
        return outbuf
 
+def register():
+       return {
+               'id': 'transitioncounter',
+               'name': 'Transition counter',
+               'desc': 'Count rising/falling edges',
+               'inputformats': ['raw'],
+               'signalnames': {}, # FIXME
+               'outputformats': ['transitioncounts'],
+       }
+
 # Use psyco (if available) as it results in huge performance improvements.
 try:
        import psyco
-       psyco.bind(sigrokdecode_count_transitions)
+       psyco.bind(decode)
 except ImportError:
        pass