X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=scripts%2Ftransitioncounter.py;h=c16ce46aacc67a90cb5c00bec11a66ba6416dbf5;hp=ba799ec1fcf5fdef290625514f8bc736bf02dddd;hb=930bd9d961b9a13259ffb4997be9fa6ebcdb9da9;hpb=1c6fa20f549c0565ce4fae6ddf7d5eb60db2ddea diff --git a/scripts/transitioncounter.py b/scripts/transitioncounter.py index ba799ec..c16ce46 100644 --- a/scripts/transitioncounter.py +++ b/scripts/transitioncounter.py @@ -18,20 +18,69 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## -# FIXME: So far this is just a simple test for the Python embedding code. def sigrokdecode_count_transitions(inbuf): """Counts the low->high and high->low transitions in the specified channel(s) of the signal.""" - outbuf = 'AB' # FIXME + 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 + + oldbit = [0] * channels + transitions = [0] * channels + rising = [0] * channels + falling = [0] * channels # print len(inbuf) # print type(inbuf) - # print inbuf - # print str(inbuf[0]) - # print str(inbuf[1]) + # Presets... + 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: + # 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 range(channels): + transitions[i] = rising[i] + falling[i] + + outbuf += "Rising edges: " + for i in range(channels): + outbuf += str(rising[i]) + " " + outbuf += "\nFalling edges: " + for i in range(channels): + outbuf += str(falling[i]) + " " + outbuf += "\nTransitions: " + for i in range(channels): + outbuf += str(transitions[i]) + " " + outbuf += "\n" - # return inbuf return outbuf +# Use psyco (if available) as it results in huge performance improvements. +try: + import psyco + psyco.bind(sigrokdecode_count_transitions) +except ImportError: + pass +