2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Gump Yang <gump.yang@gmail.com>
5 ## Copyright (C) 2019 Rene Staffen
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
21 from . import irmp_library
22 import sigrokdecode as srd
24 class SamplerateError(Exception):
27 class LibraryError(Exception):
30 class Decoder(srd.Decoder):
35 desc = 'IRMP infrared remote control multi protocol.'
41 {'id': 'ir', 'name': 'IR', 'desc': 'Data line'},
44 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low',
45 'values': ('active-low', 'active-high')},
51 ('packets', 'IR Packets', (0,)),
54 def putframe(self, data):
55 '''Emit annotation for an IR frame.'''
57 # Cache result data fields in local variables. Get the ss/es
58 # timestamps, scaled to sample numbers.
60 name = data['proto_name']
61 addr = data['address']
63 repeat = data['repeat']
64 release = data['release']
65 ss = data['start'] * self.rate_factor
66 es = data['end'] * self.rate_factor
68 # Prepare display texts for several zoom levels.
69 # Implementor's note: Keep list lengths for flags aligned during
70 # maintenance. Make sure there are as many flags text variants
71 # as are referenced by annotation text variants. Differing list
72 # lengths or dynamic refs will severely complicate the logic.
73 rep_txts = ['repeat', 'rep', 'r']
74 rel_txts = ['release', 'rel', 'R']
75 flag_txts = [None,] * len(rep_txts)
76 for zoom in range(len(flag_txts)):
79 flag_txts[zoom].append(rep_txts[zoom])
81 flag_txts[zoom].append(rel_txts[zoom])
82 flag_txts = [' '.join(t) or '-' for t in flag_txts]
83 flg = flag_txts # Short name for .format() references.
85 'Protocol: {name} ({nr}), Address 0x{addr:04x}, Command: 0x{cmd:04x}, Flags: {flg[0]}'.format(**locals()),
86 'P: {name} ({nr}), Addr: 0x{addr:x}, Cmd: 0x{cmd:x}, Flg: {flg[1]}'.format(**locals()),
87 'P: {nr} A: 0x{addr:x} C: 0x{cmd:x} F: {flg[1]}'.format(**locals()),
88 'C:{cmd:x} A:{addr:x} {flg[2]}'.format(**locals()),
89 'C:{cmd:x}'.format(**locals()),
92 # Emit the annotation from details which were constructed above.
93 self.put(ss, es, self.out_ann, [0, txts])
100 self.want_reset = True
103 self.out_ann = self.register(srd.OUTPUT_ANN)
105 def metadata(self, key, value):
106 if key == srd.SRD_CONF_SAMPLERATE:
107 self.samplerate = value
112 self.irmp = irmp_library.IrmpLibrary()
113 except Exception as e:
115 raise LibraryError(txt)
117 self.lib_rate = self.irmp.get_sample_rate()
118 if not self.irmp or not self.lib_rate:
119 raise LibraryError('Cannot access IRMP library. One instance limit exceeded?')
120 if not self.samplerate:
121 raise SamplerateError('Cannot decode without samplerate.')
122 if self.samplerate % self.lib_rate:
123 raise SamplerateError('Capture samplerate must be multiple of library samplerate ({})'.format(self.lib_rate))
124 self.rate_factor = int(self.samplerate / self.lib_rate)
126 self.irmp.reset_state()
127 self.want_reset = False
129 self.active = 0 if self.options['polarity'] == 'active-low' else 1
134 if self.irmp.add_one_sample(ir):
135 data = self.irmp.get_result_data()
137 ir, = self.wait([{'skip': self.rate_factor}])