X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fir_sirc%2Fpd.py;h=14ba63f0d10a75a0be3289a5111e05566bf8d832;hb=3851b0c084bc358b134069ba829b7ce1f4c410ff;hp=9aba8a53c4e9aa448ff4f9bbb4e6732eba61d2ac;hpb=3e10fce7f87ff1bae97241a8b8f9d3602c3bfe8a;p=libsigrokdecode.git diff --git a/decoders/ir_sirc/pd.py b/decoders/ir_sirc/pd.py index 9aba8a5..14ba63f 100644 --- a/decoders/ir_sirc/pd.py +++ b/decoders/ir_sirc/pd.py @@ -17,8 +17,9 @@ ## along with this program; if not, see . ## -import sigrokdecode as srd +from common.srdhelper import bitpack_lsb from .lists import ADDRESSES +import sigrokdecode as srd class SamplerateError(Exception): pass @@ -29,45 +30,51 @@ class SIRCError(Exception): class SIRCErrorSilent(SIRCError): pass +class Ann: + BIT, AGC, PAUSE, START, CMD, ADDR, EXT, REMOTE, WARN = range(9) + +AGC_USEC = 2400 +ONE_USEC = 1200 +ZERO_USEC = 600 +PAUSE_USEC = 600 + class Decoder(srd.Decoder): api_version = 3 id = 'ir_sirc' name = 'IR SIRC' - longname = 'IR SIRC' - desc = 'Sony SIRC infrared remote control protocol.' + longname = 'Sony IR (SIRC)' + desc = 'Sony infrared remote control protocol (SIRC).' license = 'gplv2+' + tags = ['IR'] inputs = ['logic'] - outputs = ['ir_sirc'] + outputs = [] channels = ( - dict(id='ir', name='IR', desc='Data line'), + {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'}, ) options = ( - dict(id='polarity', desc='Polarity', default='active-low', - values=('active-low', 'active-high')), + {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low', + 'values': ('active-low', 'active-high')}, ) annotations = ( ('bit', 'Bit'), ('agc', 'AGC'), ('pause', 'Pause'), - ('start', 'Start'), ('command', 'Command'), ('address', 'Address'), ('extended', 'Extended'), - ('remote', 'Remote'), - - ('warnings', 'Warnings'), + ('warning', 'Warning'), ) annotation_rows = ( - ('bits', 'Bits', (0, 1, 2)), - ('fields', 'Fields', (3, 4, 5, 6)), - ('remote', 'Remote', (7,)), - ('warnings', 'Warnings', (8,)), + ('bits', 'Bits', (Ann.BIT, Ann.AGC, Ann.PAUSE)), + ('fields', 'Fields', (Ann.START, Ann.CMD, Ann.ADDR, Ann.EXT)), + ('remotes', 'Remotes', (Ann.REMOTE,)), + ('warnings', 'Warnings', (Ann.WARN,)), ) def __init__(self): - pass + self.reset() def reset(self): pass @@ -79,66 +86,69 @@ class Decoder(srd.Decoder): def metadata(self, key, value): if key == srd.SRD_CONF_SAMPLERATE: self.samplerate = value + self.snum_per_us = self.samplerate / 1e6 + + def putg(self, ss, es, cls, texts): + self.put(ss, es, self.out_ann, [cls, texts]) - def tolerance(self, start, end, expected): - microseconds = 1000000 * (end - start) / self.samplerate + def tolerance(self, ss, es, expected): + microseconds = (es - ss) / self.snum_per_us tolerance = expected * 0.30 return (expected - tolerance) < microseconds < (expected + tolerance) - def wait(self, *conds, timeout=None): - conds = list(conds) + def wait_wrap(self, conds, timeout): if timeout is not None: - to = int(self.samplerate * timeout / 1000000) + to = int(timeout * self.snum_per_us) conds.append({'skip': to}) - start = self.samplenum - signals = super(Decoder, self).wait(conds) - end = self.samplenum - return signals, start, end, self.matched + ss = self.samplenum + pins = self.wait(conds) + es = self.samplenum + return pins, ss, es, self.matched def read_pulse(self, high, time): e = 'f' if high else 'r' max_time = int(time * 1.30) - signals, start, end, (edge, timeout) = self.wait({0: e}, timeout=max_time) - if timeout or not self.tolerance(start, end, time): + (ir,), ss, es, (edge, timeout) = self.wait_wrap([{0: e}], max_time) + if timeout or not self.tolerance(ss, es, time): raise SIRCError('Timeout') - return signals, start, end, (edge, timeout) + return ir, ss, es, (edge, timeout) def read_bit(self): e = 'f' if self.active else 'r' - signals, high_start, high_end, (edge, timeout) = self.wait({0:e}, timeout=2000) + _, high_ss, high_es, (edge, timeout) = self.wait_wrap([{0: e}], 2000) if timeout: raise SIRCError('Bit High Timeout') - if self.tolerance(high_start, high_end, 1200): + if self.tolerance(high_ss, high_es, ONE_USEC): bit = 1 - elif self.tolerance(high_start, high_end, 600): + elif self.tolerance(high_ss, high_es, ZERO_USEC): bit = 0 else: raise SIRCError('Bit Low Timeout') try: - signals, low_start, low_end, matched = self.read_pulse(not self.active, 600) + _, low_ss, low_es, _ = self.read_pulse(not self.active, PAUSE_USEC) good = True except SIRCError: - low_end = high_end + int(600 * self.samplerate / 1000000) + low_es = high_es + int(PAUSE_USEC * self.snum_per_us) good = False - self.put(high_start, low_end, self.out_ann, [0, [str(bit)]]) - return bit, high_start, low_end, good + self.putg(high_ss, low_es, Ann.BIT, ['{}'.format(bit)]) + return bit, high_ss, low_es, good def read_signal(self): # Start code try: - signals, agc_start, agc_end, matched = self.read_pulse(self.active, 2400) - signals, pause_start, pause_end, matched = self.read_pulse(not self.active, 600) + _, agc_ss, agc_es, _ = self.read_pulse(self.active, AGC_USEC) + _, pause_ss, pause_es, _ = self.read_pulse(not self.active, PAUSE_USEC) except SIRCError: raise SIRCErrorSilent('not an SIRC message') - self.put(agc_start, agc_end, self.out_ann, [1, ['AGC', 'A']]) - self.put(pause_start, pause_end, self.out_ann, [2, ['Pause', 'P']]) - self.put(agc_start, pause_end, self.out_ann, [3, ['Start', 'S']]) + self.putg(agc_ss, agc_es, Ann.AGC, ['AGC', 'A']) + self.putg(pause_ss, pause_es, Ann.PAUSE, ['Pause', 'P']) + self.putg(agc_ss, pause_es, Ann.START, ['Start', 'S']) # Read bits bits = [] while True: - bit, start, end, good = self.read_bit() - bits.append((bit, start, end)) + bit, ss, es, good = self.read_bit() + bits.append((bit, ss, es)) if len(bits) > 20: raise SIRCError('too many bits') if not good: @@ -155,37 +165,51 @@ class Decoder(srd.Decoder): address = bits[7:12] extended = bits[12:20] else: - raise SIRCError('incorrect number of bits: {}'.format(len(bits))) + raise SIRCError('incorrect bits count {}'.format(len(bits))) break - number = lambda bits:sum(b << i for i, (b, s, e) in enumerate(bits)) - command_num = number(command) - address_num = number(address) + command_num = bitpack_lsb(command, 0) + address_num = bitpack_lsb(address, 0) command_str = '0x{:02X}'.format(command_num) address_str = '0x{:02X}'.format(address_num) - self.put(command[0][1], command[-1][2], self.out_ann, [4, ['Command: ' + command_str, 'C:' + command_str]]) - self.put(address[0][1], address[-1][2], self.out_ann, [5, ['Address: ' + address_str, 'A:' + address_str]]) + self.putg(command[0][1], command[-1][2], Ann.CMD, [ + 'Command: {}'.format(command_str), + 'C:{}'.format(command_str), + ]) + self.putg(address[0][1], address[-1][2], Ann.ADDR, [ + 'Address: {}'.format(address_str), + 'A:{}'.format(address_str), + ]) extended_num = None if extended: - extended_num = number(extended) + extended_num = bitpack_lsb(extended, 0) extended_str = '0x{:02X}'.format(extended_num) - self.put(extended[0][1], extended[-1][2], self.out_ann, [6, ['Extended: ' + extended_str, 'E:' + extended_str]]) + self.putg(extended[0][1], extended[-1][2], Ann.EXT, [ + 'Extended: {}'.format(extended_str), + 'E:{}'.format(extended_str), + ]) return address_num, command_num, extended_num, bits[0][1], bits[-1][2] def decode(self): if not self.samplerate: raise SamplerateError('Cannot decode without samplerate.') + unknown = (['Unknown Device: ', 'UNK: '], {}) while True: e = 'h' if self.active else 'l' - signal, start, end, matched = self.wait({0:e}) + _, _, frame_ss, _ = self.wait_wrap([{0: e}], None) try: - address, command, extended, payload_start, payload_end = self.read_signal() - names, commands = ADDRESSES.get((address, extended), (['Unknown Device: ', 'UNK: '], {})) - text = commands.get(command, 'Unknown') - self.put(end, payload_end, self.out_ann, [7, [n + text for n in names]]) + addr, cmd, ext, payload_ss, payload_es = self.read_signal() + names, cmds = ADDRESSES.get((addr, ext), unknown) + text = cmds.get(cmd, 'Unknown') + self.putg(frame_ss, payload_es, Ann.REMOTE, [ + n + text for n in names + ]) except SIRCErrorSilent as e: - continue + pass except SIRCError as e: - self.put(end, self.samplenum, self.out_ann, [8, ['Error: ' + str(e), 'Error', 'E']]) - continue + self.putg(frame_ss, self.samplenum, Ann.WARN, [ + 'Error: {}'.format(e), + 'Error', + 'E', + ])