]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2cdemux/pd.py
The start() method no longer takes a metadata parameter
[libsigrokdecode.git] / decoders / i2cdemux / pd.py
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 # Generic I2C demultiplexing protocol decoder
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 1
27     id = 'i2cdemux'
28     name = 'I2C demux'
29     longname = 'I2C demultiplexer'
30     desc = 'Demux I2C packets into per-slave-address streams.'
31     license = 'gplv2+'
32     inputs = ['i2c']
33     outputs = [] # TODO: Only known at run-time.
34     probes = []
35     optional_probes = []
36     options = {}
37     annotations = []
38
39     def __init__(self, **kwargs):
40         self.packets = [] # Local cache of I2C packets
41         self.slaves = [] # List of known slave addresses
42         self.stream = -1 # Current output stream
43         self.streamcount = 0 # Number of created output streams
44
45     def start(self):
46         self.out_proto = []
47
48     def report(self):
49         pass
50
51     # Grab I2C packets into a local cache, until an I2C STOP condition
52     # packet comes along. At some point before that STOP condition, there
53     # will have been an ADDRESS READ or ADDRESS WRITE which contains the
54     # I2C address of the slave that the master wants to talk to.
55     # We use this slave address to figure out which output stream should
56     # get the whole chunk of packets (from START to STOP).
57     def decode(self, ss, es, data):
58
59         cmd, databyte = data
60
61         # Add the I2C packet to our local cache.
62         self.packets.append([ss, es, data])
63
64         if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
65             if databyte in self.slaves:
66                 self.stream = self.slaves.index(databyte)
67                 return
68
69             # We're never seen this slave, add a new stream.
70             self.slaves.append(databyte)
71             self.out_proto.append(self.add(srd.OUTPUT_PROTO,
72                                   'i2c-%s' % hex(databyte)))
73             self.stream = self.streamcount
74             self.streamcount += 1
75         elif cmd == 'STOP':
76             if self.stream == -1:
77                 raise Exception('Invalid stream!') # FIXME?
78
79             # Send the whole chunk of I2C packets to the correct stream.
80             for p in self.packets:
81                 self.put(p[0], p[1], self.out_proto[self.stream], p[2])
82
83             self.packets = []
84             self.stream = -1
85         else:
86             pass # Do nothing, only add the I2C packet to our cache.
87