]> 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 e0203a3b7d484ae41ae9d6782ca20a05b50fa6b0..a2fbb0fe26df4b0419e5c0c17b3438f61bedf870 100644 (file)
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
 import sigrokdecode as srd
 
 class Decoder(srd.Decoder):
-    api_version = 2
+    api_version = 3
     id = 'pwm'
     name = 'PWM'
     longname = 'Pulse-width modulation'
@@ -49,13 +48,11 @@ class Decoder(srd.Decoder):
         ('raw', 'RAW file'),
     )
 
-    def __init__(self, **kwargs):
-        self.ss = self.es = None
-        self.first_transition = True
+    def __init__(self):
+        self.ss_block = self.es_block = None
         self.first_samplenum = None
         self.start_samplenum = None
         self.end_samplenum = None
-        self.oldpin = None
         self.num_cycles = 0
         self.average = 0
 
@@ -64,15 +61,14 @@ 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_bin = self.register(srd.OUTPUT_BINARY)
+        self.out_binary = self.register(srd.OUTPUT_BINARY)
         self.out_average = \
             self.register(srd.OUTPUT_META,
                           meta=(float, 'Average', 'PWM base (cycle) frequency'))
 
     def putx(self, data):
-        self.put(self.ss, self.es, self.out_ann, data)
+        self.put(self.ss_block, self.es_block, self.out_ann, data)
 
     def putp(self, period_t):
         # Adjust granularity.
@@ -89,62 +85,49 @@ class Decoder(srd.Decoder):
         else:
             period_s = '%.1f ms' % (period_t * 1e3)
 
-        self.put(self.ss, self.es, self.out_ann, [1, [period_s]])
+        self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
 
     def putb(self, data):
-        self.put(self.num_cycles, self.num_cycles, self.out_bin, 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
-            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 = self.start_samplenum
-                    # Store the new rising edge position and the ending
-                    # edge interval.
-                    self.start_samplenum = self.es = 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, self.out_average,
-                             float(self.average / self.num_cycles))
-                else:
-                    # Falling edge
-                    self.end_samplenum = self.ss = self.samplenum
-
-            self.oldpin = pins[0]
+        self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
+
+    def decode(self):
+
+        # 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
+
+        # Keep getting samples for the period's middle and terminal edges.
+        # At the same time that last sample starts the next period.
+        while True:
+
+            # 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))