X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fcaliper%2Fpd.py;h=20a2a5555d01d735df723d0f0b8ef8c42ab80f92;hb=adb8233a0bf30b1d9ee9176e1caa5dc8ae1830dd;hp=0133b22ee8243d3131fd11688cd43fe0e608b864;hpb=95435ac233c861b51da34862894f2db7a681be3c;p=libsigrokdecode.git diff --git a/decoders/caliper/pd.py b/decoders/caliper/pd.py index 0133b22..20a2a55 100644 --- a/decoders/caliper/pd.py +++ b/decoders/caliper/pd.py @@ -77,28 +77,29 @@ class Decoder(srd.Decoder): self.put(ss, es, self.out_ann, [cls, data]) def decode(self): - last_measurement = None + last_sent = None timeout_ms = self.options['timeout_ms'] want_unit = self.options['unit'] show_all = self.options['changes'] == 'no' - snum_per_ms = self.samplerate / 1000 - timeout_snum = timeout_ms * snum_per_ms + wait_cond = [{0: 'r'}] + if timeout_ms: + snum_per_ms = self.samplerate / 1000 + timeout_snum = timeout_ms * snum_per_ms + wait_cond.append({'skip': round(timeout_snum)}) while True: - clk, data = self.wait([{0: 'r'}, {'skip': round(snum_per_ms)}]) - - # Timeout after inactivity. - if timeout_ms > 0: - if self.samplenum > self.es + timeout_snum: - if self.number_bits or self.flags_bits: - count = len(self.number_bits) + len(self.flags_bits) - self.putg(self.ss, self.samplenum, 1, [ - 'timeout with %s bits in buffer' % (count), - 'timeout', - ]) - self.reset() - - # Do nothing if there was timeout without rising clock edge. - if self.matched == (False, True): + # Sample data at the rising clock edge. Optionally timeout + # after inactivity for a user specified period. Present the + # number of unprocessed bits to the user for diagnostics. + clk, data = self.wait(wait_cond) + if timeout_ms and not self.matched[0]: + if self.number_bits or self.flags_bits: + count = len(self.number_bits) + len(self.flags_bits) + self.putg(self.ss, self.samplenum, 1, [ + 'timeout with {} bits in buffer'.format(count), + 'timeout ({} bits)'.format(count), + 'timeout', + ]) + self.reset() continue # Store position of first bit and last activity. @@ -122,26 +123,24 @@ class Decoder(srd.Decoder): if negative: number = -number if is_inch: - number = number / 2000 + number /= 2000 if want_unit == 'mm': number *= mm_per_inch is_inch = False else: - number = number / 100 + number /= 100 if want_unit == 'inch': number = round(number / mm_per_inch, 4) is_inch = True + unit = 'in' if is_inch else 'mm' - units = "in" if is_inch else "mm" - - measurement = (str(number) + units) - - if show_all or measurement != last_measurement: + # Construct and emit an annotation. + if show_all or (number, unit) != last_sent: self.putg(self.ss, self.es, 0, [ - measurement, - str(number), + '{number}{unit}'.format(**locals()), + '{number}'.format(**locals()), ]) - last_measurement = measurement + last_sent = (number, unit) - # Prepare for next packet. + # Reset internal state for the start of the next packet. self.reset()