]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ddc/ddc.py
f59be8e26190649ab0c6b158dff252fadd5b9752
[libsigrokdecode.git] / decoders / ddc / ddc.py
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, If not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 '''
21 This decoder extracts a DDC stream from an I2C session between a computer
22 and a display device. The stream is output as plain bytes.
23
24 Details:
25 https://en.wikipedia.org/wiki/Display_Data_Channel
26 '''
27
28 import sigrokdecode as srd
29
30 class Decoder(srd.Decoder):
31     api_version = 1
32     id = 'ddc'
33     name = 'DDC'
34     longname = 'Display Data Channel'
35     desc = 'A protocol for communication between computers and displays.'
36     longdesc = ''
37     license = 'gplv3+'
38     inputs = ['i2c']
39     outputs = ['ddc']
40     probes = []
41     extra_probes = []
42     options = {}
43     annotations = [
44         ['Byte stream', 'DDC byte stream as read from display.'],
45     ]
46
47     def __init__(self, **kwargs):
48         self.state = None
49
50     def start(self, metadata):
51         self.out_ann = self.add(srd.OUTPUT_ANN, 'ddc')
52
53     def report(self):
54         pass
55
56     def decode(self, ss, es, data):
57         try:
58             cmd, data, ack_bit = data
59         except Exception as e:
60             raise Exception('Malformed I2C input: %s' % str(e)) from e
61
62         if self.state is None:
63             # Wait for the DDC session to start.
64             if cmd in ('START', 'START REPEAT'):
65                 self.state = 'start'
66         elif self.state == 'start':
67             if cmd == 'ADDRESS READ' and data == 80:
68                 # 80 is the I2C slave address of a connected display,
69                 # so this marks the start of the DDC data transfer.
70                 self.state = 'transfer'
71             elif cmd == 'STOP':
72                 # Got back to the idle state.
73                 self.state = None
74         elif self.state == 'transfer':
75             if cmd == 'DATA READ':
76                 # There shouldn't be anything but data reads on this
77                 # address, so ignore everything else.
78                 self.put(ss, es, self.out_ann, [0, ['0x%.2x' % data]])
79         else:
80             raise Exception('Invalid state: %s' % self.state)
81