summaryrefslogtreecommitdiff
path: root/decoders
diff options
context:
space:
mode:
authorGerhard Sittig <gerhard.sittig@gmx.net>2020-07-03 11:14:31 +0200
committerGerhard Sittig <gerhard.sittig@gmx.net>2020-07-07 22:50:46 +0200
commit3e962c2f5914300d63ccd5c135e4554c4314517f (patch)
tree7285ebd405f4debe8a6340561e94d9404e6ccbdf /decoders
parent6a1b97e6b19f96d3f8df2f92c8d2dd82c1ecc3bb (diff)
downloadlibsigrokdecode-3e962c2f5914300d63ccd5c135e4554c4314517f.tar.gz
libsigrokdecode-3e962c2f5914300d63ccd5c135e4554c4314517f.zip
timing: reduce "state", most action is local to .decode()
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'.
Diffstat (limited to 'decoders')
-rw-r--r--decoders/timing/pd.py43
1 files changed, 22 insertions, 21 deletions
diff --git a/decoders/timing/pd.py b/decoders/timing/pd.py
index 03d19e6..e687562 100644
--- a/decoders/timing/pd.py
+++ b/decoders/timing/pd.py
@@ -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