]> sigrok.org Git - libsigrokdecode.git/commitdiff
pwm: Convert to PD API version 3
authorGerhard Sittig <redacted>
Tue, 6 Jun 2017 18:32:07 +0000 (20:32 +0200)
committerGerhard Sittig <redacted>
Tue, 6 Jun 2017 19:22:02 +0000 (21:22 +0200)
Have edges detected in common library code.

decoders/pwm/pd.py

index a0b0499d36fe4343364a7fbafa17bd7f17e9e2ae..0b7be458d881302e368bd778af0e5c4b2252ad8a 100644 (file)
@@ -21,7 +21,7 @@
 import sigrokdecode as srd
 
 class Decoder(srd.Decoder):
-    api_version = 2
+    api_version = 3
     id = 'pwm'
     name = 'PWM'
     longname = 'Pulse-width modulation'
@@ -50,11 +50,9 @@ class Decoder(srd.Decoder):
 
     def __init__(self):
         self.ss_block = self.es_block = None
-        self.first_transition = True
         self.first_samplenum = None
         self.start_samplenum = None
         self.end_samplenum = None
-        self.oldpin = None
         self.num_cycles = 0
         self.average = 0
 
@@ -93,57 +91,49 @@ class Decoder(srd.Decoder):
     def putb(self, data):
         self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
 
-    def decode(self, ss, es, data):
-
-        for (self.samplenum, pins) in data:
-            # Ignore identical samples early on (for performance reasons).
-            if self.oldpin == pins[0]:
-                continue
-
-            # Initialize self.oldpins with the first sample value.
-            if self.oldpin is None:
-                self.oldpin = pins[0]
-                continue
-
-            if self.first_transition:
-                # First rising edge
-                if self.oldpin != self.startedge:
-                    self.first_samplenum = self.samplenum
-                    self.start_samplenum = self.samplenum
-                    self.first_transition = False
+    def decode(self):
+
+        # Get the first rising edge.
+        pin, = self.wait({0: 'e'})
+        if pin != self.startedge:
+            pin, = self.wait({0: 'e'})
+        self.first_samplenum = self.samplenum
+        self.start_samplenum = self.samplenum
+
+        # Handle all next edges.
+        while True:
+            pin, = self.wait({0: 'e'})
+
+            if pin == self.startedge:
+                # Rising edge
+                # We are on a full cycle we can calculate
+                # the period, the duty cycle and its ratio.
+                period = self.samplenum - self.start_samplenum
+                duty = self.end_samplenum - self.start_samplenum
+                ratio = float(duty / period)
+
+                # This interval starts at this edge.
+                self.ss_block = self.start_samplenum
+                # Store the new rising edge position and the ending
+                # edge interval.
+                self.start_samplenum = self.es_block = self.samplenum
+
+                # Report the duty cycle in percent.
+                percent = float(ratio * 100)
+                self.putx([0, ['%f%%' % percent]])
+
+                # Report the duty cycle in the binary output.
+                self.putb([0, bytes([int(ratio * 256)])])
+
+                # Report the period in units of time.
+                period_t = float(period / self.samplerate)
+                self.putp(period_t)
+
+                # Update and report the new duty cycle average.
+                self.num_cycles += 1
+                self.average += percent
+                self.put(self.first_samplenum, self.es_block, self.out_average,
+                         float(self.average / self.num_cycles))
             else:
-                if self.oldpin != self.startedge:
-                    # Rising edge
-                    # We are on a full cycle we can calculate
-                    # the period, the duty cycle and its ratio.
-                    period = self.samplenum - self.start_samplenum
-                    duty = self.end_samplenum - self.start_samplenum
-                    ratio = float(duty / period)
-
-                    # This interval starts at this edge.
-                    self.ss_block = self.start_samplenum
-                    # Store the new rising edge position and the ending
-                    # edge interval.
-                    self.start_samplenum = self.es_block = self.samplenum
-
-                    # Report the duty cycle in percent.
-                    percent = float(ratio * 100)
-                    self.putx([0, ['%f%%' % percent]])
-
-                    # Report the duty cycle in the binary output.
-                    self.putb([0, bytes([int(ratio * 256)])])
-
-                    # Report the period in units of time.
-                    period_t = float(period / self.samplerate)
-                    self.putp(period_t)
-
-                    # Update and report the new duty cycle average.
-                    self.num_cycles += 1
-                    self.average += percent
-                    self.put(self.first_samplenum, self.es_block, self.out_average,
-                             float(self.average / self.num_cycles))
-                else:
-                    # Falling edge
-                    self.end_samplenum = self.ss_block = self.samplenum
-
-            self.oldpin = pins[0]
+                # Falling edge
+                self.end_samplenum = self.ss_block = self.samplenum