]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/ir_irmp/pd.py
avr_isp: Add more parts
[libsigrokdecode.git] / decoders / ir_irmp / pd.py
index cb69fd010722910ef4e131616ee13e4afa09fb06..b8df8190301cba34b34d567ce17231615e0ba3b9 100644 (file)
@@ -3,6 +3,7 @@
 ##
 ## Copyright (C) 2014 Gump Yang <gump.yang@gmail.com>
 ## Copyright (C) 2019 Rene Staffen
+## Copyright (C) 2020-2021 Gerhard Sittig <gerhard.sittig@gmx.net>
 ##
 ## 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
@@ -24,6 +25,9 @@ import sigrokdecode as srd
 class SamplerateError(Exception):
     pass
 
+class LibraryError(Exception):
+    pass
+
 class Decoder(srd.Decoder):
     api_version = 3
     id = 'ir_irmp'
@@ -90,12 +94,11 @@ class Decoder(srd.Decoder):
         self.put(ss, es, self.out_ann, [0, txts])
 
     def __init__(self):
-        self.irmp = irmp_library.IrmpLibrary()
-        self.lib_rate = self.irmp.get_sample_rate()
+        self.irmp = None
         self.reset()
 
     def reset(self):
-        self.irmp.reset_state()
+        pass
 
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
@@ -105,18 +108,32 @@ class Decoder(srd.Decoder):
             self.samplerate = value
 
     def decode(self):
+        if not self.irmp:
+            try:
+                self.irmp = irmp_library.IrmpLibrary()
+            except Exception as e:
+                txt = e.args[0]
+                raise LibraryError(txt)
+        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)
+        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}])