]> sigrok.org Git - libsigrokdecode.git/blob - decoders/timing/pd.py
5e2aaa7033bc4f42a3a37364e0e4fee01a901028
[libsigrokdecode.git] / decoders / timing / pd.py
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
22 import sigrokdecode as srd
23
24 class SamplerateError(Exception):
25     pass
26
27 def normalize_time(t):
28     if t >= 1.0:
29         return '%.3f s' % t
30     elif t >= 0.001:
31         return '%.3f ms' % (t * 1000.0)
32     elif t >= 0.000001:
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)
36     else:
37         return '%f' % t
38
39 class 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
74         for (self.samplenum, (pin,)) in data:
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
81                 self.last_samplenum = self.samplenum
82                 continue
83
84             if self.oldpin != pin:
85                 samples = self.samplenum - self.last_samplenum
86                 t = samples / self.samplerate
87
88                 # Report the timing normalized.
89                 self.put(self.last_samplenum, self.samplenum, self.out_ann,
90                          [0, [normalize_time(t)]])
91
92                 # Store data for next round.
93                 self.last_samplenum = self.samplenum
94                 self.oldpin = pin