From acaf2be9a0bfa4ba6a47b9b5e4499275dff3ddaf Mon Sep 17 00:00:00 2001 From: Abdelhak Bougouffa Date: Tue, 2 Feb 2021 10:21:33 +0100 Subject: [PATCH] Add 'Sony IR (SIRC)' decoder from libsigrokdecode codebase --- .../decoders/ir_sirc/__init__.py | 26 +++ libsigrokdecode4DSL/decoders/ir_sirc/lists.py | 201 ++++++++++++++++ libsigrokdecode4DSL/decoders/ir_sirc/pd.py | 215 ++++++++++++++++++ 3 files changed, 442 insertions(+) create mode 100644 libsigrokdecode4DSL/decoders/ir_sirc/__init__.py create mode 100644 libsigrokdecode4DSL/decoders/ir_sirc/lists.py create mode 100644 libsigrokdecode4DSL/decoders/ir_sirc/pd.py diff --git a/libsigrokdecode4DSL/decoders/ir_sirc/__init__.py b/libsigrokdecode4DSL/decoders/ir_sirc/__init__.py new file mode 100644 index 00000000..4061ed73 --- /dev/null +++ b/libsigrokdecode4DSL/decoders/ir_sirc/__init__.py @@ -0,0 +1,26 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2020 Tom Flanagan +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +''' +Decoder for the Sony IR remote control protocol (SIRC). + +https://www.sbprojects.net/knowledge/ir/sirc.php +''' + +from .pd import Decoder diff --git a/libsigrokdecode4DSL/decoders/ir_sirc/lists.py b/libsigrokdecode4DSL/decoders/ir_sirc/lists.py new file mode 100644 index 00000000..5c6d8fa6 --- /dev/null +++ b/libsigrokdecode4DSL/decoders/ir_sirc/lists.py @@ -0,0 +1,201 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2020 Tom Flanagan +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +NUMBERS = { + 0x00: '1', + 0x01: '2', + 0x02: '3', + 0x03: '4', + 0x04: '5', + 0x05: '6', + 0x06: '7', + 0x07: '8', + 0x08: '9', + 0x09: '0/10', +} + +ADDRESSES = { + # TV + (0x01, None): (['TV: ', 'TV:'], { + 0x15: 'Power', + 0x25: 'Input', + + 0x33: 'Right', + 0x34: 'Left', + 0x3A: 'Display', + + 0x60: 'Home', + 0x65: 'Enter', + + 0x74: 'Up', + 0x75: 'Down', + + }), + + # Video + (0x0B, None): (['Video: ', 'V:'], { + 0x18: 'Stop', + 0x19: 'Pause', + 0x1A: 'Play', + 0x1B: 'Rewind', + 0x1C: 'Fast Forward', + + 0x42: 'Up', + 0x43: 'Down', + 0x4D: 'Home', + + 0x51: 'Enter', + 0x5A: 'Display', + + 0x61: 'Right', + 0x62: 'Left', + }), + + # BR Input select + (0x10, 0x28): (['BlueRay: ', 'BR:'], { + 0x16: 'BlueRay', + }), + + # Amp, Game, Sat, Tuner, USB + (0x10, 0x08): (['Playback: ', 'PB:'], { + 0x2A: 'Shuffle', + 0x2C: 'Repeat', + 0x2E: 'Folder Down', + 0x2F: 'Folder Up', + + 0x30: 'Previous', + 0x31: 'Next', + 0x32: 'Play', + 0x33: 'Rewind', + 0x34: 'Fast Forward', + 0x38: 'Stop', + 0x39: 'Pause', + + 0x73: 'Options', + 0x7D: 'Return', + }), + + # CD + (0x11, None): (['CD: ', 'CD:'], { + 0x28: 'Display', + + 0x30: 'Previous', + 0x31: 'Next', + 0x32: 'Play', + 0x33: 'Rewind', + 0x34: 'Fast Forward', + 0x38: 'Stop', + 0x39: 'Pause', + }), + + # BD + (0x1A, 0xE2): (['BlueRay: ', 'BD:'], { + 0x18: 'Stop', + 0x19: 'Pause', + 0x1A: 'Play', + 0x1B: 'Rewind', + 0x1C: 'Fast Forward', + + 0x29: 'Menu', + 0x2C: 'Top Menu', + + 0x39: 'Up', + 0x3A: 'Down', + 0x3B: 'Left', + 0x3C: 'Right', + 0x3D: 'Enter', + 0x3F: 'Options', + + 0x41: 'Display', + 0x42: 'Home', + 0x43: 'Return', + + 0x56: 'Next', + 0x57: 'Previous', + }), + + # DVD + (0x1A, 0x49): (['DVD: ', 'DVD:'], { + 0x0B: 'Enter', + 0x0E: 'Return', + 0x17: 'Options', + + 0x1A: 'Top Menu', + 0x1B: 'Menu', + + 0x30: 'Previous', + 0x31: 'Next', + 0x32: 'Play', + 0x33: 'Rewind', + 0x34: 'Fast Forward', + 0x38: 'Stop', + 0x39: 'Pause', + + 0x54: 'Display', + + 0x7B: 'Left', + 0x7C: 'Right', + 0x79: 'Up', + 0x7A: 'Down', + }), + + # Amp, Game, Sat, Tuner, USB modes + (0x30, None): (['Keypad: ', 'KP:'], { + 0x0C: 'Enter', + + 0x12: 'Volume Up', + 0x13: 'Volume Down', + 0x14: 'Mute', + 0x15: 'Power', + + 0x21: 'Tuner', + 0x22: 'Video', + 0x25: 'CD', + + 0x4D: 'Home', + 0x4B: 'Display', + + 0x60: 'Sleep', + 0x6A: 'TV', + + 0x53: 'Home', + + 0x7C: 'Game', + 0x7D: 'DVD', + }), + + # Amp, Game, Sat, Tuner, USB modes + (0xB0, None): (['Arrows: ', 'Ar:'], { + 0x7A: 'Left', + 0x7B: 'Right', + 0x78: 'Up', + 0x79: 'Down', + 0x77: 'Amp Menu', + }), + + # TV mode + (0x97, None): (['TV Extra', 'TV:'], { + 0x23: 'Return', + 0x36: 'Options', + + }), +} + +for (address, extended), (name, commands) in ADDRESSES.items(): + commands.update(NUMBERS) diff --git a/libsigrokdecode4DSL/decoders/ir_sirc/pd.py b/libsigrokdecode4DSL/decoders/ir_sirc/pd.py new file mode 100644 index 00000000..14ba63f0 --- /dev/null +++ b/libsigrokdecode4DSL/decoders/ir_sirc/pd.py @@ -0,0 +1,215 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2020 Tom Flanagan +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +from common.srdhelper import bitpack_lsb +from .lists import ADDRESSES +import sigrokdecode as srd + +class SamplerateError(Exception): + pass + +class SIRCError(Exception): + pass + +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 = 'Sony IR (SIRC)' + desc = 'Sony infrared remote control protocol (SIRC).' + license = 'gplv2+' + tags = ['IR'] + inputs = ['logic'] + outputs = [] + channels = ( + {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'}, + ) + options = ( + {'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'), + ('warning', 'Warning'), + ) + annotation_rows = ( + ('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): + self.reset() + + def reset(self): + pass + + def start(self): + self.out_ann = self.register(srd.OUTPUT_ANN) + self.active = self.options['polarity'] == 'active-high' + + 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, ss, es, expected): + microseconds = (es - ss) / self.snum_per_us + tolerance = expected * 0.30 + return (expected - tolerance) < microseconds < (expected + tolerance) + + def wait_wrap(self, conds, timeout): + if timeout is not None: + to = int(timeout * self.snum_per_us) + conds.append({'skip': to}) + 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) + (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 ir, ss, es, (edge, timeout) + + def read_bit(self): + e = 'f' if self.active else 'r' + _, high_ss, high_es, (edge, timeout) = self.wait_wrap([{0: e}], 2000) + if timeout: + raise SIRCError('Bit High Timeout') + if self.tolerance(high_ss, high_es, ONE_USEC): + bit = 1 + elif self.tolerance(high_ss, high_es, ZERO_USEC): + bit = 0 + else: + raise SIRCError('Bit Low Timeout') + try: + _, low_ss, low_es, _ = self.read_pulse(not self.active, PAUSE_USEC) + good = True + except SIRCError: + low_es = high_es + int(PAUSE_USEC * self.snum_per_us) + good = False + self.putg(high_ss, low_es, Ann.BIT, ['{}'.format(bit)]) + return bit, high_ss, low_es, good + + def read_signal(self): + # Start code + try: + _, 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.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, ss, es, good = self.read_bit() + bits.append((bit, ss, es)) + if len(bits) > 20: + raise SIRCError('too many bits') + if not good: + if len(bits) == 12: + command = bits[0:7] + address = bits[7:12] + extended = [] + elif len(bits) == 15: + command = bits[0:7] + address = bits[7:15] + extended = [] + elif len(bits) == 20: + command = bits[0:7] + address = bits[7:12] + extended = bits[12:20] + else: + raise SIRCError('incorrect bits count {}'.format(len(bits))) + break + + 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.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 = bitpack_lsb(extended, 0) + extended_str = '0x{:02X}'.format(extended_num) + 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' + _, _, frame_ss, _ = self.wait_wrap([{0: e}], None) + try: + 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: + pass + except SIRCError as e: + self.putg(frame_ss, self.samplenum, Ann.WARN, [ + 'Error: {}'.format(e), + 'Error', + 'E', + ])