]> sigrok.org Git - libsigrokdecode.git/blame - decoders/timing/pd.py
timing: reduce "state", most action is local to .decode()
[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
4539e9ca 18## along with this program; if not, see <http://www.gnu.org/licenses/>.
92adde51
BE
19##
20
21import sigrokdecode as srd
2cd9b197 22from collections import deque
92adde51
BE
23
24class SamplerateError(Exception):
25 pass
26
27def normalize_time(t):
a9360134 28 if abs(t) >= 1.0:
2cd9b197 29 return '%.3f s (%.3f Hz)' % (t, (1/t))
a9360134 30 elif abs(t) >= 0.001:
2cd9b197
UH
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)
a9360134 35 elif abs(t) >= 0.000001:
2cd9b197
UH
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)
a9360134 40 elif abs(t) >= 0.000000001:
2cd9b197
UH
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)
92adde51
BE
45 else:
46 return '%f' % t
47
6a1b97e6
GS
48class Pin:
49 (DATA,) = range(1)
50
51class Ann:
52 (TIME, AVG, DELTA,) = range(3)
53
92adde51 54class Decoder(srd.Decoder):
831e976a 55 api_version = 3
92adde51
BE
56 id = 'timing'
57 name = 'Timing'
2cd9b197 58 longname = 'Timing calculation with frequency and averaging'
92adde51
BE
59 desc = 'Calculate time between edges.'
60 license = 'gplv2+'
61 inputs = ['logic']
6cbba91f 62 outputs = []
d6d8a8a4 63 tags = ['Clock/timing', 'Util']
92adde51
BE
64 channels = (
65 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
66 )
67 annotations = (
68 ('time', 'Time'),
2cd9b197 69 ('average', 'Average'),
b8c44f69 70 ('delta', 'Delta'),
92adde51
BE
71 )
72 annotation_rows = (
6a1b97e6
GS
73 ('times', 'Times', (Ann.TIME,)),
74 ('averages', 'Averages', (Ann.AVG,)),
75 ('deltas', 'Deltas', (Ann.DELTA,)),
2cd9b197
UH
76 )
77 options = (
78 { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 },
b8c44f69
KP
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') },
92adde51
BE
81 )
82
92b7b49f 83 def __init__(self):
10aeb8ea
GS
84 self.reset()
85
86 def reset(self):
92adde51 87 self.samplerate = None
92adde51
BE
88
89 def metadata(self, key, value):
90 if key == srd.SRD_CONF_SAMPLERATE:
91 self.samplerate = value
92
93 def start(self):
94 self.out_ann = self.register(srd.OUTPUT_ANN)
95
831e976a 96 def decode(self):
92adde51
BE
97 if not self.samplerate:
98 raise SamplerateError('Cannot decode without samplerate.')
3e962c2f
GS
99 edge = self.options['edge']
100 avg_period = self.options['avg_period']
101 last_samplenum = None
102 last_n = deque()
103 last_t = None
831e976a 104 while True:
3e962c2f 105 if edge == 'rising':
6a1b97e6 106 pin = self.wait({Pin.DATA: 'r'})
3e962c2f 107 elif edge == 'falling':
6a1b97e6 108 pin = self.wait({Pin.DATA: 'f'})
b8c44f69 109 else:
6a1b97e6 110 pin = self.wait({Pin.DATA: 'e'})
92adde51 111
3e962c2f
GS
112 if not last_samplenum:
113 last_samplenum = self.samplenum
92adde51 114 continue
3e962c2f 115 samples = self.samplenum - last_samplenum
b8c44f69 116 t = samples / self.samplerate
92adde51 117
b8c44f69 118 if t > 0:
3e962c2f
GS
119 last_n.append(t)
120 if len(last_n) > avg_period:
121 last_n.popleft()
2cd9b197 122
3e962c2f 123 self.put(last_samplenum, self.samplenum, self.out_ann,
6a1b97e6 124 [Ann.TIME, [normalize_time(t)]])
3e962c2f
GS
125 if avg_period > 0:
126 self.put(last_samplenum, self.samplenum, self.out_ann,
127 [Ann.AVG, [normalize_time(sum(last_n) / len(last_n))]])
128 if last_t and self.options['delta'] == 'yes':
129 self.put(last_samplenum, self.samplenum, self.out_ann,
130 [Ann.DELTA, [normalize_time(t - last_t)]])
2cd9b197 131
3e962c2f
GS
132 last_t = t
133 last_samplenum = self.samplenum