]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/transitioncounter.py
srd: Finish consistency rename to ANN/PROTO.
[libsigrokdecode.git] / decoders / transitioncounter.py
index 1b105186116f69b859429b6e9c8fb06db109c06f..9d437d1bfa156e06b51a2b8e7daf297e02779bba 100644 (file)
 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 ##
 
-class Sample():
-    def __init__(self, data):
-        self.data = data
-    def probe(self, probe):
-        s = ord(self.data[probe / 8]) & (1 << (probe % 8))
-        return True if s else False
-
-def sampleiter(data, unitsize):
-    for i in range(0, len(data), unitsize):
-        yield(Sample(data[i:i+unitsize]))
-
-class Decoder():
+import sigrokdecode
+
+class Decoder(sigrokdecode.Decoder):
+    id = 'transitioncounter'
     name = 'Transition counter'
     longname = '...'
     desc = 'Counts rising/falling edges in the signal.'
@@ -39,37 +31,34 @@ class Decoder():
     license = 'gplv2+'
     inputs = ['logic']
     outputs = ['transitioncounts']
-    probes = {}
+    probes = [
+    ] # TODO?
     options = {}
 
-    def __init__(self, unitsize, **kwargs):
-        # Metadata comes in here, we don't care for now.
-        # print kwargs
-        self.unitsize = unitsize
-
-        self.probes = Decoder.probes.copy()
-
-        # TODO: Don't hardcode the number of channels.
-        self.channels = 8
-
+    def __init__(self, **kwargs):
+        self.out_proto = None
+        self.out_ann = None
+        self.channels = -1
         self.lastsample = None
-        self.oldbit = [0] * self.channels
-        self.transitions = [0] * self.channels
-        self.rising = [0] * self.channels
-        self.falling = [0] * self.channels
+
+    def start(self, metadata):
+        # self.out_proto = self.add(sigrokdecode.SRD_OUTPUT_PROTO, 'transitioncounter')
+        self.out_ann = self.add(sigrokdecode.SRD_OUTPUT_ANN, 'transitioncounter')
 
     def report(self):
         pass
 
-    def decode(self, data):
-        """Counts the low->high and high->low transitions in the specified
-           channel(s) of the signal."""
+    def decode(self, timeoffset, duration, data):
 
-        # We should accept a list of samples and iterate...
-        for sample in sampleiter(data["data"], self.unitsize):
+        for (samplenum, s) in data:
 
-            # TODO: Eliminate the need for ord().
-            s = ord(sample.data)
+            # ...
+            if self.channels == -1:
+                self.channels = len(s)
+                self.oldbit = [0] * self.channels
+                self.transitions = [0] * self.channels
+                self.rising = [0] * self.channels
+                self.falling = [0] * self.channels
 
             # Optimization: Skip identical samples (no transitions).
             if self.lastsample == s:
@@ -79,12 +68,12 @@ class Decoder():
             if self.lastsample == None:
                 self.lastsample = s
                 for i in range(self.channels):
-                    self.oldbit[i] = (self.lastsample & (1 << i)) >> i
+                    self.oldbit[i] = self.lastsample[i]
 
             # Iterate over all channels/probes in this sample.
             # Count rising and falling edges for each channel.
             for i in range(self.channels):
-                curbit = (s & (1 << i)) >> i
+                curbit = s[i]
                 # Optimization: Skip identical bits (no transitions).
                 if self.oldbit[i] == curbit:
                     continue
@@ -106,14 +95,8 @@ class Decoder():
         outdata = []
         for i in range(self.channels):
             outdata += [[self.transitions[i], self.rising[i], self.falling[i]]]
-        sigrok.put(outdata)
-
-# Use psyco (if available) as it results in huge performance improvements.
-try:
-    import psyco
-    psyco.bind(decode)
-except ImportError:
-    pass
 
-import sigrok
+        if outdata != []:
+            # self.put(0, 0, self.out_proto, out_proto)
+            self.put(0, 0, self.out_ann, outdata)