]> sigrok.org Git - libsigrokdecode.git/blob - decoders/timing/pd.py
timing: eliminate magic numbers, remove unused variables
[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, see <http://www.gnu.org/licenses/>.
19 ##
20
21 import sigrokdecode as srd
22 from collections import deque
23
24 class SamplerateError(Exception):
25     pass
26
27 def normalize_time(t):
28     if abs(t) >= 1.0:
29         return '%.3f s  (%.3f Hz)' % (t, (1/t))
30     elif abs(t) >= 0.001:
31         if 1/t/1000 < 1:
32             return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t))
33         else:
34             return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000)
35     elif abs(t) >= 0.000001:
36         if 1/t/1000/1000 < 1:
37             return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000)
38         else:
39             return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000)
40     elif abs(t) >= 0.000000001:
41         if 1/t/1000/1000/1000:
42             return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000)
43         else:
44             return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000)
45     else:
46         return '%f' % t
47
48 class Pin:
49     (DATA,) = range(1)
50
51 class Ann:
52     (TIME, AVG, DELTA,) = range(3)
53
54 class Decoder(srd.Decoder):
55     api_version = 3
56     id = 'timing'
57     name = 'Timing'
58     longname = 'Timing calculation with frequency and averaging'
59     desc = 'Calculate time between edges.'
60     license = 'gplv2+'
61     inputs = ['logic']
62     outputs = []
63     tags = ['Clock/timing', 'Util']
64     channels = (
65         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
66     )
67     annotations = (
68         ('time', 'Time'),
69         ('average', 'Average'),
70         ('delta', 'Delta'),
71     )
72     annotation_rows = (
73         ('times', 'Times', (Ann.TIME,)),
74         ('averages', 'Averages', (Ann.AVG,)),
75         ('deltas', 'Deltas', (Ann.DELTA,)),
76     )
77     options = (
78         { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
79         { 'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling') },
80         { 'id': 'delta', 'desc': 'Show delta from last', 'default': 'no', 'values': ('yes', 'no') },
81     )
82
83     def __init__(self):
84         self.reset()
85
86     def reset(self):
87         self.samplerate = None
88         self.last_samplenum = None
89         self.last_n = deque()
90         self.last_t = None
91
92     def metadata(self, key, value):
93         if key == srd.SRD_CONF_SAMPLERATE:
94             self.samplerate = value
95
96     def start(self):
97         self.out_ann = self.register(srd.OUTPUT_ANN)
98         self.edge = self.options['edge']
99
100     def decode(self):
101         if not self.samplerate:
102             raise SamplerateError('Cannot decode without samplerate.')
103         while True:
104             if self.edge == 'rising':
105                 pin = self.wait({Pin.DATA: 'r'})
106             elif self.edge == 'falling':
107                 pin = self.wait({Pin.DATA: 'f'})
108             else:
109                 pin = self.wait({Pin.DATA: 'e'})
110
111             if not self.last_samplenum:
112                 self.last_samplenum = self.samplenum
113                 continue
114             samples = self.samplenum - self.last_samplenum
115             t = samples / self.samplerate
116
117             if t > 0:
118                 self.last_n.append(t)
119             if len(self.last_n) > self.options['avg_period']:
120                 self.last_n.popleft()
121
122             self.put(self.last_samplenum, self.samplenum, self.out_ann,
123                      [Ann.TIME, [normalize_time(t)]])
124             if self.options['avg_period'] > 0:
125                 self.put(self.last_samplenum, self.samplenum, self.out_ann,
126                          [Ann.AVG, [normalize_time(sum(self.last_n) / len(self.last_n))]])
127             if self.last_t and self.options['delta'] == 'yes':
128                 self.put(self.last_samplenum, self.samplenum, self.out_ann,
129                          [Ann.DELTA, [normalize_time(t - self.last_t)]])
130
131             self.last_t = t
132             self.last_samplenum = self.samplenum