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 def decode_bit(edges):
23 # Datasheet says long pulse is 3 times short pulse.
24 lmin = 2 # long min multiplier
25 lmax = 5 # long max multiplier
26 eqmin = 0.5 # equal min multiplier
27 eqmax = 1.5 # equal max multiplier
29 (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax) and
30 (edges[2] >= edges[0] * eqmin and edges[2] <= edges[0] * eqmax) and
31 (edges[3] >= edges[0] * lmin and edges[3] <= edges[0] * lmax)):
34 (edges[0] >= edges[1] * lmin and edges[0] <= edges[1] * lmax) and
35 (edges[0] >= edges[2] * eqmin and edges[0] <= edges[2] * eqmax) and
36 (edges[0] >= edges[3] * lmin and edges[0] <= edges[3] * lmax)):
38 elif ( # float ---_-___
39 (edges[1] >= edges[0] * lmin and edges[1] <= edges[0] * lmax) and
40 (edges[2] >= edges[0] * lmin and edges[2] <= edges[0]* lmax) and
41 (edges[3] >= edges[0] * eqmin and edges[3] <= edges[0] * eqmax)):
46 def pinlabels(bit_count):
48 return 'A%i' % (bit_count - 1)
50 return 'A%i/D%i' % (bit_count - 1, 12 - bit_count)
52 def decode_model(model, bits):
53 if model == 'maplin_l95ar':
54 address = 'Addr' # Address pins A0 to A5
56 address = address + ' %i:' % (i + 1) + \
57 ('on' if bits[i][0] == '0' else 'off')
59 # Button pins A6/D5 to A11/D0
60 if bits[6][0] == '0' and bits[11][0] == '0':
61 button = button + ' A ON/OFF'
62 elif bits[7][0] == '0' and bits[11][0] == '0':
63 button = button + ' B ON/OFF'
64 elif bits[9][0] == '0' and bits[11][0] == '0':
65 button = button + ' C ON/OFF'
66 elif bits[8][0] == '0' and bits[11][0] == '0':
67 button = button + ' D ON/OFF'
69 button = button + ' Unknown'
70 return ['%s' % address, bits[0][1], bits[5][2], \
71 '%s' % button, bits[6][1], bits[11][2]]
73 class Decoder(srd.Decoder):
77 longname = 'Remote control encoder'
78 desc = 'PT2262/HX2262/SC5262 remote control encoder protocol.'
83 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
91 ('bits', 'Bits', (0,)),
92 ('pins', 'Pins', (1,)),
93 ('remote', 'Remote', (2,)),
96 {'id': 'remote', 'desc': 'Remote', 'default': 'none',
97 'values': ('none', 'maplin_l95ar')},
104 self.samplenumber_last = None
109 self.bit_first = None
114 self.out_ann = self.register(srd.OUTPUT_ANN)
115 self.model = self.options['remote']
119 pin = self.wait({0: 'e'})
120 self.state = 'DECODING'
122 if not self.samplenumber_last: # Set counters to start of signal.
123 self.samplenumber_last = self.samplenum
124 self.bit_first = self.samplenum
127 if self.bit_count < 12: # Decode A0 to A11.
129 for i in range(0, 4): # Get four pulses for each bit.
131 pin = self.wait({0: 'e'}) # Get next 3 edges.
132 samples = self.samplenum - self.samplenumber_last
133 self.pulses.append(samples) # Save the pulse width.
134 self.samplenumber_last = self.samplenum
135 self.bit_last = self.samplenum
136 self.bits.append([decode_bit(self.pulses), self.bit_first,
137 self.bit_last]) # Save states and times.
138 self.put(self.bit_first, self.bit_last, self.out_ann,
139 [0, [decode_bit(self.pulses)]]) # Write decoded bit.
140 self.put(self.bit_first, self.bit_last, self.out_ann,
141 [1, [pinlabels(self.bit_count)]]) # Write pin labels.
143 self.bit_first = self.samplenum
145 if self.model != 'none':
146 self.labels = decode_model(self.model, self.bits)
147 self.put(self.labels[1], self.labels[2], self.out_ann,
148 [2, [self.labels[0]]]) # Write model decode.
149 self.put(self.labels[4], self.labels[5], self.out_ann,
150 [2, [self.labels[3]]]) # Write model decode.
151 samples = self.samplenum - self.samplenumber_last
152 pin = self.wait({'skip': 8 * samples}) # Wait for end of sync bit.
153 self.bit_last = self.samplenum
154 self.put(self.bit_first, self.bit_last, self.out_ann,
155 [0, ['Sync']]) # Write sync label.
156 self.reset() # Reset and wait for next set of pulses.
157 self.state = 'DECODE_TIMEOUT'
158 if not self.state == 'DECODE_TIMEOUT':
159 self.samplenumber_last = self.samplenum