]> sigrok.org Git - libsigrokdecode.git/commitdiff
pwm: Eliminate more decoder "state"
authorGerhard Sittig <redacted>
Sun, 18 Jun 2017 11:38:15 +0000 (13:38 +0200)
committerGerhard Sittig <redacted>
Tue, 20 Jun 2017 21:10:03 +0000 (23:10 +0200)
Move more items from the Python object's members to local variables of the
decode() method, as they are used there exclusively.

Mark a spot where the binary output references suspicious "sample numbers".

decoders/pwm/pd.py

index a2fbb0fe26df4b0419e5c0c17b3438f61bedf870..d6411ae42fd8fea4740cd1a21b9451a161743eec 100644 (file)
@@ -50,11 +50,7 @@ class Decoder(srd.Decoder):
 
     def __init__(self):
         self.ss_block = self.es_block = None
-        self.first_samplenum = None
-        self.start_samplenum = None
-        self.end_samplenum = None
         self.num_cycles = 0
-        self.average = 0
 
     def metadata(self, key, value):
         if key == srd.SRD_CONF_SAMPLERATE:
@@ -88,9 +84,14 @@ class Decoder(srd.Decoder):
         self.put(self.ss_block, self.es_block, self.out_ann, [1, [period_s]])
 
     def putb(self, data):
+        # TODO Are these ss/es specs appropriate? It's the same value,
+        # which represents a mere period counter, not sample numbers.
+        # Probably should be:
+        # self.put(self.ss_block, self.es_block, self.out_binary, data)
         self.put(self.num_cycles, self.num_cycles, self.out_binary, data)
 
     def decode(self):
+        average = 0
 
         # Wait for an "active" edge (depends on config). This starts
         # the first full period of the inspected signal waveform.
@@ -103,16 +104,16 @@ class Decoder(srd.Decoder):
 
             # Get the next two edges. Setup some variables that get
             # referenced in the calculation and in put() routines.
-            self.start_samplenum = self.samplenum
+            start_samplenum = self.samplenum
             pins = self.wait({0: 'e'})
-            self.end_samplenum = self.samplenum
+            end_samplenum = self.samplenum
             pins = self.wait({0: 'e'})
-            self.ss_block = self.start_samplenum
+            self.ss_block = 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
+            period = self.samplenum - start_samplenum
+            duty = end_samplenum - start_samplenum
             ratio = float(duty / period)
 
             # Report the duty cycle in percent.
@@ -128,6 +129,6 @@ class Decoder(srd.Decoder):
 
             # Update and report the new duty cycle average.
             self.num_cycles += 1
-            self.average += percent
+            average += percent
             self.put(self.first_samplenum, self.es_block, self.out_average,
-                     float(self.average / self.num_cycles))
+                     float(average / self.num_cycles))