From: Gerhard Sittig Date: Sat, 25 Jul 2020 15:48:07 +0000 (+0200) Subject: ir_nec: rephrase annotation logic, symbolic names, format() call X-Git-Url: http://sigrok.org/gitweb/?a=commitdiff_plain;h=34ed4b3fb179f4ff7972879534a7395c90fe8d84;hp=32d1c3503d2c5402140c31fab37438861e0d7cb3;p=libsigrokdecode.git ir_nec: rephrase annotation logic, symbolic names, format() call Eliminate magic numbers, use symbolic names for annotation classes and pin numbers. Also vertically align text variants for zoom levels in the more complex cases, to simplify maintenance. Prefer .format() calls to improve readability in the more complex invocations. --- diff --git a/decoders/ir_nec/pd.py b/decoders/ir_nec/pd.py index 3ee3716..2e547d5 100644 --- a/decoders/ir_nec/pd.py +++ b/decoders/ir_nec/pd.py @@ -23,6 +23,14 @@ from .lists import * class SamplerateError(Exception): pass +class Pin: + IR, = range(1) + +class Ann: + BIT, AGC, LONG_PAUSE, SHORT_PAUSE, STOP_BIT, \ + LEADER_CODE, ADDR, ADDR_INV, CMD, CMD_INV, REPEAT_CODE, \ + REMOTE, WARN = range(13) + class Decoder(srd.Decoder): api_version = 3 id = 'ir_nec' @@ -57,10 +65,10 @@ class Decoder(srd.Decoder): ('warning', 'Warning'), ) annotation_rows = ( - ('bits', 'Bits', (0, 1, 2, 3, 4)), - ('fields', 'Fields', (5, 6, 7, 8, 9, 10)), - ('remote-vals', 'Remote', (11,)), - ('warnings', 'Warnings', (12,)), + ('bits', 'Bits', (Ann.BIT, Ann.AGC, Ann.LONG_PAUSE, Ann.SHORT_PAUSE, Ann.STOP_BIT)), + ('fields', 'Fields', (Ann.LEADER_CODE, Ann.ADDR, Ann.ADDR_INV, Ann.CMD, Ann.CMD_INV, Ann.REPEAT_CODE)), + ('remote-vals', 'Remote', (Ann.REMOTE,)), + ('warnings', 'Warnings', (Ann.WARN,)), ) def putx(self, data): @@ -71,23 +79,31 @@ class Decoder(srd.Decoder): def putd(self, data): name = self.state.title() - d = {'ADDRESS': 6, 'ADDRESS#': 7, 'COMMAND': 8, 'COMMAND#': 9} + d = {'ADDRESS': Ann.ADDR, 'ADDRESS#': Ann.ADDR_INV, + 'COMMAND': Ann.CMD, 'COMMAND#': Ann.CMD_INV} s = {'ADDRESS': ['ADDR', 'A'], 'ADDRESS#': ['ADDR#', 'A#'], 'COMMAND': ['CMD', 'C'], 'COMMAND#': ['CMD#', 'C#']} - self.putx([d[self.state], ['%s: 0x%02X' % (name, data), - '%s: 0x%02X' % (s[self.state][0], data), - '%s: 0x%02X' % (s[self.state][1], data), s[self.state][1]]]) + self.putx([d[self.state], [ + '{}: 0x{:02X}'.format(name, data), + '{}: 0x{:02X}'.format(s[self.state][0], data), + '{}: 0x{:02X}'.format(s[self.state][1], data), + s[self.state][1], + ]]) def putstop(self, ss): self.put(ss, ss + self.stop, self.out_ann, - [4, ['Stop bit', 'Stop', 'St', 'S']]) + [Ann.STOP_BIT, ['Stop bit', 'Stop', 'St', 'S']]) def putpause(self, p): self.put(self.ss_start, self.ss_other_edge, self.out_ann, - [1, ['AGC pulse', 'AGC', 'A']]) - idx = 2 if p == 'Long' else 3 - self.put(self.ss_other_edge, self.samplenum, self.out_ann, - [idx, [p + ' pause', '%s-pause' % p[0], '%sP' % p[0], 'P']]) + [Ann.AGC, ['AGC pulse', 'AGC', 'A']]) + idx = Ann.LONG_PAUSE if p == 'Long' else Ann.SHORT_PAUSE + self.put(self.ss_other_edge, self.samplenum, self.out_ann, [idx, [ + '{} pause'.format(p), + '{}-pause'.format(p[0]), + '{}P'.format(p[0]), + 'P', + ]]) def putremote(self): dev = address.get(self.addr, 'Unknown device') @@ -96,9 +112,11 @@ class Decoder(srd.Decoder): btn = ['Unknown', 'Unk'] else: btn = buttons.get(self.cmd, ['Unknown', 'Unk']) - self.put(self.ss_remote, self.ss_bit + self.stop, self.out_ann, - [11, ['%s: %s' % (dev, btn[0]), '%s: %s' % (dev, btn[1]), - '%s' % btn[1]]]) + self.put(self.ss_remote, self.ss_bit + self.stop, self.out_ann, [Ann.REMOTE, [ + '{}: {}'.format(dev, btn[0]), + '{}: {}'.format(dev, btn[1]), + '{}'.format(btn[1]), + ]]) def __init__(self): self.reset() @@ -133,7 +151,7 @@ class Decoder(srd.Decoder): elif self.compare_with_tolerance(tick, self.daone): ret = 1 if ret in (0, 1): - self.putb([0, ['%d' % ret]]) + self.putb([Ann.BIT, ['{:d}'.format(ret)]]) self.data |= (ret << self.count) # LSB-first self.count = self.count + 1 self.ss_bit = self.samplenum @@ -151,7 +169,7 @@ class Decoder(srd.Decoder): if ret == 0: self.putd(self.data >> 8) else: - self.putx([12, ['%s error: 0x%04X' % (name, self.data)]]) + self.putx([Ann.WARN, ['{} error: 0x{:04X}'.format(name, self.data)]]) self.data = self.count = 0 self.ss_bit = self.ss_start = self.samplenum return ret == 0 @@ -182,7 +200,7 @@ class Decoder(srd.Decoder): # active period, but will shift their signal changes by one # carrier period before they get passed to decoding logic. if cd_count: - (cur_ir,) = self.wait([{0: 'e'}, {'skip': cd_count}]) + (cur_ir,) = self.wait([{Pin.IR: 'e'}, {'skip': cd_count}]) if self.matched[0]: cur_ir = self.active if cur_ir == prev_ir: @@ -190,7 +208,7 @@ class Decoder(srd.Decoder): prev_ir = cur_ir self.ir = cur_ir else: - (self.ir,) = self.wait({0: 'e'}) + (self.ir,) = self.wait({Pin.IR: 'e'}) if self.ir != self.active: # Save the non-active edge, then wait for the next edge. @@ -203,7 +221,7 @@ class Decoder(srd.Decoder): if self.state == 'IDLE': if self.compare_with_tolerance(b, self.lc): self.putpause('Long') - self.putx([5, ['Leader code', 'Leader', 'LC', 'L']]) + self.putx([Ann.LEADER_CODE, ['Leader code', 'Leader', 'LC', 'L']]) self.ss_remote = self.ss_start self.data = self.count = 0 self.state = 'ADDRESS' @@ -211,7 +229,7 @@ class Decoder(srd.Decoder): self.putpause('Short') self.putstop(self.samplenum) self.samplenum += self.stop - self.putx([10, ['Repeat code', 'Repeat', 'RC', 'R']]) + self.putx([Ann.REPEAT_CODE, ['Repeat code', 'Repeat', 'RC', 'R']]) self.data = self.count = 0 self.ss_bit = self.ss_start = self.samplenum elif self.state == 'ADDRESS':