]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/timing/pd.py
uart: handle zero stop bits configuration
[libsigrokdecode.git] / decoders / timing / pd.py
index f14b64f4853abe3ada07cfbb4edd958faadb81ba..95c533211df78f85f4b5d9e91d7e5df6ddaccd27 100644 (file)
@@ -45,11 +45,55 @@ def normalize_time(t):
     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, AVG, DELTA,) = range(3)
+    (TIME, TERSE, AVG, DELTA,) = range(4)
 
 class Decoder(srd.Decoder):
     api_version = 3
@@ -66,18 +110,25 @@ class Decoder(srd.Decoder):
     )
     annotations = (
         ('time', 'Time'),
+        ('terse', 'Terse'),
         ('average', 'Average'),
         ('delta', 'Delta'),
     )
     annotation_rows = (
-        ('times', 'Times', (Ann.TIME,)),
+        ('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': '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):
@@ -98,6 +149,8 @@ class Decoder(srd.Decoder):
             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
@@ -113,22 +166,29 @@ 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 t > 0:
-                last_n.append(t)
-            if len(last_n) > avg_period:
-                last_n.popleft()
+            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])
 
-            self.put(ss, es, self.out_ann,
-                     [Ann.TIME, [normalize_time(t)]])
             if avg_period > 0:
-                self.put(ss, es, self.out_ann,
-                         [Ann.AVG, [normalize_time(sum(last_n) / len(last_n))]])
-            if last_t and self.options['delta'] == 'yes':
-                self.put(ss, es, self.out_ann,
-                         [Ann.DELTA, [normalize_time(t - last_t)]])
+                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]])
 
             last_t = t
             ss = es