]> sigrok.org Git - libsigrokdecode.git/blobdiff - scripts/transitioncounter.py
Various Python decoder infrastructure improvements.
[libsigrokdecode.git] / scripts / transitioncounter.py
index cbf80680704aedfc0d53be61e363b30839047270..4064d2f5debc71da5dd732d3afd6f20d418a09c6 100644 (file)
 ## 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."""
 
        outbuf = ''
 
+       # FIXME: Get the data in the correct format in the first place.
+       inbuf = [ord(x) for x in inbuf]
+
        # TODO: Don't hardcode the number of channels.
        channels = 8
 
@@ -36,37 +39,59 @@ def sigrokdecode_count_transitions(inbuf):
        # print type(inbuf)
 
        # Presets...
-       s = ord(inbuf[0])
-       for i in xrange(channels):
-               curbit = (s & (1 << i) != 0)
-               oldbit[i] = curbit
+       oldbyte = inbuf[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.
        for s in inbuf:
-               s = ord(s) # FIXME
-               for i in xrange(channels):
-                       curbit = (s & (1 << i) != 0)
-                       if (oldbit[i] == 0 and curbit == 1):
+               # Optimization: Skip identical bytes (no transitions).
+               if oldbyte == s:
+                       continue
+               for i in range(channels):
+                       curbit = (s & (1 << i)) >> i
+                       # Optimization: Skip identical bits (no transitions).
+                       if oldbit[i] == curbit:
+                               continue
+                       elif (oldbit[i] == 0 and curbit == 1):
                                rising[i] += 1
                        elif (oldbit[i] == 1 and curbit == 0):
                                falling[i] += 1
                        oldbit[i] = curbit
 
        # 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': 'TODO',
+               'func': 'decode',
+               'inputformats': ['raw'],
+               'signalnames': {}, # FIXME
+               'outputformats': ['transitioncounts'],
+       }
+
+# Use psyco (if available) as it results in huge performance improvements.
+try:
+       import psyco
+       psyco.bind(decode)
+except ImportError:
+       pass
+