2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
5 ## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
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.
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.
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
22 import sigrokdecode as srd
24 class SamplerateError(Exception):
27 def normalize_time(t):
31 return '%.3f ms' % (t * 1000.0)
33 return '%.3f μs' % (t * 1000.0 * 1000.0)
34 elif t >= 0.000000001:
35 return '%.3f ns' % (t * 1000.0 * 1000.0 * 1000.0)
39 class Decoder(srd.Decoder):
43 longname = 'Timing calculation'
44 desc = 'Calculate time between edges.'
49 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
55 ('time', 'Time', (0,)),
58 def __init__(self, **kwargs):
59 self.samplerate = None
61 self.last_samplenum = None
63 def metadata(self, key, value):
64 if key == srd.SRD_CONF_SAMPLERATE:
65 self.samplerate = value
68 self.out_ann = self.register(srd.OUTPUT_ANN)
70 def decode(self, ss, es, data):
71 if not self.samplerate:
72 raise SamplerateError('Cannot decode without samplerate.')
74 for (samplenum, (pin,)) in data:
75 # Ignore identical samples early on (for performance reasons).
76 if self.oldpin == pin:
79 if self.oldpin is None:
81 self.last_samplenum = samplenum
84 if self.oldpin != pin:
85 samples = samplenum - self.last_samplenum
86 t = samples / self.samplerate
88 # Report the timing normalized.
89 self.put(self.last_samplenum, samplenum, self.out_ann,
90 [0, [normalize_time(t)]])
92 # Store data for next round.
93 self.last_samplenum = samplenum