]> sigrok.org Git - libsigrokdecode.git/blob - decoders/guess_bitrate/pd.py
Rename inter-PD output type to SRD_OUTPUT_PYTHON
[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 # Bitrate / baudrate guessing protocol decoder
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 1
27     id = 'guess_bitrate'
28     name = 'Guess bitrate'
29     longname = 'Guess bitrate/baudrate'
30     desc = 'Guess the bitrate/baudrate of a UART (or other) protocol.'
31     license = 'gplv2+'
32     inputs = ['logic']
33     outputs = ['guess_bitrate']
34     probes = [
35         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
36     ]
37     optional_probes = []
38     options = {}
39     annotations = [
40         ['bitrate', 'Bitrate / baudrate'],
41     ]
42
43     def putx(self, data):
44         self.put(self.ss_edge, self.samplenum, self.out_ann, data)
45
46     def __init__(self, **kwargs):
47         self.olddata = None
48         self.ss_edge = None
49         self.first_transition = True
50         self.bitwidth = None
51
52     def start(self, metadata):
53         self.samplerate = metadata['samplerate']
54         # self.out_proto = self.add(srd.OUTPUT_PYTHON, 'guess_bitrate')
55         self.out_ann = self.add(srd.OUTPUT_ANN, 'guess_bitrate')
56
57     def report(self):
58         pass
59
60     def decode(self, ss, es, data):
61         for (self.samplenum, pins) in data:
62
63             data = pins[0]
64
65             # Wait for any transition on the data line.
66             if data == self.olddata:
67                 continue
68
69             # Initialize first self.olddata with the first sample value.
70             if self.olddata == None:
71                 self.olddata = data
72                 continue
73
74             # Get the smallest distance between two transitions
75             # and use that to calculate the bitrate/baudrate.
76             if self.first_transition == True:
77                 self.ss_edge = self.samplenum
78                 self.first_transition = False
79             else:
80                 b = self.samplenum - self.ss_edge
81                 if self.bitwidth == None or b < self.bitwidth:
82                     self.bitwidth = b
83                     bitrate = int(float(self.samplerate) / float(b))
84                     self.putx([0, ['%d' % bitrate]])
85                 self.ss_edge = self.samplenum
86
87             self.olddata = data
88