]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/timing/pd.py
timing: add edge-edge mode and delta calculation
[libsigrokdecode.git] / decoders / timing / pd.py
... / ...
CommitLineData
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
21import sigrokdecode as srd
22from collections import deque
23
24class SamplerateError(Exception):
25 pass
26
27def normalize_time(t):
28 if t >= 1.0:
29 return '%.3f s (%.3f Hz)' % (t, (1/t))
30 elif 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 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 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
48class Decoder(srd.Decoder):
49 api_version = 3
50 id = 'timing'
51 name = 'Timing'
52 longname = 'Timing calculation with frequency and averaging'
53 desc = 'Calculate time between edges.'
54 license = 'gplv2+'
55 inputs = ['logic']
56 outputs = ['timing']
57 channels = (
58 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
59 )
60 annotations = (
61 ('time', 'Time'),
62 ('average', 'Average'),
63 ('delta', 'Delta'),
64 )
65 annotation_rows = (
66 ('time', 'Time', (0,)),
67 ('average', 'Average', (1,)),
68 ('delta', 'Delta', (2,)),
69 )
70 options = (
71 { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
72 { 'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling') },
73 { 'id': 'delta', 'desc': 'Show delta from last', 'default': 'no', 'values': ('yes', 'no') },
74 )
75
76 def __init__(self):
77 self.samplerate = None
78 self.oldpin = None
79 self.last_samplenum = None
80 self.last_n = deque()
81 self.chunks = 0
82 self.level_changed = False
83 self.last_t = None
84
85 def metadata(self, key, value):
86 if key == srd.SRD_CONF_SAMPLERATE:
87 self.samplerate = value
88
89 def start(self):
90 self.out_ann = self.register(srd.OUTPUT_ANN)
91 self.edge = self.options['edge']
92 self.initial_pins = [0]
93
94 def decode(self):
95 if not self.samplerate:
96 raise SamplerateError('Cannot decode without samplerate.')
97 while True:
98 if self.edge == 'rising':
99 pin = self.wait({0: 'r'})
100 elif self.edge == 'falling':
101 pin = self.wait({0: 'f'})
102 else:
103 pin = self.wait({0: 'e'})
104
105 if not self.last_samplenum:
106 self.last_samplenum = self.samplenum
107 continue
108 samples = self.samplenum - self.last_samplenum
109 t = samples / self.samplerate
110
111 if t > 0:
112 self.last_n.append(t)
113 if len(self.last_n) > self.options['avg_period']:
114 self.last_n.popleft()
115
116 self.put(self.last_samplenum, self.samplenum, self.out_ann,
117 [0, [normalize_time(t)]])
118 if self.options['avg_period'] > 0:
119 self.put(self.last_samplenum, self.samplenum, self.out_ann,
120 [1, [normalize_time(sum(self.last_n) / len(self.last_n))]])
121 if self.last_t and self.options['delta'] == 'yes':
122 self.put(self.last_samplenum, self.samplenum, self.out_ann,
123 [2, [normalize_time(t - self.last_t)]])
124
125 self.last_t = t
126 self.last_samplenum = self.samplenum