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 from common.srdhelper import bitpack
21 from .lists import ADDRESSES
22 import sigrokdecode as srd
24 class SamplerateError(Exception):
27 class SIRCError(Exception):
30 class SIRCErrorSilent(SIRCError):
33 class Decoder(srd.Decoder):
37 longname = 'Sony IR (SIRC)'
38 desc = 'Sony infrared remote control protocol (SIRC).'
44 {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'},
47 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low',
48 'values': ('active-low', 'active-high')},
55 ('command', 'Command'),
56 ('address', 'Address'),
57 ('extended', 'Extended'),
59 ('warning', 'Warning'),
62 ('bits', 'Bits', (0, 1, 2)),
63 ('fields', 'Fields', (3, 4, 5, 6)),
64 ('remotes', 'Remotes', (7,)),
65 ('warnings', 'Warnings', (8,)),
75 self.out_ann = self.register(srd.OUTPUT_ANN)
76 self.active = self.options['polarity'] == 'active-high'
78 def metadata(self, key, value):
79 if key == srd.SRD_CONF_SAMPLERATE:
80 self.samplerate = value
82 def putg(self, ss, es, cls, texts):
83 self.put(ss, es, self.out_ann, [cls, texts])
85 def tolerance(self, start, end, expected):
86 microseconds = 1000000 * (end - start) / self.samplerate
87 tolerance = expected * 0.30
88 return (expected - tolerance) < microseconds < (expected + tolerance)
90 def wait(self, *conds, timeout=None):
92 if timeout is not None:
93 to = int(self.samplerate * timeout / 1000000)
94 conds.append({'skip': to})
95 start = self.samplenum
96 signals = super(Decoder, self).wait(conds)
98 return signals, start, end, self.matched
100 def read_pulse(self, high, time):
101 e = 'f' if high else 'r'
102 max_time = int(time * 1.30)
103 signals, start, end, (edge, timeout) = self.wait({0: e}, timeout=max_time)
104 if timeout or not self.tolerance(start, end, time):
105 raise SIRCError('Timeout')
106 return signals, start, end, (edge, timeout)
109 e = 'f' if self.active else 'r'
110 signals, high_start, high_end, (edge, timeout) = self.wait({0: e}, timeout=2000)
112 raise SIRCError('Bit High Timeout')
113 if self.tolerance(high_start, high_end, 1200):
115 elif self.tolerance(high_start, high_end, 600):
118 raise SIRCError('Bit Low Timeout')
120 signals, low_start, low_end, matched = self.read_pulse(not self.active, 600)
123 low_end = high_end + int(600 * self.samplerate / 1000000)
125 self.putg(high_start, low_end, 0, ['{}'.format(bit)])
126 return bit, high_start, low_end, good
128 def read_signal(self):
131 signals, agc_start, agc_end, matched = self.read_pulse(self.active, 2400)
132 signals, pause_start, pause_end, matched = self.read_pulse(not self.active, 600)
134 raise SIRCErrorSilent('not an SIRC message')
135 self.putg(agc_start, agc_end, 1, ['AGC', 'A'])
136 self.putg(pause_start, pause_end, 2, ['Pause', 'P'])
137 self.putg(agc_start, pause_end, 3, ['Start', 'S'])
142 bit, start, end, good = self.read_bit()
143 bits.append((bit, start, end))
145 raise SIRCError('too many bits')
151 elif len(bits) == 15:
155 elif len(bits) == 20:
158 extended = bits[12:20]
160 raise SIRCError('incorrect number of bits: {}'.format(len(bits)))
163 command_num = bitpack([b[0] for b in command])
164 address_num = bitpack([b[0] for b in address])
165 command_str = '0x{:02X}'.format(command_num)
166 address_str = '0x{:02X}'.format(address_num)
167 self.putg(command[0][1], command[-1][2], 4, [
168 'Command: {}'.format(command_str),
169 'C:{}'.format(command_str),
171 self.putg(address[0][1], address[-1][2], 5, [
172 'Address: {}'.format(address_str),
173 'A:{}'.format(address_str),
177 extended_num = bitpack([b[0] for b in extended])
178 extended_str = '0x{:02X}'.format(extended_num)
179 self.putg(extended[0][1], extended[-1][2], 6, [
180 'Extended: {}'.format(extended_str),
181 'E:{}'.format(extended_str),
183 return address_num, command_num, extended_num, bits[0][1], bits[-1][2]
186 if not self.samplerate:
187 raise SamplerateError('Cannot decode without samplerate.')
190 e = 'h' if self.active else 'l'
191 signal, start, end, matched = self.wait({0: e})
193 address, command, extended, payload_start, payload_end = self.read_signal()
194 names, commands = ADDRESSES.get((address, extended), (['Unknown Device: ', 'UNK: '], {}))
195 text = commands.get(command, 'Unknown')
196 self.putg(end, payload_end, 7, [n + text for n in names])
197 except SIRCErrorSilent as e:
199 except SIRCError as e:
200 self.putg(end, self.samplenum, 8, [
201 'Error: {}'.format(e),