]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ir_irmp/IrmpPythonWrap.py
irmp: introduce Python language binding for shared library
[libsigrokdecode.git] / decoders / ir_irmp / IrmpPythonWrap.py
1
2
3
4 from ctypes import *
5 import platform
6
7       
8
9 class IrmpWrap:
10     class IrmpData(Structure):
11          _fields_ = [  ("protocol"      , c_uint32 ),
12                        ("protocolName"  , c_char_p ),
13                        ("address"       , c_uint32 ),
14                        ("command"       , c_uint32 ),
15                        ("flags"         , c_uint32 ),
16                        ("startSample"   , c_uint32 ),
17                        ("endSample"     , c_uint32 ),
18          ]
19     
20     def __init__(self):
21         libname = "irmp.dll"
22         # get the right filename
23
24         if platform.uname()[0] == "Linux":
25             name = "irmp.so"    
26     
27         self.__irmpDll = cdll.LoadLibrary("irmp.dll")
28         self.__irmpDll.IRMP_GetSampleRate.restype = c_int32
29         self.__irmpDll.IRMP_GetSampleRate.argtypes = []
30         
31         
32         self.__irmpDll.IRMP_GetProtocolName.restype = c_char_p
33         self.__irmpDll.IRMP_GetProtocolName.argtypes = [c_uint32]
34         
35         self.__irmpDll.IRMP_Reset.restype = None
36         self.__irmpDll.IRMP_Reset.argtypes = []
37         
38         self.__irmpDll.IRMP_AddSample.restype = c_uint32
39         self.__irmpDll.IRMP_AddSample.argtypes = [c_uint8]
40         
41         self.__irmpDll.IRMP_GetData.restype = c_uint32
42         self.__irmpDll.IRMP_GetData.argtypes = [POINTER(IrmpWrap.IrmpData)]
43         
44         self.__irmpDll.IRMP_Detect.restype = IrmpWrap.IrmpData
45         self.__irmpDll.IRMP_Detect.argtypes = [ c_char_p, c_uint32]
46         
47         self.__data = IrmpWrap.IrmpData()
48         self.__startSample = c_uint32(0)
49         self.__endSample   = c_uint32(0)
50
51         return
52
53     def GetProtocollName(self, pn):
54         return self.__irmpDll.IRMP_GetProtocollName(pn)
55     
56     def GetSampleRate(self):
57         return self.__irmpDll.IRMP_GetSampleRate()
58     
59     def Reset(self):
60         self.__irmpDll.IRMP_Reset()
61         
62     def AddSample(self, level):
63         
64         if self.__irmpDll.IRMP_AddSample(c_uint8( 1 if (level!=0) else 0)):
65             self.__irmpDll.IRMP_GetData( byref(self.__data))
66             return True
67         else:
68             return False
69         
70     def GetData(self):
71         return { 'data'  : { 
72                              'protocol'     : self.__data.protocol,
73                              'protocolName' : self.__data.protocolName.decode('UTF-8'),
74                              'address'       : self.__data.address,
75                              'command'       : self.__data.command,
76                              'repeat'        : (self.__data.flags != 0)
77                            }, 
78                  'start' : self.__data.startSample,
79                  'end'   : self.__data.endSample
80                }
81