irmp: introduce Python language binding for shared library
[libsigrokdecode/gsi.git] / decoders / ir_irmp / IrmpPythonWrap.py
blobee250c4d173ef36d128f0eaf49b45fe95c175bc9
4 from ctypes import *
5 import platform
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 ),
20 def __init__(self):
21 libname = "irmp.dll"
22 # get the right filename
24 if platform.uname()[0] == "Linux":
25 name = "irmp.so"
27 self.__irmpDll = cdll.LoadLibrary("irmp.dll")
28 self.__irmpDll.IRMP_GetSampleRate.restype = c_int32
29 self.__irmpDll.IRMP_GetSampleRate.argtypes = []
32 self.__irmpDll.IRMP_GetProtocolName.restype = c_char_p
33 self.__irmpDll.IRMP_GetProtocolName.argtypes = [c_uint32]
35 self.__irmpDll.IRMP_Reset.restype = None
36 self.__irmpDll.IRMP_Reset.argtypes = []
38 self.__irmpDll.IRMP_AddSample.restype = c_uint32
39 self.__irmpDll.IRMP_AddSample.argtypes = [c_uint8]
41 self.__irmpDll.IRMP_GetData.restype = c_uint32
42 self.__irmpDll.IRMP_GetData.argtypes = [POINTER(IrmpWrap.IrmpData)]
44 self.__irmpDll.IRMP_Detect.restype = IrmpWrap.IrmpData
45 self.__irmpDll.IRMP_Detect.argtypes = [ c_char_p, c_uint32]
47 self.__data = IrmpWrap.IrmpData()
48 self.__startSample = c_uint32(0)
49 self.__endSample = c_uint32(0)
51 return
53 def GetProtocollName(self, pn):
54 return self.__irmpDll.IRMP_GetProtocollName(pn)
56 def GetSampleRate(self):
57 return self.__irmpDll.IRMP_GetSampleRate()
59 def Reset(self):
60 self.__irmpDll.IRMP_Reset()
62 def AddSample(self, level):
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
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