]> sigrok.org Git - libsigrokdecode.git/commitdiff
timing: reduce "state", most action is local to .decode()
authorGerhard Sittig <redacted>
Fri, 3 Jul 2020 09:14:31 +0000 (11:14 +0200)
committerGerhard Sittig <redacted>
Tue, 7 Jul 2020 20:50:46 +0000 (22:50 +0200)
Reduce the number of self members, use local variables instead for data
which is strictly kept within a method and need not remain across calls.
Move options dictionary lookups out of the main loop, as the previous
implementation already did with 'edge'.

decoders/timing/pd.py

index 03d19e6880d955ccc672b37a1ed127890503cae7..e6875622cd60ff95abf69d869fddb70f15f9c383 100644 (file)
@@ -85,9 +85,6 @@ class Decoder(srd.Decoder):
 
     def reset(self):
         self.samplerate = None
-        self.last_samplenum = None
-        self.last_n = deque()
-        self.last_t = None
 
     def metadata(self, key, value):
         if key == srd.SRD_CONF_SAMPLERATE:
@@ -95,38 +92,42 @@ class Decoder(srd.Decoder):
 
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
-        self.edge = self.options['edge']
 
     def decode(self):
         if not self.samplerate:
             raise SamplerateError('Cannot decode without samplerate.')
+        edge = self.options['edge']
+        avg_period = self.options['avg_period']
+        last_samplenum = None
+        last_n = deque()
+        last_t = None
         while True:
-            if self.edge == 'rising':
+            if edge == 'rising':
                 pin = self.wait({Pin.DATA: 'r'})
-            elif self.edge == 'falling':
+            elif edge == 'falling':
                 pin = self.wait({Pin.DATA: 'f'})
             else:
                 pin = self.wait({Pin.DATA: 'e'})
 
-            if not self.last_samplenum:
-                self.last_samplenum = self.samplenum
+            if not last_samplenum:
+                last_samplenum = self.samplenum
                 continue
-            samples = self.samplenum - self.last_samplenum
+            samples = self.samplenum - last_samplenum
             t = samples / self.samplerate
 
             if t > 0:
-                self.last_n.append(t)
-            if len(self.last_n) > self.options['avg_period']:
-                self.last_n.popleft()
+                last_n.append(t)
+            if len(last_n) > avg_period:
+                last_n.popleft()
 
-            self.put(self.last_samplenum, self.samplenum, self.out_ann,
+            self.put(last_samplenum, self.samplenum, self.out_ann,
                      [Ann.TIME, [normalize_time(t)]])
-            if self.options['avg_period'] > 0:
-                self.put(self.last_samplenum, self.samplenum, self.out_ann,
-                         [Ann.AVG, [normalize_time(sum(self.last_n) / len(self.last_n))]])
-            if self.last_t and self.options['delta'] == 'yes':
-                self.put(self.last_samplenum, self.samplenum, self.out_ann,
-                         [Ann.DELTA, [normalize_time(t - self.last_t)]])
+            if avg_period > 0:
+                self.put(last_samplenum, self.samplenum, self.out_ann,
+                         [Ann.AVG, [normalize_time(sum(last_n) / len(last_n))]])
+            if last_t and self.options['delta'] == 'yes':
+                self.put(last_samplenum, self.samplenum, self.out_ann,
+                         [Ann.DELTA, [normalize_time(t - last_t)]])
 
-            self.last_t = t
-            self.last_samplenum = self.samplenum
+            last_t = t
+            last_samplenum = self.samplenum