]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/morse/pd.py
morse: fix for #1278
[libsigrokdecode.git] / decoders / morse / pd.py
index a1e3c23072d6bad62a871ecfa80832ed4011349a..9a83b63a84b390df109cd55af1e694d88791da03 100644 (file)
@@ -154,8 +154,6 @@ class Decoder(srd.Decoder):
         # Annotate symbols, emit symbols, handle timeout via token.
 
         timeunit = self.options['timeunit']
-        if self.samplerate is None:
-            self.samplerate = 1.0
 
         self.wait({0: 'r'})
         prevtime = self.samplenum # Time of an actual edge.
@@ -167,7 +165,7 @@ class Decoder(srd.Decoder):
             curtime = self.samplenum
             dt = (curtime - prevtime) / self.samplerate
             units = dt / timeunit
-            iunits = round(units)
+            iunits = int(max(1, round(units)))
             error = abs(units - iunits)
 
             symbol = (pval, iunits)
@@ -177,15 +175,17 @@ class Decoder(srd.Decoder):
                 continue
 
             self.put(prevtime, curtime, self.out_ann, [0, ['{:.3g}'.format(dt)]])
-            self.put(prevtime, curtime, self.out_ann, [1, ['{:.1f}*{:.3g}'.format(units, timeunit)]])
 
             if symbol in symbols:
+                self.put(prevtime, curtime, self.out_ann, [1, ['{:.1f}*{:.3g}'.format(units, timeunit)]])
                 yield (prevtime, curtime, symbol)
+            else:
+                self.put(prevtime, curtime, self.out_ann, [1, ['!! {:.1f}*{:.3g} !!'.format(units, timeunit)]])
 
             prevtime = curtime
 
             thisunit = dt / iunits
-            timeunit += (thisunit - timeunit) * 0.02 * iunits # Adapt.
+            timeunit += (thisunit - timeunit) * 0.2 * max(0, 1 - 2*error) # Adapt.
 
     def decode_morse(self):
         # Group symbols into letters.
@@ -217,6 +217,15 @@ class Decoder(srd.Decoder):
                 yield None # Pass through flush of 5+ space.
 
     def decode(self):
+
+        # Strictly speaking there is no point in running this decoder
+        # when the sample rate is unknown or zero. But the previous
+        # implementation already fell back to a rate of 1 in that case.
+        # We stick with this approach, to not introduce new constraints
+        # for existing use scenarios.
+        if not self.samplerate:
+            self.samplerate = 1.0
+
         # Annotate letters, group into words.
         s0 = s1 = None
         word = ''