summaryrefslogtreecommitdiff
path: root/decoders
diff options
context:
space:
mode:
authorGerhard Sittig <gerhard.sittig@gmx.net>2021-01-13 20:12:07 +0100
committerGerhard Sittig <gerhard.sittig@gmx.net>2021-01-13 20:25:09 +0100
commit88b62601d661281be74fde8e3913e66dbe2fa7b4 (patch)
tree9d92c75445db963e3a9cbf976951063dd2fc5670 /decoders
parent93dd1225b285cdddaf9fc7f0bfa9cd6848595585 (diff)
downloadlibsigrokdecode-88b62601d661281be74fde8e3913e66dbe2fa7b4.tar.gz
libsigrokdecode-88b62601d661281be74fde8e3913e66dbe2fa7b4.zip
seven_segment: cosmetics in decode(), reduce redundancy
"Un-clutter" the decode() routine. Group related instructions to improve readability. Drop redundant statements where common code can handle all cases. Also fixes an unconditional access to the optional decimal point input signal (which had caused a runtime error, and ceased decoding).
Diffstat (limited to 'decoders')
-rw-r--r--decoders/seven_segment/pd.py38
1 files changed, 14 insertions, 24 deletions
diff --git a/decoders/seven_segment/pd.py b/decoders/seven_segment/pd.py
index 5708fd5..2172269 100644
--- a/decoders/seven_segment/pd.py
+++ b/decoders/seven_segment/pd.py
@@ -151,46 +151,36 @@ class Decoder(srd.Decoder):
def decode(self):
oldpins = self.wait()
-
- # Check if at least the 7 signals are present.
- if False in [p in (0, 1) for p in oldpins[:7]]:
- raise ChannelError('7 or 8 pins have to be present.')
-
lastpos = self.samplenum
+ # Check mandatory and optional decoder input signals.
+ if False in [p in (0, 1) for p in oldpins[:7]]:
+ raise ChannelError('Need at least segments A-G.')
self.have_dp = self.has_channel(7)
+ seg_count = 8 if self.have_dp else 7
- conditions = [{0: 'e'}, {1: 'e'}, {2: 'e'}, {3: 'e'}, {4: 'e'}, {5: 'e'}, {6: 'e'}]
-
- if self.have_dp:
- conditions.append({7: 'e'})
-
+ conditions = [{i: 'e'} for i in range(seg_count)]
while True:
# Wait for any change.
pins = self.wait(conditions)
+ # Invert all data lines if a common anode display is used.
if self.options['polarity'] == 'common-anode':
- # Invert all data lines if a common anode display is used.
- if self.have_dp:
- oldpins = tuple((1 - state for state in oldpins))
- else:
- oldpins = tuple((1 - state for state in oldpins[:7]))
+ oldpins = tuple((1 - state for state in oldpins[:seg_count]))
# Convert to character string.
digit = self.pins_to_hex(oldpins[:7])
-
if digit is None and self.options['show_unknown'] == 'yes':
digit = '#'
+ # Emit annotation when conversion succeeded.
+ # Optionally present the decimal point when active.
if digit is not None:
- dp = oldpins[7]
-
- # Check if decimal point is present and active.
- if self.have_dp and dp == 1:
- digit += '.'
-
+ if self.have_dp:
+ dp = oldpins[7]
+ if dp == 1:
+ digit += '.'
self.putb(lastpos, self.samplenum, [0, [digit]])
- lastpos = self.samplenum
-
oldpins = pins
+ lastpos = self.samplenum