]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2cfilter/i2cfilter.py
srd: rename extra_probes to optional_probes in all PDs
[libsigrokdecode.git] / decoders / i2cfilter / i2cfilter.py
CommitLineData
61c2bd36
BV
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 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, see <http://www.gnu.org/licenses/>.
18##
19
20import sigrokdecode as srd
21
22
23class Decoder(srd.Decoder):
24 api_version = 1
25 id = 'i2cfilter'
26 name = 'I2C filter'
27 longname = 'I2C filter'
28 desc = 'Filter out specific addresses/directions in an I2C stream.'
29 license = 'gplv3+'
30 inputs = ['i2c']
31 outputs = []
32 options = {
33 'address': ['Address to filter out of the I2C stream', 0],
34 'direction': ['Direction to filter (read/write)', '']
35 }
36
37 def __init__(self, **kwargs):
38 self.state = None
39
40 def start(self, metadata):
41 self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2cdata')
42 if self.options['direction'] not in ('', 'read', 'write'):
43 raise Exception("Invalid direction: expected 'read' or 'write'")
44
45 def decode(self, ss, es, data):
46 try:
47 cmd, data, ack_bit = data
48 except Exception as e:
49 raise Exception('Malformed I2C input: %s' % str(e)) from e
50
51 # Whichever state we're in, these always reset the state machine.
52 # This should make it easier to deal with corrupt data etc.
53 if cmd in ('START', 'START REPEAT'):
54 self.state = 'start'
55 return
56 if cmd == 'STOP':
57 self.state = None
58 return
59
60 if self.state == 'start':
61 # Start of a transfer, see if we want this one.
62 if cmd == 'ADDRESS READ' and self.options['direction'] == 'write':
63 return
64 elif cmd == 'ADDRESS WRITE' and self.options['direction'] == 'read':
65 return
66 elif cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
67 if self.options['address'] in (0, data):
68 # We want this tranfer.
69 self.state = 'transfer'
70 elif self.state == 'transfer':
71 if cmd in ('DATA READ', 'DATA WRITE'):
72 self.put(ss, es, self.out_proto, data)
73 else:
74 raise Exception('Invalid state: %s' % self.state)
75
76