]> sigrok.org Git - libsigrokdecode.git/blame - decoders/timing/pd.py
timing: Use self.samplenum for consistency across PDs.
[libsigrokdecode.git] / decoders / timing / pd.py
CommitLineData
92adde51
BE
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
5## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
6##
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, write to the Free Software
19## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20##
21
22import sigrokdecode as srd
23
24class SamplerateError(Exception):
25 pass
26
27def normalize_time(t):
28 if t >= 1.0:
468c974e 29 return '%.3f s' % t
92adde51 30 elif t >= 0.001:
468c974e 31 return '%.3f ms' % (t * 1000.0)
92adde51 32 elif t >= 0.000001:
468c974e 33 return '%.3f μs' % (t * 1000.0 * 1000.0)
92adde51 34 elif t >= 0.000000001:
468c974e 35 return '%.3f ns' % (t * 1000.0 * 1000.0 * 1000.0)
92adde51
BE
36 else:
37 return '%f' % t
38
39class Decoder(srd.Decoder):
40 api_version = 2
41 id = 'timing'
42 name = 'Timing'
43 longname = 'Timing calculation'
44 desc = 'Calculate time between edges.'
45 license = 'gplv2+'
46 inputs = ['logic']
47 outputs = ['timing']
48 channels = (
49 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
50 )
51 annotations = (
52 ('time', 'Time'),
53 )
54 annotation_rows = (
55 ('time', 'Time', (0,)),
56 )
57
58 def __init__(self, **kwargs):
59 self.samplerate = None
60 self.oldpin = None
61 self.last_samplenum = None
62
63 def metadata(self, key, value):
64 if key == srd.SRD_CONF_SAMPLERATE:
65 self.samplerate = value
66
67 def start(self):
68 self.out_ann = self.register(srd.OUTPUT_ANN)
69
70 def decode(self, ss, es, data):
71 if not self.samplerate:
72 raise SamplerateError('Cannot decode without samplerate.')
73
963d71d5 74 for (self.samplenum, (pin,)) in data:
92adde51
BE
75 # Ignore identical samples early on (for performance reasons).
76 if self.oldpin == pin:
77 continue
78
79 if self.oldpin is None:
80 self.oldpin = pin
963d71d5 81 self.last_samplenum = self.samplenum
92adde51
BE
82 continue
83
84 if self.oldpin != pin:
963d71d5 85 samples = self.samplenum - self.last_samplenum
92adde51
BE
86 t = samples / self.samplerate
87
88 # Report the timing normalized.
963d71d5 89 self.put(self.last_samplenum, self.samplenum, self.out_ann,
92adde51
BE
90 [0, [normalize_time(t)]])
91
92 # Store data for next round.
963d71d5 93 self.last_samplenum = self.samplenum
92adde51 94 self.oldpin = pin