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