]> sigrok.org Git - libsigrokdecode.git/blame - decoders/rc_encode/pd.py
Add initial rc_encode protocol decoder.
[libsigrokdecode.git] / decoders / rc_encode / pd.py
CommitLineData
2a2c9b16 1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 Steve R <steversig@virginmedia.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
22def 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
28 if ( # 0 -___-___
29 (int(edges[1]) >= int(edges[0]) * lmin and int(edges[1]) <= int(edges[0]) * lmax) and
30 (int(edges[2]) >= int(edges[0]) * eqmin and int(edges[2]) <= int(edges[0]) * eqmax) and
31 (int(edges[3]) >= int(edges[0]) * lmin and int(edges[3]) <= int(edges[0]) * lmax)):
32 return '0'
33 elif ( # 1 ---_---_
34 (int(edges[0]) >= int(edges[1]) * lmin and int(edges[0]) <= int(edges[1]) * lmax) and
35 (int(edges[0]) >= int(edges[2]) * eqmin and int(edges[0]) <= int(edges[2]) * eqmax) and
36 (int(edges[0]) >= int(edges[3]) * lmin and int(edges[0]) <= int(edges[3]) * lmax)):
37 return '1'
38 elif ( # float ---_-___
39 (int(edges[1]) >= int(edges[0]) * lmin and int(edges[1]) <= int(edges[0]) * lmax) and
40 (int(edges[2]) >= int(edges[0]) * lmin and int(edges[2]) <= int(edges[0]) * lmax) and
41 (int(edges[3]) >= int(edges[0]) * eqmin and int(edges[3]) <= int(edges[0]) * eqmax)):
42 return 'f'
43 else:
44 return 'U'
45
46def pinlabels(bit_count):
47 if bit_count <= 6:
48 return 'A%i' % (bit_count - 1)
49 else:
50 return 'A%i/D%i' % (bit_count - 1, 12 - bit_count)
51
52def decode_model(model, bits):
53 if model == 'maplin_l95ar':
54 address = 'Addr' # Address pins A0 to A5
55 for i in range(0, 6):
56 address = address + ' %i:' % (i + 1) + \
57 ('on' if bits[i][0] == '0' else 'off')
58 button = 'Button'
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'
68 else:
69 button = button + ' Unknown'
70 return ['%s' % address, bits[0][1], bits[5][2], \
71 '%s' % button, bits[6][1], bits[11][2]]
72
73class Decoder(srd.Decoder):
74 api_version = 3
75 id = 'rc_encode'
76 name = 'RC encode'
77 longname = 'Remote control encoder'
78 desc = 'PT2262/HX2262/SC5262 remote control encoder protocol.'
79 license = 'gplv2+'
80 inputs = ['logic']
81 outputs = []
82 channels = (
83 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
84 )
85 annotations = (
86 ('bits', 'Bits'),
87 ('pins', 'Pins'),
88 ('remote', 'Remote'),
89 )
90 annotation_rows = (
91 ('bits', 'Bits', (0,)),
92 ('pins', 'Pins', (1,)),
93 ('remote', 'Remote', (2,)),
94 )
95 options = (
96 {'id': 'remote', 'desc': 'Remote', 'default': 'none',
97 'values': ('none', 'maplin_l95ar')},
98 )
99
100 def __init__(self):
101 self.reset()
102
103 def reset(self):
104 self.samplenumber_last = None
105 self.pulses = []
106 self.bits = []
107 self.labels = []
108 self.bit_count = 0
109 self.bit_first = None
110 self.bit_last = None
111 self.state = 'IDLE'
112
113 def start(self):
114 self.out_ann = self.register(srd.OUTPUT_ANN)
115 self.model = self.options['remote']
116
117 def decode(self):
118 while True:
119 pin = self.wait({0: 'e'})
120 self.state = 'DECODING'
121
122 if not self.samplenumber_last: # Set counters to start of signal.
123 self.samplenumber_last = self.samplenum
124 self.bit_first = self.samplenum
125 continue
126
127 if self.bit_count < 12: # Decode A0 to A11.
128 self.bit_count += 1
129 for i in range(0, 4): # Get four pulses for each bit.
130 if i > 0:
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.
142 self.pulses = []
143 self.bit_first = self.samplenum
144 else:
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