X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Ftiming%2Fpd.py;h=19836b0b3014cbfc00edbbdef928fc082f5778f2;hp=5cb162f99a8ab597a9ac9620a5546060fa0da5b4;hb=4539e9ca58966ce3c9cad4801b16c315e86ace01;hpb=92adde514e1be3015ebd44da813fb6bec0001370 diff --git a/decoders/timing/pd.py b/decoders/timing/pd.py index 5cb162f..19836b0 100644 --- a/decoders/timing/pd.py +++ b/decoders/timing/pd.py @@ -15,32 +15,41 @@ ## 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 + return '%.3f s (%.3f Hz)' % (t, (1/t)) elif t >= 0.001: - return '%.3f msec' % (t * 1000.0) + 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 t >= 0.000001: - return '%.3f usec' % (t * 1000.0 * 1000.0) + 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 t >= 0.000000001: - return '%.3f nsec' % (t * 1000.0 * 1000.0 * 1000.0) + 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 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'] @@ -50,15 +59,22 @@ class Decoder(srd.Decoder): ) annotations = ( ('time', 'Time'), + ('average', 'Average'), ) annotation_rows = ( ('time', 'Time', (0,)), + ('average', 'Average', (1,)), + ) + options = ( + { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 }, ) - def __init__(self, **kwargs): + def __init__(self): self.samplerate = None self.oldpin = None self.last_samplenum = None + self.last_n = deque() + self.chunks = 0 def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: @@ -66,29 +82,43 @@ class Decoder(srd.Decoder): def start(self): self.out_ann = self.register(srd.OUTPUT_ANN) + self.initial_pins = [0] - def decode(self, ss, es, data): + def decode(self): if not self.samplerate: raise SamplerateError('Cannot decode without samplerate.') - - for (samplenum, (pin,)) in data: - # Ignore identical samples early on (for performance reasons). - if self.oldpin == pin: - continue + while True: + pin = self.wait({0: 'e'}) if self.oldpin is None: self.oldpin = pin - self.last_samplenum = samplenum + self.last_samplenum = self.samplenum continue if self.oldpin != pin: - samples = samplenum - self.last_samplenum + samples = self.samplenum - self.last_samplenum t = samples / self.samplerate + self.chunks += 1 + + # Don't insert the first chunk into the averaging as it is + # not complete probably. + if self.last_samplenum is None or self.chunks < 2: + # Report the timing normalized. + self.put(self.last_samplenum, self.samplenum, self.out_ann, + [0, [normalize_time(t)]]) + else: + if t > 0: + self.last_n.append(t) + + if len(self.last_n) > self.options['avg_period']: + self.last_n.popleft() - # Report the timing normalized. - self.put(self.last_samplenum, samplenum, self.out_ann, - [0, [normalize_time(t)]]) + # Report the timing normalized. + self.put(self.last_samplenum, self.samplenum, self.out_ann, + [0, [normalize_time(t)]]) + self.put(self.last_samplenum, self.samplenum, self.out_ann, + [1, [normalize_time(sum(self.last_n) / len(self.last_n))]]) # Store data for next round. - self.last_samplenum = samplenum + self.last_samplenum = self.samplenum self.oldpin = pin