]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/pwm/pd.py
pwm: Rephrase edge and period detection, eliminate internal state
[libsigrokdecode.git] / decoders / pwm / pd.py
index 0b7be458d881302e368bd778af0e5c4b2252ad8a..a2fbb0fe26df4b0419e5c0c17b3438f61bedf870 100644 (file)
@@ -61,7 +61,6 @@ class Decoder(srd.Decoder):
             self.samplerate = value
 
     def start(self):
-        self.startedge = 0 if self.options['polarity'] == 'active-low' else 1
         self.out_ann = self.register(srd.OUTPUT_ANN)
         self.out_binary = self.register(srd.OUTPUT_BINARY)
         self.out_average = \
@@ -93,47 +92,42 @@ class Decoder(srd.Decoder):
 
     def decode(self):
 
-        # Get the first rising edge.
-        pin, = self.wait({0: 'e'})
-        if pin != self.startedge:
-            pin, = self.wait({0: 'e'})
+        # Wait for an "active" edge (depends on config). This starts
+        # the first full period of the inspected signal waveform.
+        self.wait({0: 'f' if self.options['polarity'] == 'active-low' else 'r'})
         self.first_samplenum = self.samplenum
-        self.start_samplenum = self.samplenum
 
-        # Handle all next edges.
+        # Keep getting samples for the period's middle and terminal edges.
+        # At the same time that last sample starts the next period.
         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:
-                # Falling edge
-                self.end_samplenum = self.ss_block = self.samplenum
+
+            # Get the next two edges. Setup some variables that get
+            # referenced in the calculation and in put() routines.
+            self.start_samplenum = self.samplenum
+            pins = self.wait({0: 'e'})
+            self.end_samplenum = self.samplenum
+            pins = self.wait({0: 'e'})
+            self.ss_block = self.start_samplenum
+            self.es_block = self.samplenum
+
+            # 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)
+
+            # 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))