]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ir_sirc/pd.py
ir_sirc: introduce decoder for Sony IR remote control protocol
[libsigrokdecode.git] / decoders / ir_sirc / pd.py
CommitLineData
3e10fce7
TF
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2020 Tom Flanagan <knio@zkpq.ca>
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
21from .lists import ADDRESSES
22
23class SamplerateError(Exception):
24 pass
25
26class SIRCError(Exception):
27 pass
28
29class SIRCErrorSilent(SIRCError):
30 pass
31
32class Decoder(srd.Decoder):
33 api_version = 3
34 id = 'ir_sirc'
35 name = 'IR SIRC'
36 longname = 'IR SIRC'
37 desc = 'Sony SIRC infrared remote control protocol.'
38 license = 'gplv2+'
39 inputs = ['logic']
40 outputs = ['ir_sirc']
41 channels = (
42 dict(id='ir', name='IR', desc='Data line'),
43 )
44 options = (
45 dict(id='polarity', desc='Polarity', default='active-low',
46 values=('active-low', 'active-high')),
47 )
48 annotations = (
49 ('bit', 'Bit'),
50 ('agc', 'AGC'),
51 ('pause', 'Pause'),
52
53 ('start', 'Start'),
54 ('command', 'Command'),
55 ('address', 'Address'),
56 ('extended', 'Extended'),
57
58 ('remote', 'Remote'),
59
60 ('warnings', 'Warnings'),
61 )
62 annotation_rows = (
63 ('bits', 'Bits', (0, 1, 2)),
64 ('fields', 'Fields', (3, 4, 5, 6)),
65 ('remote', 'Remote', (7,)),
66 ('warnings', 'Warnings', (8,)),
67 )
68
69 def __init__(self):
70 pass
71
72 def reset(self):
73 pass
74
75 def start(self):
76 self.out_ann = self.register(srd.OUTPUT_ANN)
77 self.active = self.options['polarity'] == 'active-high'
78
79 def metadata(self, key, value):
80 if key == srd.SRD_CONF_SAMPLERATE:
81 self.samplerate = value
82
83 def tolerance(self, start, end, expected):
84 microseconds = 1000000 * (end - start) / self.samplerate
85 tolerance = expected * 0.30
86 return (expected - tolerance) < microseconds < (expected + tolerance)
87
88 def wait(self, *conds, timeout=None):
89 conds = list(conds)
90 if timeout is not None:
91 to = int(self.samplerate * timeout / 1000000)
92 conds.append({'skip': to})
93 start = self.samplenum
94 signals = super(Decoder, self).wait(conds)
95 end = self.samplenum
96 return signals, start, end, self.matched
97
98 def read_pulse(self, high, time):
99 e = 'f' if high else 'r'
100 max_time = int(time * 1.30)
101 signals, start, end, (edge, timeout) = self.wait({0: e}, timeout=max_time)
102 if timeout or not self.tolerance(start, end, time):
103 raise SIRCError('Timeout')
104 return signals, start, end, (edge, timeout)
105
106 def read_bit(self):
107 e = 'f' if self.active else 'r'
108 signals, high_start, high_end, (edge, timeout) = self.wait({0:e}, timeout=2000)
109 if timeout:
110 raise SIRCError('Bit High Timeout')
111 if self.tolerance(high_start, high_end, 1200):
112 bit = 1
113 elif self.tolerance(high_start, high_end, 600):
114 bit = 0
115 else:
116 raise SIRCError('Bit Low Timeout')
117 try:
118 signals, low_start, low_end, matched = self.read_pulse(not self.active, 600)
119 good = True
120 except SIRCError:
121 low_end = high_end + int(600 * self.samplerate / 1000000)
122 good = False
123 self.put(high_start, low_end, self.out_ann, [0, [str(bit)]])
124 return bit, high_start, low_end, good
125
126 def read_signal(self):
127 # Start code
128 try:
129 signals, agc_start, agc_end, matched = self.read_pulse(self.active, 2400)
130 signals, pause_start, pause_end, matched = self.read_pulse(not self.active, 600)
131 except SIRCError:
132 raise SIRCErrorSilent('not an SIRC message')
133 self.put(agc_start, agc_end, self.out_ann, [1, ['AGC', 'A']])
134 self.put(pause_start, pause_end, self.out_ann, [2, ['Pause', 'P']])
135 self.put(agc_start, pause_end, self.out_ann, [3, ['Start', 'S']])
136
137 # Read bits
138 bits = []
139 while True:
140 bit, start, end, good = self.read_bit()
141 bits.append((bit, start, end))
142 if len(bits) > 20:
143 raise SIRCError('too many bits')
144 if not good:
145 if len(bits) == 12:
146 command = bits[0:7]
147 address = bits[7:12]
148 extended = []
149 elif len(bits) == 15:
150 command = bits[0:7]
151 address = bits[7:15]
152 extended = []
153 elif len(bits) == 20:
154 command = bits[0:7]
155 address = bits[7:12]
156 extended = bits[12:20]
157 else:
158 raise SIRCError('incorrect number of bits: {}'.format(len(bits)))
159 break
160
161 number = lambda bits:sum(b << i for i, (b, s, e) in enumerate(bits))
162 command_num = number(command)
163 address_num = number(address)
164 command_str = '0x{:02X}'.format(command_num)
165 address_str = '0x{:02X}'.format(address_num)
166 self.put(command[0][1], command[-1][2], self.out_ann, [4, ['Command: ' + command_str, 'C:' + command_str]])
167 self.put(address[0][1], address[-1][2], self.out_ann, [5, ['Address: ' + address_str, 'A:' + address_str]])
168 extended_num = None
169 if extended:
170 extended_num = number(extended)
171 extended_str = '0x{:02X}'.format(extended_num)
172 self.put(extended[0][1], extended[-1][2], self.out_ann, [6, ['Extended: ' + extended_str, 'E:' + extended_str]])
173 return address_num, command_num, extended_num, bits[0][1], bits[-1][2]
174
175 def decode(self):
176 if not self.samplerate:
177 raise SamplerateError('Cannot decode without samplerate.')
178
179 while True:
180 e = 'h' if self.active else 'l'
181 signal, start, end, matched = self.wait({0:e})
182 try:
183 address, command, extended, payload_start, payload_end = self.read_signal()
184 names, commands = ADDRESSES.get((address, extended), (['Unknown Device: ', 'UNK: '], {}))
185 text = commands.get(command, 'Unknown')
186 self.put(end, payload_end, self.out_ann, [7, [n + text for n in names]])
187 except SIRCErrorSilent as e:
188 continue
189 except SIRCError as e:
190 self.put(end, self.samplenum, self.out_ann, [8, ['Error: ' + str(e), 'Error', 'E']])
191 continue