X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fmorse%2Fpd.py;h=f768b3a082da8254b154c8b3214218835a6d121d;hb=HEAD;hp=588394dadc2e5a5f5f266a02b048c257e1660200;hpb=500d5ff479c2ee1c1e5531735485311f4952c368;p=libsigrokdecode.git diff --git a/decoders/morse/pd.py b/decoders/morse/pd.py index 588394d..f768b3a 100644 --- a/decoders/morse/pd.py +++ b/decoders/morse/pd.py @@ -120,7 +120,8 @@ class Decoder(srd.Decoder): desc = 'Demodulated morse code protocol.' license = 'gplv2+' inputs = ['logic'] - outputs = ['morse'] + outputs = [] + tags = ['Encoding'] channels = ( {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, ) @@ -129,14 +130,17 @@ class Decoder(srd.Decoder): ) annotations = ( ('time', 'Time'), - ('units', 'Units'), + ('unit', 'Unit'), ('symbol', 'Symbol'), ('letter', 'Letter'), ('word', 'Word'), ) - annotation_rows = tuple((u, v, (i,)) for i, (u, v) in enumerate(annotations)) + annotation_rows = tuple((u + 's', v + 's', (i,)) for i, (u, v) in enumerate(annotations)) def __init__(self): + self.reset() + + def reset(self): self.samplerate = None def metadata(self, key, value): @@ -151,8 +155,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. @@ -164,7 +166,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) @@ -174,15 +176,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. @@ -214,6 +218,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 = ''