2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2018 Steve R <steversig@virginmedia.com>
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.
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.
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/>.
20 import sigrokdecode as srd
22 bitvals = ('0', '1', 'f', 'U')
24 def decode_bit(edges, pulses_per_bit):
25 if pulses_per_bit == 2:
26 # Datasheet says long pulse is 3 times short pulse.
27 lmin = 1.5 # long min multiplier
28 lmax = 5 # long max multiplier
29 if (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax): # 0 -___
31 elif (edges[0] >= edges[1] * lmin and edges[0] <= edges[1] * lmax): # 1 ---_
33 # No float type for this line encoding
37 if pulses_per_bit == 4:
38 # Datasheet says long pulse is 3 times short pulse.
39 lmin = 2 # long min multiplier
40 lmax = 5 # long max multiplier
41 eqmin = 0.5 # equal min multiplier
42 eqmax = 1.5 # equal max multiplier
44 (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax) and
45 (edges[2] >= edges[0] * eqmin and edges[2] <= edges[0] * eqmax) and
46 (edges[3] >= edges[0] * lmin and edges[3] <= edges[0] * lmax)):
49 (edges[0] >= edges[1] * lmin and edges[0] <= edges[1] * lmax) and
50 (edges[0] >= edges[2] * eqmin and edges[0] <= edges[2] * eqmax) and
51 (edges[0] >= edges[3] * lmin and edges[0] <= edges[3] * lmax)):
53 elif ( # float ---_-___
54 (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax) and
55 (edges[2] >= edges[0] * lmin and edges[2] <= edges[0]* lmax) and
56 (edges[3] >= edges[0] * eqmin and edges[3] <= edges[0] * eqmax)):
61 def pinlabels(bit_count, packet_bit_count):
62 if packet_bit_count == 12:
64 return 'A%i' % (bit_count - 1)
66 return 'A%i/D%i' % (bit_count - 1, 12 - bit_count)
68 if packet_bit_count == 24:
70 return 'A%i' % (bit_count - 1)
72 return 'D%i' % (bit_count - 21)
74 def decode_model(model, bits):
75 if model == 'maplin_l95ar':
76 address = 'Addr' # Address bits A0 to A5
78 address += ' %i:' % (i + 1) + ('on' if bits[i][0] == '0' else 'off')
80 # Button bits A6/D5 to A11/D0
81 if bits[6][0] == '0' and bits[11][0] == '0':
83 elif bits[7][0] == '0' and bits[11][0] == '0':
85 elif bits[9][0] == '0' and bits[11][0] == '0':
87 elif bits[8][0] == '0' and bits[11][0] == '0':
91 return [address, bits[0][1], bits[5][2], \
92 button, bits[6][1], bits[11][2]]
97 for i in range(0, 20):
99 addr += int(bits[i][0]) * 2 ** i
104 address = 'Address 0x%X %X %X' % (addr & 0xFF, (addr >> 8) & 0xFF, addr >> 16)
106 address = 'Invalid address as not all bits are 0 or 1'
108 output = ' K0 = ' + bits[20][0] + ','
109 output += ' K1 = ' + bits[21][0] + ','
110 output += ' K2 = ' + bits[22][0] + ','
111 output += ' K3 = ' + bits[23][0]
112 return [address, bits[0][1], bits[19][2], \
113 output, bits[20][1], bits[23][2]]
115 class Decoder(srd.Decoder):
119 longname = 'Remote control encoder'
120 desc = 'PT22x2/HX22x2/SC52x2 and xx1527 remote control encoder protocol.'
126 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
133 ('bit-sync', 'Bit sync'),
135 ('code-word-addr', 'Code word address'),
136 ('code-word-data', 'Code word data'),
139 ('bits', 'Bits', (0, 1, 2, 3, 4)),
140 ('pins', 'Pins', (5,)),
141 ('code-words', 'Code words', (6, 7)),
144 {'id': 'linecoding', 'desc': 'Encoding', 'default': 'SC52x2/HX22x2', 'values': ('SC52x2/HX22x2', 'xx1527')},
145 {'id': 'remote', 'desc': 'Remote', 'default': 'none', 'values': ('none', 'maplin_l95ar')},
152 self.samplenumber_last = None
162 self.out_ann = self.register(srd.OUTPUT_ANN)
163 self.model = self.options['remote']
164 if self.options['linecoding'] == 'xx1527':
165 self.pulses_per_bit = 2
166 self.packet_bits = 24
167 self.model = 'xx1527'
169 self.pulses_per_bit = 4 # Each bit is repeated
170 self.packet_bits = 12
172 def putx(self, data):
173 self.put(self.ss, self.es, self.out_ann, data)
177 pin = self.wait({0: 'e'})
178 self.state = 'DECODING'
180 if not self.samplenumber_last: # Set counters to start of signal.
181 self.samplenumber_last = self.samplenum
182 self.ss = self.samplenum
185 if self.bit_count < self.packet_bits: # Decode A0 to A11 / A23.
187 for i in range(0, self.pulses_per_bit):
189 pin = self.wait({0: 'e'}) # Get next edges if we need more.
190 samples = self.samplenum - self.samplenumber_last
191 self.pulses.append(samples) # Save the pulse width.
192 self.samplenumber_last = self.samplenum
193 self.es = self.samplenum
194 self.bits.append([decode_bit(self.pulses, self.pulses_per_bit), self.ss,
195 self.es]) # Save states and times.
196 idx = bitvals.index(decode_bit(self.pulses, self.pulses_per_bit))
197 self.putx([idx, [decode_bit(self.pulses, self.pulses_per_bit)]]) # Write decoded bit.
198 self.putx([5, [pinlabels(self.bit_count, self.packet_bits)]]) # Write pin labels.
200 self.ss = self.samplenum
202 if self.model != 'none':
203 self.labels = decode_model(self.model, self.bits)
204 self.put(self.labels[1], self.labels[2], self.out_ann,
205 [6, [self.labels[0]]]) # Write model decode.
206 self.put(self.labels[4], self.labels[5], self.out_ann,
207 [7, [self.labels[3]]]) # Write model decode.
208 samples = self.samplenum - self.samplenumber_last
209 pin = self.wait({'skip': 8 * samples}) # Wait for end of sync bit.
210 self.es = self.samplenum
211 self.putx([4, ['Sync']]) # Write sync label.
212 self.reset() # Reset and wait for next set of pulses.
213 self.state = 'DECODE_TIMEOUT'
214 if not self.state == 'DECODE_TIMEOUT':
215 self.samplenumber_last = self.samplenum