]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
2cd9b197 | 23 | from collections import deque |
92adde51 BE |
24 | |
25 | class SamplerateError(Exception): | |
26 | pass | |
27 | ||
28 | def normalize_time(t): | |
29 | if t >= 1.0: | |
2cd9b197 | 30 | return '%.3f s (%.3f Hz)' % (t, (1/t)) |
92adde51 | 31 | elif t >= 0.001: |
2cd9b197 UH |
32 | if 1/t/1000 < 1: |
33 | return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t)) | |
34 | else: | |
35 | return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000) | |
92adde51 | 36 | elif t >= 0.000001: |
2cd9b197 UH |
37 | if 1/t/1000/1000 < 1: |
38 | return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000) | |
39 | else: | |
40 | return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000) | |
92adde51 | 41 | elif t >= 0.000000001: |
2cd9b197 UH |
42 | if 1/t/1000/1000/1000: |
43 | return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000) | |
44 | else: | |
45 | return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000) | |
92adde51 BE |
46 | else: |
47 | return '%f' % t | |
48 | ||
49 | class Decoder(srd.Decoder): | |
50 | api_version = 2 | |
51 | id = 'timing' | |
52 | name = 'Timing' | |
2cd9b197 | 53 | longname = 'Timing calculation with frequency and averaging' |
92adde51 BE |
54 | desc = 'Calculate time between edges.' |
55 | license = 'gplv2+' | |
56 | inputs = ['logic'] | |
57 | outputs = ['timing'] | |
58 | channels = ( | |
59 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, | |
60 | ) | |
61 | annotations = ( | |
62 | ('time', 'Time'), | |
2cd9b197 | 63 | ('average', 'Average'), |
92adde51 BE |
64 | ) |
65 | annotation_rows = ( | |
66 | ('time', 'Time', (0,)), | |
2cd9b197 UH |
67 | ('average', 'Average', (1,)), |
68 | ) | |
69 | options = ( | |
70 | { 'id': 'avg_period', 'desc': 'Averaging period', 'default': 100 }, | |
92adde51 BE |
71 | ) |
72 | ||
92b7b49f | 73 | def __init__(self): |
92adde51 BE |
74 | self.samplerate = None |
75 | self.oldpin = None | |
76 | self.last_samplenum = None | |
2cd9b197 UH |
77 | self.last_n = deque() |
78 | self.chunks = 0 | |
92adde51 BE |
79 | |
80 | def metadata(self, key, value): | |
81 | if key == srd.SRD_CONF_SAMPLERATE: | |
82 | self.samplerate = value | |
83 | ||
84 | def start(self): | |
85 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
86 | ||
87 | def decode(self, ss, es, data): | |
88 | if not self.samplerate: | |
89 | raise SamplerateError('Cannot decode without samplerate.') | |
90 | ||
963d71d5 | 91 | for (self.samplenum, (pin,)) in data: |
92adde51 BE |
92 | if self.oldpin is None: |
93 | self.oldpin = pin | |
963d71d5 | 94 | self.last_samplenum = self.samplenum |
92adde51 BE |
95 | continue |
96 | ||
97 | if self.oldpin != pin: | |
963d71d5 | 98 | samples = self.samplenum - self.last_samplenum |
92adde51 | 99 | t = samples / self.samplerate |
2cd9b197 UH |
100 | self.chunks += 1 |
101 | ||
102 | # Don't insert the first chunk into the averaging as it is | |
103 | # not complete probably. | |
104 | if self.last_samplenum is None or self.chunks < 2: | |
105 | # Report the timing normalized. | |
106 | self.put(self.last_samplenum, self.samplenum, self.out_ann, | |
107 | [0, [normalize_time(t)]]) | |
108 | else: | |
109 | if t > 0: | |
110 | self.last_n.append(t) | |
111 | ||
112 | if len(self.last_n) > self.options['avg_period']: | |
113 | self.last_n.popleft() | |
92adde51 | 114 | |
2cd9b197 UH |
115 | # Report the timing normalized. |
116 | self.put(self.last_samplenum, self.samplenum, self.out_ann, | |
117 | [0, [normalize_time(t)]]) | |
118 | self.put(self.last_samplenum, self.samplenum, self.out_ann, | |
119 | [1, [normalize_time(sum(self.last_n) / len(self.last_n))]]) | |
92adde51 BE |
120 | |
121 | # Store data for next round. | |
963d71d5 | 122 | self.last_samplenum = self.samplenum |
92adde51 | 123 | self.oldpin = pin |