From: Uwe Hermann Date: Fri, 7 Mar 2014 13:55:54 +0000 (+0100) Subject: Rename 'ir_nec6122' PD to 'ir_nec', minor fixes and simplifications. X-Git-Tag: libsigrokdecode-0.3.0~58 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=00962e763e610d8535dd9df1ac97377b3fb727a7;p=libsigrokdecode.git Rename 'ir_nec6122' PD to 'ir_nec', minor fixes and simplifications. This IR protocol is commonly referred to as "the NEC protocol", and can be generated by various means (not only via the NEC µPD6121/µPD6122 ICs). Drop some unneeded variables and fix/simplify the code a bit. --- diff --git a/decoders/ir_nec/__init__.py b/decoders/ir_nec/__init__.py new file mode 100644 index 0000000..84bf428 --- /dev/null +++ b/decoders/ir_nec/__init__.py @@ -0,0 +1,26 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2014 Gump Yang +## +## 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, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +''' +NEC is a pulse-distance based infrared remote control protocol. +''' + +from .pd import * + diff --git a/decoders/ir_nec/pd.py b/decoders/ir_nec/pd.py new file mode 100644 index 0000000..542139d --- /dev/null +++ b/decoders/ir_nec/pd.py @@ -0,0 +1,160 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2014 Gump Yang +## +## 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, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +import sigrokdecode as srd + +class Decoder(srd.Decoder): + api_version = 1 + id = 'ir_nec' + name = 'IR NEC' + longname = 'IR NEC' + desc = 'NEC infrared remote control protocol.' + license = 'gplv2+' + inputs = ['logic'] + outputs = ['ir_nec'] + probes = [ + {'id': 'ir', 'name': 'IR', 'desc': 'Data line'}, + ] + optional_probes = [] + options = { + 'cnt_lc': ['Leader code time (µs)', 13500], + 'cnt_rc': ['Repeat code time (µs)', 11250], + 'cnt_rc_end': ['Repeat code end time (µs)', 562], + 'cnt_accuracy': ['Accuracy range (µs)', 100], + 'cnt_dazero': ['Data 0 time (µs)', 1125], + 'cnt_daone': ['Data 1 time (µs)', 2250], + 'polarity': ['Polarity', 'active-low'], + } + annotations = [ + ['bit', 'Bit'], + ['lc', 'Leader code'], + ['info', 'Info'], + ['error', 'Error'], + ] + annotation_rows = ( + ('bits', 'Bits', (0,)), + ('fields', 'Fields', (1, 2, 3)), + ) + + def putx(self, data): + self.put(self.ss_start, self.samplenum, self.out_ann, data) + + def putb(self, data): + self.put(self.ss_bit, self.samplenum, self.out_ann, data) + + def __init__(self, **kwargs): + self.ss_bit = 0 + self.state = 'IDLE' + self.data = 0 + self.count = 0 + self.ss_start = 0 + self.act_polar = 0 + + def start(self): + # self.out_python = self.register(srd.OUTPUT_PYTHON) + self.out_ann = self.register(srd.OUTPUT_ANN) + self.act_polar = 1 if self.options['polarity'] == 'active-low' else 0 + self.old_ir = self.act_polar + + def metadata(self, key, value): + if key == srd.SRD_CONF_SAMPLERATE: + self.samplerate = value + samplerate = float(self.samplerate) + + x = float(self.options['cnt_accuracy']) / 1000000.0 + self.margin = int(samplerate * x) - 1 + x = float(self.options['cnt_lc']) / 1000000.0 + self.lc = int(samplerate * x) - 1 + x = float(self.options['cnt_rc']) / 1000000.0 + self.rc = int(samplerate * x) - 1 + x = float(self.options['cnt_rc_end']) / 1000000.0 + self.rc_end = int(samplerate * x) - 1 + x = float(self.options['cnt_dazero']) / 1000000.0 + self.dazero = int(samplerate * x) - 1 + x = float(self.options['cnt_daone']) / 1000000.0 + self.daone = int(samplerate * x) - 1 + x = float(10000) / 1000000.0 + self.end = int(samplerate * x) - 1 + + def handle_bits(self, tick): + ret = 0xff + if tick in range(self.dazero - self.margin, self.dazero + self.margin): + ret = 0 + elif tick in range(self.daone - self.margin, self.daone + self.margin): + ret = 1 + + if ret < 2: + self.putb([0, ['%d' % ret]]) + self.data = self.data * 2 + ret + self.count = self.count + 1 + + self.ss_bit = self.samplenum + return ret + + def data_judge(self, name): + buf = int((self.data & 0xff00) / 0x100) + nbuf = int(self.data & 0xff) + ret = buf & nbuf + if ret == 0: + self.putx([2, ['%s: 0x%02x' % (name, buf)]]) + else: + self.putx([3, ['%s Error: 0x%04x' % (name, self.data)]]) + + self.data = self.count = 0 + self.ss_bit = self.ss_start = self.samplenum + return ret + + def decode(self, ss, es, data): + if self.samplerate is None: + raise Exception("Cannot decode without samplerate.") + for (self.samplenum, pins) in data: + self.ir = pins[0] + + # Wait for any edge (rising or falling). + if self.old_ir == self.ir: + continue + + if self.old_ir == self.act_polar: + b = self.samplenum - self.ss_bit + # State machine. + if self.state == 'IDLE': + if b in range(self.lc - self.margin, self.lc + self.margin): + self.putx([1, ['Leader code', 'Leader', 'LC', 'L']]) + self.data = self.count = 0 + self.state = 'ADDRESS' + elif b in range(self.rc - self.margin, self.rc + self.margin): + self.putx([1, ['Repeat code', 'Repeat', 'RC', 'R']]) + self.data = self.count = 0 + self.ss_bit = self.ss_start = self.samplenum + elif self.state == 'ADDRESS': + self.handle_bits(b) + if self.count > 15: + if self.data_judge(self.state) == 0: + self.state = 'COMMAND' + else: + self.state = 'IDLE' + elif self.state == 'COMMAND': + self.handle_bits(b) + if self.count > 15: + self.data_judge(self.state) + self.state = 'IDLE' + + self.old_ir = self.ir + diff --git a/decoders/ir_nec6122/__init__.py b/decoders/ir_nec6122/__init__.py deleted file mode 100644 index 972d5a9..0000000 --- a/decoders/ir_nec6122/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -## -## This file is part of the libsigrokdecode project. -## -## Copyright (C) 2014 Gump Yang -## -## 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, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -## - -''' -NEC 6121/6122 is a biphase/manchester based infrared remote control protocol. -''' - -from .pd import * - diff --git a/decoders/ir_nec6122/pd.py b/decoders/ir_nec6122/pd.py deleted file mode 100644 index ba1cdf9..0000000 --- a/decoders/ir_nec6122/pd.py +++ /dev/null @@ -1,172 +0,0 @@ -## -## This file is part of the libsigrokdecode project. -## -## Copyright (C) 2014 Gump Yang -## -## 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, write to the Free Software -## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -## - -import sigrokdecode as srd - -class Decoder(srd.Decoder): - api_version = 1 - id = 'ir_nec6122' - name = 'IR NEC 6122' - longname = '1-Wire Infrared remote controller NEC 6122' - desc = 'Unidirectional, asynchronous serial bus.' - license = 'gplv2+' - inputs = ['logic'] - outputs = ['ir_nec6122'] - probes = [ - {'id': 'ir', 'name': 'IR', 'desc': 'Data line'}, - ] - optional_probes = [] - options = { - 'level': ['Tirgger Level L/H', 0], - 'cnt_peroid': ['Peroid time (us)', 13500], - 'cnt_repeat': ['Repeat time (us)', 11250], - 'cnt_repeat_end': ['Repeat end time (us)', 562], - 'cnt_accuracy': ['Accuracy range (us)', 100], - 'cnt_dazero': ['Data 0 time (us)', 1125], - 'cnt_daone': ['Data 1 time (us)', 2250], - 'polarity': ['Polarity', 'active-low'], - } - annotations = [ - ['bit', 'Bit'], - ['preoid', 'Preoid'], - ['info', 'Info'], - ['error', 'Error'], - ] - annotation_rows = ( - ('fields', 'Fields', (1, 2, 3, 4, 5, 6)), - ('bits', 'Bits', (0,)), - ) - - def putx(self, data): - self.put(self.ss_edge, self.samplenum, self.out_ann, data) - - def putx(self, data): - self.put(self.ss_start, self.samplenum, self.out_ann, data) - - def putb(self, data): - self.put(self.ss_bit, self.samplenum, self.out_ann, data) - - def __init__(self, **kwargs): - self.olddata = None - self.ss_edge = 0 - self.ss_bit = 0 - self.first_transition = True - self.bitwidth = None - self.state = 'IDLE' - self.data = 0; - self.count = 0; - self.ss_start = 0 - self.act_polar = 0 - - def start(self): - # self.out_python = self.register(srd.OUTPUT_PYTHON) - self.out_ann = self.register(srd.OUTPUT_ANN) - self.act_polar = 1 if self.options['polarity'] == 'active-low' else 0 - self.old_ir = self.act_polar - - def metadata(self, key, value): - if key == srd.SRD_CONF_SAMPLERATE: - self.samplerate = value - samplerate = float(self.samplerate) - - x = float(self.options['cnt_accuracy']) / 1000000.0 - self.margin = int(samplerate * x) - 1 - x = float(self.options['cnt_peroid']) / 1000000.0 - self.preoid = int(samplerate * x) - 1 - x = float(self.options['cnt_repeat']) / 1000000.0 - self.repeat = int(samplerate * x) - 1 - x = float(self.options['cnt_repeat_end']) / 1000000.0 - self.repeat_end = int(samplerate * x) - 1 - x = float(self.options['cnt_dazero']) / 1000000.0 - self.dazero = int(samplerate * x) - 1 - x = float(self.options['cnt_daone']) / 1000000.0 - self.daone = int(samplerate * x) - 1 - x = float(10000) / 1000000.0 - self.end = int(samplerate * x) - 1 - - def handle_bits(self, tick): - ret = 0xff - if tick in range(self.dazero - self.margin, - self.dazero + self.margin): - ret = 0 - elif tick in range(self.daone - self.margin, - self.daone + self.margin): - ret = 1 - - if ret < 2: - self.putb([0, ['%d' % ret]]) - self.data = self.data * 2 + ret - self.count = self.count + 1 - - self.ss_bit = self.samplenum - return ret; - - def data_judge(self, name): - buf = int((self.data & 0xff00) / 0x100) - nbuf = int(self.data & 0xff) - ret = buf & nbuf - if ret == 0: - self.putx([2, ['%s: 0x%02x' % (name, buf)]]) - else: - self.putx([3, ['%s Error: 0x%04x' % (name, self.data)]]) - - self.data = self.count = 0 - self.ss_bit = self.ss_start = self.samplenum - return ret - - def decode(self, ss, es, data): - if self.samplerate is None: - raise Exception("Cannot decode without samplerate.") - for (self.samplenum, pins) in data: - self.ir = pins[0] - - # Wait for any edge (rising or falling). - if self.old_ir == self.ir: - continue - - if self.old_ir == self.act_polar: - b = self.samplenum - self.ss_bit - # State machine. - if self.state == 'IDLE': - if b in range(self.preoid - self.margin, - self.preoid + self.margin): - self.putx([1, ['Preoid', 'Pre', 'P']]) - self.data = self.count = 0 - self.state = 'ADDRESS' - elif b in range(self.repeat - self.margin, - self.repeat + self.margin): - self.putx([1, ['Repeat', 'Rep', 'R']]) - self.data = self.count = 0 - self.ss_bit = self.ss_start = self.samplenum - elif self.state == 'ADDRESS': - self.handle_bits(b) - if self.count > 15: - if self.data_judge(self.state) == 0: - self.state = 'CODE' - else: - self.state = 'IDLE' - elif self.state == 'CODE': - self.handle_bits(b) - if self.count > 15: - self.data_judge(self.state) - self.state = 'IDLE' - - self.old_ir = self.ir -