2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Rene Staffen
5 ## Copyright (C) 2020-2021 Gerhard Sittig <gerhard.sittig@gmx.net>
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/>.
22 Python binding for the IRMP library.
30 Library instance for an infrared protocol detector.
33 __usable_instance
= None
35 class ResultData(ctypes
.Structure
):
37 ( 'protocol', ctypes
.c_uint32
, ),
38 ( 'protocol_name', ctypes
.c_char_p
, ),
39 ( 'address', ctypes
.c_uint32
, ),
40 ( 'command', ctypes
.c_uint32
, ),
41 ( 'flags', ctypes
.c_uint32
, ),
42 ( 'start_sample', ctypes
.c_uint32
, ),
43 ( 'end_sample', ctypes
.c_uint32
, ),
46 FLAG_REPETITION
= 1 << 0
49 def _library_filename(self
):
51 Determine the library filename depending on the platform.
54 if platform
.uname()[0] == 'Linux':
56 if platform
.uname()[0] == 'Darwin':
57 return 'libirmp.dylib'
60 def _library_setup_api(self
):
62 Lookup the C library's API routines. Declare their prototypes.
65 self
._lib
.irmp_get_sample_rate
.restype
= ctypes
.c_uint32
66 self
._lib
.irmp_get_sample_rate
.argtypes
= []
68 self
._lib
.irmp_instance_alloc
.restype
= ctypes
.c_void_p
69 self
._lib
.irmp_instance_alloc
.argtypes
= []
71 self
._lib
.irmp_instance_free
.restype
= None
72 self
._lib
.irmp_instance_free
.argtypes
= [ ctypes
.c_void_p
, ]
74 self
._lib
.irmp_instance_id
.restype
= ctypes
.c_size_t
75 self
._lib
.irmp_instance_id
.argtypes
= [ ctypes
.c_void_p
, ]
77 self
._lib
.irmp_instance_lock
.restype
= ctypes
.c_int
78 self
._lib
.irmp_instance_lock
.argtypes
= [ ctypes
.c_void_p
, ctypes
.c_int
, ]
80 self
._lib
.irmp_instance_unlock
.restype
= None
81 self
._lib
.irmp_instance_unlock
.argtypes
= [ ctypes
.c_void_p
, ]
83 self
._lib
.irmp_reset_state
.restype
= None
84 self
._lib
.irmp_reset_state
.argtypes
= []
86 self
._lib
.irmp_add_one_sample
.restype
= ctypes
.c_int
87 self
._lib
.irmp_add_one_sample
.argtypes
= [ ctypes
.c_int
, ]
90 self
._lib
.irmp_detect_buffer
.restype
= self
.ResultData
91 self
._lib
.irmp_detect_buffer
.argtypes
= [ ctypes
.POINTER(ctypes
.c_uint8
), ctypes
.c_size_t
, ]
93 self
._lib
.irmp_get_result_data
.restype
= ctypes
.c_int
94 self
._lib
.irmp_get_result_data
.argtypes
= [ ctypes
.POINTER(self
.ResultData
), ]
96 self
._lib
.irmp_get_protocol_name
.restype
= ctypes
.c_char_p
97 self
._lib
.irmp_get_protocol_name
.argtypes
= [ ctypes
.c_uint32
, ]
99 # Create a result buffer that's local to the library instance.
100 self
._data
= self
.ResultData()
107 Create a library instance.
110 filename
= self
._library
_filename
()
111 self
._lib
= ctypes
.cdll
.LoadLibrary(filename
)
112 self
._library
_setup
_api
()
116 Release a disposed library instance.
120 self
._lib
.irmp_instance_free(self
._inst
)
125 Enter a context (lock management).
128 if self
._inst
is None:
129 self
._inst
= self
._lib
.irmp_instance_alloc()
130 self
._lib
.irmp_instance_lock(self
._inst
, 1)
133 def __exit__(self
, extype
, exvalue
, trace
):
135 Leave a context (lock management).
138 self
._lib
.irmp_instance_unlock(self
._inst
)
142 return self
._lib
.irmp_instance_id(self
._inst
)
144 def get_sample_rate(self
):
145 return self
._lib
.irmp_get_sample_rate()
147 def reset_state(self
):
148 self
._lib
.irmp_reset_state()
150 def add_one_sample(self
, level
):
151 if not self
._lib
.irmp_add_one_sample(int(level
)):
153 self
._lib
.irmp_get_result_data(ctypes
.byref(self
._data
))
156 def get_result_data(self
):
160 'proto_nr': self
._data
.protocol
,
161 'proto_name': self
._data
.protocol_name
.decode('UTF-8', 'ignore'),
162 'address': self
._data
.address
,
163 'command': self
._data
.command
,
164 'repeat': bool(self
._data
.flags
& self
.FLAG_REPETITION
),
165 'release': bool(self
._data
.flags
& self
.FLAG_RELEASE
),
166 'start': self
._data
.start_sample
,
167 'end': self
._data
.end_sample
,