]> sigrok.org Git - libsigrokdecode.git/blob - decoders/guess_bitrate/pd.py
All PDs: Drop unneeded comments.
[libsigrokdecode.git] / decoders / guess_bitrate / pd.py
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
21 import sigrokdecode as srd
22
23 class 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']
32     probes = [
33         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
34     ]
35     optional_probes = []
36     options = {}
37     annotations = [
38         ['bitrate', 'Bitrate / baudrate'],
39     ]
40
41     def putx(self, data):
42         self.put(self.ss_edge, self.samplenum, self.out_ann, data)
43
44     def __init__(self, **kwargs):
45         self.olddata = None
46         self.ss_edge = None
47         self.first_transition = True
48         self.bitwidth = None
49
50     def start(self, metadata):
51         self.samplerate = metadata['samplerate']
52         # self.out_proto = self.register(srd.OUTPUT_PYTHON)
53         self.out_ann = self.register(srd.OUTPUT_ANN)
54
55     def decode(self, ss, es, data):
56         for (self.samplenum, pins) in data:
57
58             data = pins[0]
59
60             # Wait for any transition on the data line.
61             if data == self.olddata:
62                 continue
63
64             # Initialize first self.olddata with the first sample value.
65             if self.olddata == None:
66                 self.olddata = data
67                 continue
68
69             # Get the smallest distance between two transitions
70             # and use that to calculate the bitrate/baudrate.
71             if self.first_transition == True:
72                 self.ss_edge = self.samplenum
73                 self.first_transition = False
74             else:
75                 b = self.samplenum - self.ss_edge
76                 if self.bitwidth == None or b < self.bitwidth:
77                     self.bitwidth = b
78                     bitrate = int(float(self.samplerate) / float(b))
79                     self.putx([0, ['%d' % bitrate]])
80                 self.ss_edge = self.samplenum
81
82             self.olddata = data
83