summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerhard Sittig <gerhard.sittig@gmx.net>2020-07-18 20:30:43 +0200
committerGerhard Sittig <gerhard.sittig@gmx.net>2020-07-18 20:39:48 +0200
commitea0c4cb644431594c11b0ecf9449791f5e65b165 (patch)
treee8b00291c645f95ca266c518ebd9b81200ae0757
parent9e208e00f7b90c7ded552e84bcc5540be39f3e48 (diff)
downloadlibsigrokdecode-ea0c4cb644431594c11b0ecf9449791f5e65b165.tar.gz
libsigrokdecode-ea0c4cb644431594c11b0ecf9449791f5e65b165.zip
caliper: rephrase measurement annotation text construction
Unclutter the construction of the annotation text for measurements. Prefer one code block over multiple scatterred lines. Only do string formatting when an annotation gets emitted. Prefer .format() for its better control over generated text. Fixup remaining Python idioms in the unit conversion block while we are here.
-rw-r--r--decoders/caliper/pd.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/decoders/caliper/pd.py b/decoders/caliper/pd.py
index ff7d314..20a2a55 100644
--- a/decoders/caliper/pd.py
+++ b/decoders/caliper/pd.py
@@ -77,7 +77,7 @@ class Decoder(srd.Decoder):
self.put(ss, es, self.out_ann, [cls, data])
def decode(self):
- last_measurement = None
+ last_sent = None
timeout_ms = self.options['timeout_ms']
want_unit = self.options['unit']
show_all = self.options['changes'] == 'no'
@@ -123,26 +123,24 @@ class Decoder(srd.Decoder):
if negative:
number = -number
if is_inch:
- number = number / 2000
+ number /= 2000
if want_unit == 'mm':
number *= mm_per_inch
is_inch = False
else:
- number = number / 100
+ number /= 100
if want_unit == 'inch':
number = round(number / mm_per_inch, 4)
is_inch = True
+ unit = 'in' if is_inch else 'mm'
- units = "in" if is_inch else "mm"
-
- measurement = (str(number) + units)
-
- if show_all or measurement != last_measurement:
+ # Construct and emit an annotation.
+ if show_all or (number, unit) != last_sent:
self.putg(self.ss, self.es, 0, [
- measurement,
- str(number),
+ '{number}{unit}'.format(**locals()),
+ '{number}'.format(**locals()),
])
- last_measurement = measurement
+ last_sent = (number, unit)
- # Prepare for next packet.
+ # Reset internal state for the start of the next packet.
self.reset()