]> sigrok.org Git - libsigrokdecode.git/blame - decoders/dsi/pd.py
dsi: Cosmetics, drop obsolete comments.
[libsigrokdecode.git] / decoders / dsi / pd.py
CommitLineData
23a8a383
JS
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2015 Jeremy Swanson <jeremy@rakocontrols.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 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 SamplerateError(Exception):
23 pass
24
25class Decoder(srd.Decoder):
26 api_version = 2
27 id = 'dsi'
28 name = 'DSI'
92c117d6 29 longname = 'Digital Serial Interface'
23a8a383
JS
30 desc = 'DSI lighting control protocol.'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['dsi']
34 channels = (
35 {'id': 'dsi', 'name': 'DSI', 'desc': 'DSI data line'},
36 )
37 options = (
38 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
39 'values': ('active-low', 'active-high')},
40 )
41 annotations = (
42 ('bit', 'Bit'),
6e333d9e
UH
43 ('startbit', 'Start bit'),
44 ('level', 'Dimmer level'),
23a8a383
JS
45 ('raw', 'Raw data'),
46 )
47 annotation_rows = (
48 ('bits', 'Bits', (0,)),
6e333d9e
UH
49 ('raw', 'Raw data', (3,)),
50 ('fields', 'Fields', (1, 2)),
23a8a383
JS
51 )
52
53 def __init__(self):
54 self.samplerate = None
55 self.samplenum = None
56 self.edges, self.bits, self.ss_es_bits = [], [], []
57 self.state = 'IDLE'
23a8a383
JS
58
59 def start(self):
60 self.out_ann = self.register(srd.OUTPUT_ANN)
41b37321 61 self.old_dsi = 1 if self.options['polarity'] == 'active-low' else 0
23a8a383
JS
62
63 def metadata(self, key, value):
64 if key == srd.SRD_CONF_SAMPLERATE:
65 self.samplerate = value
66 # One bit: 1666.7us (one half low, one half high).
67 # This is how many samples are in 1TE.
68 self.halfbit = int((self.samplerate * 0.0016667) / 2.0)
69
70 def putb(self, bit1, bit2, data):
71 ss, es = self.ss_es_bits[bit1][0], self.ss_es_bits[bit2][1]
72 self.put(ss, es, self.out_ann, data)
73
74 def handle_bits(self, length):
75 a, c, f, g, b = 0, 0, 0, 0, self.bits
76 # Individual raw bits.
77 for i in range(length):
78 if i == 0:
79 ss = max(0, self.bits[0][0])
80 else:
81 ss = self.ss_es_bits[i - 1][1]
82 es = self.bits[i][0] + (self.halfbit * 2)
83 self.ss_es_bits.append([ss, es])
84 self.putb(i, i, [0, ['%d' % self.bits[i][1]]])
85 # Bits[0:0]: Startbit
86 s = ['Startbit: %d' % b[0][1], 'ST: %d' % b[0][1], 'ST', 'S', 'S']
87 self.putb(0, 0, [1, s])
88 self.putb(0, 0, [3, s])
89 # Bits[1:8]
90 for i in range(8):
91 f |= (b[1 + i][1] << (7 - i))
92 g = f / 2.55
93 if length == 9: # BACKWARD Frame
94 s = ['Data: %02X' % f, 'Dat: %02X' % f,
95 'Dat: %02X' % f, 'D: %02X' % f, 'D']
96 self.putb(1, 8, [3, s])
97 s = ['Level: %d%%' % g, 'Lev: %d%%' % g,
98 'Lev: %d%%' % g, 'L: %d' % g, 'D']
99 self.putb(1, 8, [2, s])
100 return
101
102 def reset_decoder_state(self):
103 self.edges, self.bits, self.ss_es_bits = [], [], []
104 self.state = 'IDLE'
105
106 def decode(self, ss, es, data):
107 if not self.samplerate:
108 raise SamplerateError('Cannot decode without samplerate.')
6e333d9e 109 bit = 0
23a8a383 110 for (self.samplenum, pins) in data:
41b37321 111 self.dsi = pins[0]
23a8a383 112 if self.options['polarity'] == 'active-high':
41b37321 113 self.dsi ^= 1 # Invert.
23a8a383
JS
114
115 # State machine.
116 if self.state == 'IDLE':
117 # Wait for any edge (rising or falling).
41b37321 118 if self.old_dsi == self.dsi:
23a8a383
JS
119 continue
120 # Add in the first half of the start bit.
121 self.edges.append(self.samplenum - int(self.halfbit))
122 self.edges.append(self.samplenum)
123 # Start bit is 0->1.
41b37321 124 self.phase0 = self.dsi ^ 1
23a8a383 125 self.state = 'PHASE1'
41b37321 126 self.old_dsi = self.dsi
23a8a383 127 # Get the next sample point.
41b37321 128 self.old_dsi = self.dsi
23a8a383
JS
129 continue
130
41b37321 131 if self.old_dsi != self.dsi:
23a8a383
JS
132 self.edges.append(self.samplenum)
133 elif self.samplenum == (self.edges[-1] + int(self.halfbit * 1.5)):
134 self.edges.append(self.samplenum - int(self.halfbit * 0.5))
135 else:
136 continue
137
41b37321 138 bit = self.old_dsi
23a8a383
JS
139 if self.state == 'PHASE0':
140 self.phase0 = bit
141 self.state = 'PHASE1'
142 elif self.state == 'PHASE1':
6e333d9e 143 if (bit == 1) and (self.phase0 == 1): # Stop bit.
23a8a383 144 if len(self.bits) == 17 or len(self.bits) == 9:
6e333d9e 145 # Forward or Backward.
23a8a383
JS
146 self.handle_bits(len(self.bits))
147 self.reset_decoder_state() # Reset upon errors.
148 continue
149 else:
150 self.bits.append([self.edges[-3], bit])
151 self.state = 'PHASE0'
152
41b37321 153 self.old_dsi = self.dsi