]> sigrok.org Git - libsigrokdecode.git/blob - decoders/uart_dump/pd.py
All PDs: Name the files pd.py consistently.
[libsigrokdecode.git] / decoders / uart_dump / pd.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 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 import os
23 import sys
24
25 RX = 0
26 TX = 1
27
28 class Decoder(srd.Decoder):
29     api_version = 1
30     id = 'uart_dump'
31     name = 'UART dump'
32     longname = 'UART dump'
33     desc = 'Output decoded UART data to a file.'
34     license = 'gplv2+'
35     inputs = ['uart']
36     outputs = [] # TODO?
37     probes = []
38     optional_probes = []
39     options = {
40         'rx': ['Output RX data?', 'yes'],
41         'tx': ['Output TX data?', 'yes'],
42         'filename': ['File name for RX and TX data', '-'],
43         'filename_rx': ['File name for RX data', 'none'],
44         'filename_tx': ['File name for TX data', 'none'],
45     }
46     annotations = []
47
48     def __init__(self, **kwargs):
49         self.f = None
50         self.f_rx = None
51         self.f_tx = None
52
53     def file_open(self, filename):
54         if filename == 'none':
55             return None
56         elif filename == '-':
57             return sys.stdout
58         else:
59             return open(filename, 'w')
60
61     def start(self, metadata):
62         # The user can specify 'filename' (gets both RX and TX data), and/or
63         # 'filename_rx' (for RX data only), and/or 'filename_tx', respectively.
64
65         # Default is to output RX and TX to 'filename', with 'filename_rx' and
66         # 'filename_tx' being unused.
67
68         # If multiple 'filename*' options are specified, the user must NOT
69         # use the same file for any of them.
70
71         # A filename of 'none' is not allowed (has special meaning). A filename
72         # of '-' means 'stdout'.
73
74         self.f = self.file_open(self.options['filename'])
75         self.f_rx = self.file_open(self.options['filename_rx'])
76         self.f_tx = self.file_open(self.options['filename_tx'])
77
78     def report(self):
79         pass
80
81     def decode(self, ss, es, data):
82         ptype, rxtx, pdata = data
83
84         # Ignore all UART packets except the actual data packets (i.e., we
85         # do not print start bits, parity bits, stop bits, errors, and so on).
86         if ptype != 'DATA':
87             return
88
89         # TODO: Configurable format.
90         c = chr(pdata)
91
92         # TODO: Error handling.
93
94         # Output RX and/or TX to 'filename'.
95         if self.f != None:
96             self.f.write(c)
97             self.f.flush()
98
99         # Output RX data to 'filename_rx'.
100         if self.f_rx != None:
101             if self.options['rx'] == 'yes' and rxtx == RX:
102                 self.f_rx.write(c)
103                 self.f_rx.flush()
104
105         # Output TX data to 'filename_tx'.
106         if self.f_tx != None:
107             if self.options['tx'] == 'yes' and rxtx == TX:
108                 self.f_tx.write(c)
109                 self.f_tx.flush()
110