X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Ftiming%2Fpd.py;h=95c533211df78f85f4b5d9e91d7e5df6ddaccd27;hb=3db0d9750d6947080ae1cfe4ba1108a1687cf24f;hp=1295477161ce177045fa210953c1edb6ecb3b818;hpb=74c9c926a8936b6e7b781639908d1016e2088307;p=libsigrokdecode.git diff --git a/decoders/timing/pd.py b/decoders/timing/pd.py index 1295477..95c5332 100644 --- a/decoders/timing/pd.py +++ b/decoders/timing/pd.py @@ -45,20 +45,49 @@ def normalize_time(t): else: return '%f' % t -def terse_times(t): - if abs(t) >= 1e0: - t *= 1e0 - return ['{:.0f}s'.format(t), '{:.0f}'.format(t)] - if abs(t) >= 1e-3: - t *= 1e3 - return ['{:.0f}ms'.format(t), '{:.0f}'.format(t)] - if abs(t) >= 1e-6: - t *= 1e6 - return ['{:.0f}us'.format(t), '{:.0f}'.format(t)] - if abs(t) >= 1e-9: - t *= 1e9 - return ['{:.0f}ns'.format(t), '{:.0f}'.format(t)] - return ['{:f}'.format(t = t)] +def terse_times(t, fmt): + # Strictly speaking these variants are not used in the current + # implementation, but can reduce diffs during future maintenance. + if fmt == 'full': + return [normalize_time(t)] + # End of "forward compatibility". + + if fmt == 'samples': + # See below. No unit text, on purpose. + return ['{:d}'.format(t)] + + # Use caller specified scale, or automatically find one. + scale, unit = None, None + if fmt == 'terse-auto': + if abs(t) >= 1e0: + scale, unit = 1e0, 's' + elif abs(t) >= 1e-3: + scale, unit = 1e3, 'ms' + elif abs(t) >= 1e-6: + scale, unit = 1e6, 'us' + elif abs(t) >= 1e-9: + scale, unit = 1e9, 'ns' + elif abs(t) >= 1e-12: + scale, unit = 1e12, 'ps' + # Beware! Uses unit-less text when the user picked the scale. For + # more consistent output with less clutter, thus faster navigation + # by humans. Can also un-hide text at higher distance zoom levels. + elif fmt == 'terse-s': + scale, unit = 1e0, '' + elif fmt == 'terse-ms': + scale, unit = 1e3, '' + elif fmt == 'terse-us': + scale, unit = 1e6, '' + elif fmt == 'terse-ns': + scale, unit = 1e9, '' + elif fmt == 'terse-ps': + scale, unit = 1e12, '' + if scale: + t *= scale + return ['{:.0f}{}'.format(t, unit), '{:.0f}'.format(t)] + + # Unspecified format, and nothing auto-detected. + return ['{:f}'.format(t)] class Pin: (DATA,) = range(1) @@ -92,9 +121,14 @@ class Decoder(srd.Decoder): ) options = ( { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 }, - { 'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling') }, - { 'id': 'delta', 'desc': 'Show delta from last', 'default': 'no', 'values': ('yes', 'no') }, - { 'id': 'terse', 'desc': 'Show periods in terse format', 'default': 'no', 'values': ('yes', 'no') }, + { 'id': 'edge', 'desc': 'Edges to check', + 'default': 'any', 'values': ('any', 'rising', 'falling') }, + { 'id': 'delta', 'desc': 'Show delta from last', + 'default': 'no', 'values': ('yes', 'no') }, + { 'id': 'format', 'desc': 'Format of \'time\' annotation', + 'default': 'full', 'values': ('full', 'terse-auto', + 'terse-s', 'terse-ms', 'terse-us', 'terse-ns', 'terse-ps', + 'samples') }, ) def __init__(self): @@ -116,7 +150,7 @@ class Decoder(srd.Decoder): edge = self.options['edge'] avg_period = self.options['avg_period'] delta = self.options['delta'] == 'yes' - terse = self.options['terse'] == 'yes' + fmt = self.options['format'] ss = None last_n = deque() last_t = None @@ -132,15 +166,18 @@ class Decoder(srd.Decoder): ss = self.samplenum continue es = self.samplenum - samples = es - ss - t = samples / self.samplerate + sa = es - ss + t = sa / self.samplerate - if terse: - cls, txt = Ann.TERSE, terse_times(t) - self.put(ss, es, self.out_ann, [cls, txt]) - else: + if fmt == 'full': cls, txt = Ann.TIME, [normalize_time(t)] + elif fmt == 'samples': + cls, txt = Ann.TERSE, terse_times(sa, fmt) + else: + cls, txt = Ann.TERSE, terse_times(t, fmt) + if txt: self.put(ss, es, self.out_ann, [cls, txt]) + if avg_period > 0: if t > 0: last_n.append(t)