X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=decoders%2Ftiming%2Fpd.py;h=95c533211df78f85f4b5d9e91d7e5df6ddaccd27;hb=33687d6ed7b923662566e6a6f2761792bb087fb4;hp=5cb162f99a8ab597a9ac9620a5546060fa0da5b4;hpb=92adde514e1be3015ebd44da813fb6bec0001370;p=libsigrokdecode.git diff --git a/decoders/timing/pd.py b/decoders/timing/pd.py index 5cb162f..95c5332 100644 --- a/decoders/timing/pd.py +++ b/decoders/timing/pd.py @@ -15,50 +15,127 @@ ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## import sigrokdecode as srd +from collections import deque class SamplerateError(Exception): pass def normalize_time(t): - if t >= 1.0: - return '%.3f sec' % t - elif t >= 0.001: - return '%.3f msec' % (t * 1000.0) - elif t >= 0.000001: - return '%.3f usec' % (t * 1000.0 * 1000.0) - elif t >= 0.000000001: - return '%.3f nsec' % (t * 1000.0 * 1000.0 * 1000.0) + if abs(t) >= 1.0: + return '%.3f s (%.3f Hz)' % (t, (1/t)) + elif abs(t) >= 0.001: + if 1/t/1000 < 1: + return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t)) + else: + return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000) + elif abs(t) >= 0.000001: + if 1/t/1000/1000 < 1: + return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000) + else: + return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000) + elif abs(t) >= 0.000000001: + if 1/t/1000/1000/1000: + return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000) + else: + return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000) else: return '%f' % 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) + +class Ann: + (TIME, TERSE, AVG, DELTA,) = range(4) + class Decoder(srd.Decoder): - api_version = 2 + api_version = 3 id = 'timing' name = 'Timing' - longname = 'Timing calculation' + longname = 'Timing calculation with frequency and averaging' desc = 'Calculate time between edges.' license = 'gplv2+' inputs = ['logic'] - outputs = ['timing'] + outputs = [] + tags = ['Clock/timing', 'Util'] channels = ( {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, ) annotations = ( ('time', 'Time'), + ('terse', 'Terse'), + ('average', 'Average'), + ('delta', 'Delta'), ) annotation_rows = ( - ('time', 'Time', (0,)), + ('times', 'Times', (Ann.TIME, Ann.TERSE,)), + ('averages', 'Averages', (Ann.AVG,)), + ('deltas', 'Deltas', (Ann.DELTA,)), + ) + 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': '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, **kwargs): + def __init__(self): + self.reset() + + def reset(self): self.samplerate = None - self.oldpin = None - self.last_samplenum = None def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: @@ -67,28 +144,51 @@ class Decoder(srd.Decoder): def start(self): self.out_ann = self.register(srd.OUTPUT_ANN) - def decode(self, ss, es, data): + def decode(self): if not self.samplerate: raise SamplerateError('Cannot decode without samplerate.') + edge = self.options['edge'] + avg_period = self.options['avg_period'] + delta = self.options['delta'] == 'yes' + fmt = self.options['format'] + ss = None + last_n = deque() + last_t = None + while True: + if edge == 'rising': + pin = self.wait({Pin.DATA: 'r'}) + elif edge == 'falling': + pin = self.wait({Pin.DATA: 'f'}) + else: + pin = self.wait({Pin.DATA: 'e'}) - for (samplenum, (pin,)) in data: - # Ignore identical samples early on (for performance reasons). - if self.oldpin == pin: - continue - - if self.oldpin is None: - self.oldpin = pin - self.last_samplenum = samplenum + if not ss: + ss = self.samplenum continue + es = self.samplenum + sa = es - ss + t = sa / self.samplerate - if self.oldpin != pin: - samples = samplenum - self.last_samplenum - t = samples / self.samplerate + 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]) - # Report the timing normalized. - self.put(self.last_samplenum, samplenum, self.out_ann, - [0, [normalize_time(t)]]) + if avg_period > 0: + if t > 0: + last_n.append(t) + if len(last_n) > avg_period: + last_n.popleft() + average = sum(last_n) / len(last_n) + cls, txt = Ann.AVG, normalize_time(average) + self.put(ss, es, self.out_ann, [cls, [txt]]) + if last_t and delta: + cls, txt = Ann.DELTA, normalize_time(t - last_t) + self.put(ss, es, self.out_ann, [cls, [txt]]) - # Store data for next round. - self.last_samplenum = samplenum - self.oldpin = pin + last_t = t + ss = es