]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/i2cdemux/pd.py
Mark all stacked decoders as being PD API version 3.
[libsigrokdecode.git] / decoders / i2cdemux / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode 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, see <http://www.gnu.org/licenses/>.
18##
19
20import sigrokdecode as srd
21
22class Decoder(srd.Decoder):
23 api_version = 3
24 id = 'i2cdemux'
25 name = 'I²C demux'
26 longname = 'I²C demultiplexer'
27 desc = 'Demux I²C packets into per-slave-address streams.'
28 license = 'gplv2+'
29 inputs = ['i2c']
30 outputs = [] # TODO: Only known at run-time.
31
32 def __init__(self):
33 self.packets = [] # Local cache of I²C packets
34 self.slaves = [] # List of known slave addresses
35 self.stream = -1 # Current output stream
36 self.streamcount = 0 # Number of created output streams
37
38 def start(self):
39 self.out_python = []
40
41 # Grab I²C packets into a local cache, until an I²C STOP condition
42 # packet comes along. At some point before that STOP condition, there
43 # will have been an ADDRESS READ or ADDRESS WRITE which contains the
44 # I²C address of the slave that the master wants to talk to.
45 # We use this slave address to figure out which output stream should
46 # get the whole chunk of packets (from START to STOP).
47 def decode(self, ss, es, data):
48
49 cmd, databyte = data
50
51 # Add the I²C packet to our local cache.
52 self.packets.append([ss, es, data])
53
54 if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
55 if databyte in self.slaves:
56 self.stream = self.slaves.index(databyte)
57 return
58
59 # We're never seen this slave, add a new stream.
60 self.slaves.append(databyte)
61 self.out_python.append(self.register(srd.OUTPUT_PYTHON,
62 proto_id='i2c-%s' % hex(databyte)))
63 self.stream = self.streamcount
64 self.streamcount += 1
65 elif cmd == 'STOP':
66 if self.stream == -1:
67 raise Exception('Invalid stream!') # FIXME?
68
69 # Send the whole chunk of I²C packets to the correct stream.
70 for p in self.packets:
71 self.put(p[0], p[1], self.out_python[self.stream], p[2])
72
73 self.packets = []
74 self.stream = -1
75 else:
76 pass # Do nothing, only add the I²C packet to our cache.