]>
Commit | Line | Data |
---|---|---|
31b646e2 GS |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2019 Rene Staffen | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | ''' | |
21 | Python binding for the IRMP library. | |
22 | ''' | |
23 | ||
24 | import ctypes | |
25 | import platform | |
26 | ||
27 | class IrmpLibrary: | |
28 | ''' | |
29 | Library instance for an infrared protocol detector. | |
30 | ''' | |
31 | ||
32 | class ResultData(ctypes.Structure): | |
33 | _fields_ = [ | |
34 | ( 'protocol', ctypes.c_uint32, ), | |
35 | ( 'protocol_name', ctypes.c_char_p, ), | |
36 | ( 'address', ctypes.c_uint32, ), | |
37 | ( 'command', ctypes.c_uint32, ), | |
38 | ( 'flags', ctypes.c_uint32, ), | |
39 | ( 'start_sample', ctypes.c_uint32, ), | |
40 | ( 'end_sample', ctypes.c_uint32, ), | |
41 | ] | |
42 | ||
43 | FLAG_REPETITION = 1 << 0 | |
44 | ||
45 | def _library_filename(self): | |
46 | ''' | |
47 | Determine the library filename depending on the platform. | |
48 | ''' | |
49 | ||
50 | if platform.uname()[0] == 'Linux': | |
51 | return 'libirmp.so' | |
02993c7a GS |
52 | if platform.uname()[0] == 'Darwin': |
53 | return 'libirmp.dylib' | |
31b646e2 GS |
54 | return 'irmp.dll' |
55 | ||
56 | def __init__(self): | |
57 | ''' | |
58 | Create a library instance. | |
59 | ''' | |
60 | ||
61 | # Load the library. Lookup routines, declare their prototypes. | |
62 | filename = self._library_filename() | |
63 | self._lib = ctypes.cdll.LoadLibrary(filename) | |
64 | ||
65 | self._lib.irmp_get_sample_rate.restype = ctypes.c_uint32 | |
66 | self._lib.irmp_get_sample_rate.argtypes = [] | |
67 | ||
68 | self._lib.irmp_reset_state.restype = None | |
69 | self._lib.irmp_reset_state.argtypes = [] | |
70 | ||
71 | self._lib.irmp_add_one_sample.restype = ctypes.c_int | |
72 | self._lib.irmp_add_one_sample.argtypes = [ ctypes.c_int, ] | |
73 | ||
74 | if False: | |
75 | self._lib.irmp_detect_buffer.restype = self.ResultData | |
76 | self._lib.irmp_detect_buffer.argtypes = [ ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t, ] | |
77 | ||
78 | self._lib.irmp_get_result_data.restype = ctypes.c_int | |
79 | self._lib.irmp_get_result_data.argtypes = [ ctypes.POINTER(self.ResultData), ] | |
80 | ||
81 | self._lib.irmp_get_protocol_name.restype = ctypes.c_char_p | |
82 | self._lib.irmp_get_protocol_name.argtypes = [ ctypes.c_uint32, ] | |
83 | ||
84 | # Create a result buffer that's local to the library instance. | |
85 | self._data = self.ResultData() | |
86 | ||
87 | def get_sample_rate(self): | |
88 | return self._lib.irmp_get_sample_rate() | |
89 | ||
90 | def reset_state(self): | |
91 | self._lib.irmp_reset_state() | |
92 | ||
93 | def add_one_sample(self, level): | |
94 | if not self._lib.irmp_add_one_sample(int(level)): | |
95 | return False | |
96 | self._lib.irmp_get_result_data(ctypes.byref(self._data)) | |
97 | return True | |
98 | ||
99 | def get_result_data(self): | |
100 | data = { | |
101 | 'proto_nr': self._data.protocol, | |
102 | 'proto_name': self._data.protocol_name.decode('UTF-8', 'ignore'), | |
103 | 'address': self._data.address, | |
104 | 'command': self._data.command, | |
105 | 'repeat': bool(self._data.flags & self.FLAG_REPETITION), | |
106 | 'start': self._data.start_sample, | |
107 | 'end': self._data.end_sample, | |
108 | } | |
109 | return data |