2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2020 Tom Flanagan <knio@zkpq.ca>
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
21 from .lists import ADDRESSES
23 class SamplerateError(Exception):
26 class SIRCError(Exception):
29 class SIRCErrorSilent(SIRCError):
32 class Decoder(srd.Decoder):
37 desc = 'Sony SIRC infrared remote control protocol.'
42 dict(id='ir', name='IR', desc='Data line'),
45 dict(id='polarity', desc='Polarity', default='active-low',
46 values=('active-low', 'active-high')),
54 ('command', 'Command'),
55 ('address', 'Address'),
56 ('extended', 'Extended'),
60 ('warnings', 'Warnings'),
63 ('bits', 'Bits', (0, 1, 2)),
64 ('fields', 'Fields', (3, 4, 5, 6)),
65 ('remote', 'Remote', (7,)),
66 ('warnings', 'Warnings', (8,)),
76 self.out_ann = self.register(srd.OUTPUT_ANN)
77 self.active = self.options['polarity'] == 'active-high'
79 def metadata(self, key, value):
80 if key == srd.SRD_CONF_SAMPLERATE:
81 self.samplerate = value
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)
88 def wait(self, *conds, timeout=None):
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)
96 return signals, start, end, self.matched
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)
107 e = 'f' if self.active else 'r'
108 signals, high_start, high_end, (edge, timeout) = self.wait({0:e}, timeout=2000)
110 raise SIRCError('Bit High Timeout')
111 if self.tolerance(high_start, high_end, 1200):
113 elif self.tolerance(high_start, high_end, 600):
116 raise SIRCError('Bit Low Timeout')
118 signals, low_start, low_end, matched = self.read_pulse(not self.active, 600)
121 low_end = high_end + int(600 * self.samplerate / 1000000)
123 self.put(high_start, low_end, self.out_ann, [0, [str(bit)]])
124 return bit, high_start, low_end, good
126 def read_signal(self):
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)
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']])
140 bit, start, end, good = self.read_bit()
141 bits.append((bit, start, end))
143 raise SIRCError('too many bits')
149 elif len(bits) == 15:
153 elif len(bits) == 20:
156 extended = bits[12:20]
158 raise SIRCError('incorrect number of bits: {}'.format(len(bits)))
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]])
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]
176 if not self.samplerate:
177 raise SamplerateError('Cannot decode without samplerate.')
180 e = 'h' if self.active else 'l'
181 signal, start, end, matched = self.wait({0:e})
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:
189 except SIRCError as e:
190 self.put(end, self.samplenum, self.out_ann, [8, ['Error: ' + str(e), 'Error', 'E']])