]> sigrok.org Git - libsigrokdecode.git/blame - decoders/guess_bitrate/pd.py
All PDs: Drop some unneeded comments.
[libsigrokdecode.git] / decoders / guess_bitrate / pd.py
CommitLineData
56c93143
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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
23class Decoder(srd.Decoder):
24 api_version = 1
25 id = 'guess_bitrate'
26 name = 'Guess bitrate'
27 longname = 'Guess bitrate/baudrate'
28 desc = 'Guess the bitrate/baudrate of a UART (or other) protocol.'
29 license = 'gplv2+'
30 inputs = ['logic']
31 outputs = ['guess_bitrate']
6a15597a 32 channels = (
56c93143 33 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
da9bcbd9
BV
34 )
35 annotations = (
36 ('bitrate', 'Bitrate / baudrate'),
37 )
56c93143
UH
38
39 def putx(self, data):
40 self.put(self.ss_edge, self.samplenum, self.out_ann, data)
41
42 def __init__(self, **kwargs):
43 self.olddata = None
44 self.ss_edge = None
45 self.first_transition = True
46 self.bitwidth = None
47
e2ff870e 48 def start(self):
be465111 49 self.out_ann = self.register(srd.OUTPUT_ANN)
56c93143 50
e2ff870e
UH
51 def metadata(self, key, value):
52 if key == srd.SRD_CONF_SAMPLERATE:
53 self.samplerate = value;
54
56c93143 55 def decode(self, ss, es, data):
e2ff870e
UH
56 if self.samplerate is None:
57 raise Exception("Cannot decode without samplerate.")
56c93143
UH
58 for (self.samplenum, pins) in data:
59
60 data = pins[0]
61
62 # Wait for any transition on the data line.
63 if data == self.olddata:
64 continue
65
66 # Initialize first self.olddata with the first sample value.
67 if self.olddata == None:
68 self.olddata = data
69 continue
70
71 # Get the smallest distance between two transitions
72 # and use that to calculate the bitrate/baudrate.
73 if self.first_transition == True:
74 self.ss_edge = self.samplenum
75 self.first_transition = False
76 else:
77 b = self.samplenum - self.ss_edge
78 if self.bitwidth == None or b < self.bitwidth:
79 self.bitwidth = b
80 bitrate = int(float(self.samplerate) / float(b))
81 self.putx([0, ['%d' % bitrate]])
82 self.ss_edge = self.samplenum
83
84 self.olddata = data
85