From: Gerhard Sittig Date: Sun, 26 Dec 2021 06:38:05 +0000 (+0200) Subject: ir_irmp: sigrok PD, make use of IRMP decoder core locking X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8c4477692114cfb1a823d847b70c8ea96caa7301;p=libsigrokdecode.git ir_irmp: sigrok PD, make use of IRMP decoder core locking Rephrase how the external IRMP library gets loaded, to provide better diagnostics to users. All decoder instances are equal after the recent introduction of locking support. Move the "reset state" call for the IRMP decoder core to the .decode() method's main loop, where the context manager holds the instance lock. This allows "parallel" execution of multiple IRMP decoders in the same sigrok application, assuming that the context manager scope will be left at some point in time. This fixes bug #1581 when applications communicate EOF to decoders. Move some Python object members to local variables. They exclusively are used within the .decode() method. Update the copyright for the non-trivial changes. --- diff --git a/decoders/ir_irmp/pd.py b/decoders/ir_irmp/pd.py index 979c1e0..b8df819 100644 --- a/decoders/ir_irmp/pd.py +++ b/decoders/ir_irmp/pd.py @@ -3,6 +3,7 @@ ## ## Copyright (C) 2014 Gump Yang ## Copyright (C) 2019 Rene Staffen +## Copyright (C) 2020-2021 Gerhard Sittig ## ## 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 @@ -97,7 +98,7 @@ class Decoder(srd.Decoder): self.reset() def reset(self): - self.want_reset = True + pass def start(self): self.out_ann = self.register(srd.OUTPUT_ANN) @@ -113,25 +114,26 @@ class Decoder(srd.Decoder): except Exception as e: txt = e.args[0] raise LibraryError(txt) - if self.irmp: - self.lib_rate = self.irmp.get_sample_rate() - if not self.irmp or not self.lib_rate: - raise LibraryError('Cannot access IRMP library. One instance limit exceeded?') + if not self.irmp: + raise LibraryError('Cannot access IRMP library.') if not self.samplerate: raise SamplerateError('Cannot decode without samplerate.') - if self.samplerate % self.lib_rate: - raise SamplerateError('Capture samplerate must be multiple of library samplerate ({})'.format(self.lib_rate)) - self.rate_factor = int(self.samplerate / self.lib_rate) - if self.want_reset: - self.irmp.reset_state() - self.want_reset = False + lib_rate = self.irmp.get_sample_rate() + if not lib_rate: + raise LibraryError('Cannot determine IRMP library\'s samplerate.') + if self.samplerate % lib_rate: + raise SamplerateError('Capture samplerate must be multiple of library samplerate ({})'.format(lib_rate)) + + self.rate_factor = int(self.samplerate / lib_rate) + active = 0 if self.options['polarity'] == 'active-low' else 1 - self.active = 0 if self.options['polarity'] == 'active-low' else 1 ir, = self.wait() - while True: - if self.active == 1: - ir = 1 - ir - if self.irmp.add_one_sample(ir): - data = self.irmp.get_result_data() - self.putframe(data) - ir, = self.wait([{'skip': self.rate_factor}]) + with self.irmp: + self.irmp.reset_state() + while True: + if active == 1: + ir = 1 - ir + if self.irmp.add_one_sample(ir): + data = self.irmp.get_result_data() + self.putframe(data) + ir, = self.wait([{'skip': self.rate_factor}])