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_lsb
21 from .lists import ADDRESSES
22 import sigrokdecode as srd
24 class SamplerateError(Exception):
27 class SIRCError(Exception):
30 class SIRCErrorSilent(SIRCError):
34 BIT, AGC, PAUSE, START, CMD, ADDR, EXT, REMOTE, WARN = range(9)
41 class Decoder(srd.Decoder):
45 longname = 'Sony IR (SIRC)'
46 desc = 'Sony infrared remote control protocol (SIRC).'
52 {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'},
55 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low',
56 'values': ('active-low', 'active-high')},
63 ('command', 'Command'),
64 ('address', 'Address'),
65 ('extended', 'Extended'),
67 ('warning', 'Warning'),
70 ('bits', 'Bits', (Ann.BIT, Ann.AGC, Ann.PAUSE)),
71 ('fields', 'Fields', (Ann.START, Ann.CMD, Ann.ADDR, Ann.EXT)),
72 ('remotes', 'Remotes', (Ann.REMOTE,)),
73 ('warnings', 'Warnings', (Ann.WARN,)),
83 self.out_ann = self.register(srd.OUTPUT_ANN)
84 self.active = self.options['polarity'] == 'active-high'
86 def metadata(self, key, value):
87 if key == srd.SRD_CONF_SAMPLERATE:
88 self.samplerate = value
89 self.snum_per_us = self.samplerate / 1e6
91 def putg(self, ss, es, cls, texts):
92 self.put(ss, es, self.out_ann, [cls, texts])
94 def tolerance(self, ss, es, expected):
95 microseconds = (es - ss) / self.snum_per_us
96 tolerance = expected * 0.30
97 return (expected - tolerance) < microseconds < (expected + tolerance)
99 def wait_wrap(self, conds, timeout):
100 if timeout is not None:
101 to = int(timeout * self.snum_per_us)
102 conds.append({'skip': to})
104 pins = self.wait(conds)
106 return pins, ss, es, self.matched
108 def read_pulse(self, high, time):
109 e = 'f' if high else 'r'
110 max_time = int(time * 1.30)
111 (ir,), ss, es, (edge, timeout) = self.wait_wrap([{0: e}], max_time)
112 if timeout or not self.tolerance(ss, es, time):
113 raise SIRCError('Timeout')
114 return ir, ss, es, (edge, timeout)
117 e = 'f' if self.active else 'r'
118 _, high_ss, high_es, (edge, timeout) = self.wait_wrap([{0: e}], 2000)
120 raise SIRCError('Bit High Timeout')
121 if self.tolerance(high_ss, high_es, ONE_USEC):
123 elif self.tolerance(high_ss, high_es, ZERO_USEC):
126 raise SIRCError('Bit Low Timeout')
128 _, low_ss, low_es, _ = self.read_pulse(not self.active, PAUSE_USEC)
131 low_es = high_es + int(PAUSE_USEC * self.snum_per_us)
133 self.putg(high_ss, low_es, Ann.BIT, ['{}'.format(bit)])
134 return bit, high_ss, low_es, good
136 def read_signal(self):
139 _, agc_ss, agc_es, _ = self.read_pulse(self.active, AGC_USEC)
140 _, pause_ss, pause_es, _ = self.read_pulse(not self.active, PAUSE_USEC)
142 raise SIRCErrorSilent('not an SIRC message')
143 self.putg(agc_ss, agc_es, Ann.AGC, ['AGC', 'A'])
144 self.putg(pause_ss, pause_es, Ann.PAUSE, ['Pause', 'P'])
145 self.putg(agc_ss, pause_es, Ann.START, ['Start', 'S'])
150 bit, ss, es, good = self.read_bit()
151 bits.append((bit, ss, es))
153 raise SIRCError('too many bits')
159 elif len(bits) == 15:
163 elif len(bits) == 20:
166 extended = bits[12:20]
168 raise SIRCError('incorrect bits count {}'.format(len(bits)))
171 command_num = bitpack_lsb(command, 0)
172 address_num = bitpack_lsb(address, 0)
173 command_str = '0x{:02X}'.format(command_num)
174 address_str = '0x{:02X}'.format(address_num)
175 self.putg(command[0][1], command[-1][2], Ann.CMD, [
176 'Command: {}'.format(command_str),
177 'C:{}'.format(command_str),
179 self.putg(address[0][1], address[-1][2], Ann.ADDR, [
180 'Address: {}'.format(address_str),
181 'A:{}'.format(address_str),
185 extended_num = bitpack_lsb(extended, 0)
186 extended_str = '0x{:02X}'.format(extended_num)
187 self.putg(extended[0][1], extended[-1][2], Ann.EXT, [
188 'Extended: {}'.format(extended_str),
189 'E:{}'.format(extended_str),
191 return address_num, command_num, extended_num, bits[0][1], bits[-1][2]
194 if not self.samplerate:
195 raise SamplerateError('Cannot decode without samplerate.')
197 unknown = (['Unknown Device: ', 'UNK: '], {})
199 e = 'h' if self.active else 'l'
200 _, _, frame_ss, _ = self.wait_wrap([{0: e}], None)
202 addr, cmd, ext, payload_ss, payload_es = self.read_signal()
203 names, cmds = ADDRESSES.get((addr, ext), unknown)
204 text = cmds.get(cmd, 'Unknown')
205 self.putg(frame_ss, payload_es, Ann.REMOTE, [
206 n + text for n in names
208 except SIRCErrorSilent as e:
210 except SIRCError as e:
211 self.putg(frame_ss, self.samplenum, Ann.WARN, [
212 'Error: {}'.format(e),