]> sigrok.org Git - libsigrokdecode.git/blame - decoders/guess_bitrate/pd.py
configure.ac: Also check for Python 3.6.
[libsigrokdecode.git] / decoders / guess_bitrate / pd.py
CommitLineData
56c93143
UH
1##
2## This file is part of the libsigrokdecode project.
3##
3aa79ba3 4## Copyright (C) 2013-2016 Uwe Hermann <uwe@hermann-uwe.de>
56c93143
UH
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
56c93143
UH
21import sigrokdecode as srd
22
21cda951
UH
23class SamplerateError(Exception):
24 pass
25
56c93143 26class Decoder(srd.Decoder):
3aa79ba3 27 api_version = 3
56c93143
UH
28 id = 'guess_bitrate'
29 name = 'Guess bitrate'
30 longname = 'Guess bitrate/baudrate'
31 desc = 'Guess the bitrate/baudrate of a UART (or other) protocol.'
32 license = 'gplv2+'
33 inputs = ['logic']
34 outputs = ['guess_bitrate']
6a15597a 35 channels = (
56c93143 36 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
da9bcbd9
BV
37 )
38 annotations = (
39 ('bitrate', 'Bitrate / baudrate'),
40 )
56c93143
UH
41
42 def putx(self, data):
43 self.put(self.ss_edge, self.samplenum, self.out_ann, data)
44
92b7b49f 45 def __init__(self):
56c93143
UH
46 self.ss_edge = None
47 self.first_transition = True
48 self.bitwidth = None
49
e2ff870e 50 def start(self):
be465111 51 self.out_ann = self.register(srd.OUTPUT_ANN)
56c93143 52
3aa79ba3
UH
53 self.initial_pins = [1] # TODO: Not generally correct.
54
e2ff870e
UH
55 def metadata(self, key, value):
56 if key == srd.SRD_CONF_SAMPLERATE:
35b380b1 57 self.samplerate = value
e2ff870e 58
3aa79ba3 59 def decode(self):
21cda951
UH
60 if not self.samplerate:
61 raise SamplerateError('Cannot decode without samplerate.')
56c93143 62
3aa79ba3
UH
63 while True:
64 # Wait for any transition/edge on the data line.
65 self.wait({0: 'e'})
56c93143
UH
66
67 # Get the smallest distance between two transitions
68 # and use that to calculate the bitrate/baudrate.
35b380b1 69 if self.first_transition:
56c93143
UH
70 self.ss_edge = self.samplenum
71 self.first_transition = False
72 else:
73 b = self.samplenum - self.ss_edge
35b380b1 74 if self.bitwidth is None or b < self.bitwidth:
56c93143
UH
75 self.bitwidth = b
76 bitrate = int(float(self.samplerate) / float(b))
77 self.putx([0, ['%d' % bitrate]])
78 self.ss_edge = self.samplenum