Source code for emod_controller_python_binding.relay8_module
# Example of Python binding to a C library## ctypes module is part of the standard library.# With ctypes, you need to satisfy any compile time dependency on python, and your binding will work on any python that has ctypes, not just the one it was compiled against.# It should be noted that ctypes only works with C functions.# Bindingimportctypestry:c_lib=ctypes.CDLL("/usr/lib/librelay8_module.so")exceptOSError:c_lib=Noneprint("C Library not found.")RELAY8MODULE_NUMBER_OF_RELAYS=8RELAY8MODULE_RELAY1=0x01RELAY8MODULE_RELAY2=0x02RELAY8MODULE_RELAY3=0x04RELAY8MODULE_RELAY4=0x08RELAY8MODULE_RELAY5=0x10RELAY8MODULE_RELAY6=0x20RELAY8MODULE_RELAY7=0x40RELAY8MODULE_RELAY8=0x80RELAY8MODULE_ALL_RELAY=0xFF
[docs]definit(self):""" Initialize module. It is important to emphasize that init() method is mandatory to call it and must be called always after instantiating the module. It can be called several times, each of which the module is initialized again. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_init.argtypes=[ctypes.POINTER(ctypes.c_void_p)]c_lib.Relay8Module_init.restype=ctypes.c_int32returnc_lib.Relay8Module_init(self.obj)
[docs]definit_v(self,variant):""" Initialize module. It is important to emphasize that this method is mandatory to call it. It can be called several times, each of which the module is initialized again. Args: variant: identifies the module in case more than one module of the same type are present. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_init_v.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.c_uint8,]c_lib.Relay8Module_init_v.restype=ctypes.c_int32returnc_lib.Relay8Module_init_v(self.obj,ctypes.c_uint8(variant))
[docs]defconfig_pulse_width(self,relay_mask,width_ms):""" Configure pulse width of a relay or list of relays. When pulse width is 0 a relay stays in the current state, and there is no pulse. Args: relay_mask: relay or list of relays to configure. width_ms: pulse width in ms. Value range 0 to 2^32-1. A 0-value means disable pulse. Default value is 0. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_configPulseWidth.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.c_uint8,ctypes.c_uint32,]c_lib.Relay8Module_configPulseWidth.restype=ctypes.c_int32returnc_lib.Relay8Module_configPulseWidth(self.obj,ctypes.c_uint8(relay_mask),ctypes.c_uint32(width_ms))
[docs]defconfig_all_pulse_width(self,width_ms):""" Configure pulse width of all relays. When pulse width is 0 a relay stays in the current state, and there is no pulse. Args: width_ms: pulse width in ms. Value range 0 to 2^32-1. A 0-value means disable pulse. Default value is 0. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_configAllPulseWidth.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.c_uint32,]c_lib.Relay8Module_configAllPulseWidth.restype=ctypes.c_int32returnc_lib.Relay8Module_configAllPulseWidth(self.obj,ctypes.c_uint32(width_ms))
[docs]defactivate_relay(self,relay_mask):""" Activates a relay or set of relays. Args: relay_mask: relay or list of relays to activate. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_activate.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.c_uint8,]c_lib.Relay8Module_activate.restype=ctypes.c_int32returnc_lib.Relay8Module_activate(self.obj,ctypes.c_uint8(relay_mask))
[docs]defdeactivate_relay(self,relay_mask):""" Deactivates a relay or set of relays. Args: relay_mask: relay or list of relays to deactivate. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_deactivate.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.c_uint8,]c_lib.Relay8Module_deactivate.restype=ctypes.c_int32returnc_lib.Relay8Module_deactivate(self.obj,ctypes.c_uint8(relay_mask))
[docs]defactivate_all_relays(self):""" Activates all module relays. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_activateAll.argtypes=[ctypes.POINTER(ctypes.c_void_p)]c_lib.Relay8Module_activateAll.restype=ctypes.c_int32returnc_lib.Relay8Module_activateAll(self.obj)
[docs]defdeactivate_all_relays(self):""" Deactivates all module relays. Returns: It returns if the initialization was successfull (0) or not (!=0). """c_lib.Relay8Module_deactivateAll.argtypes=[ctypes.POINTER(ctypes.c_void_p)]c_lib.Relay8Module_deactivateAll.restype=ctypes.c_int32returnc_lib.Relay8Module_deactivateAll(self.obj)
[docs]defget_relay_status(self,relay_mask):""" Gets a relay status. Args: relay_mask: relay mask number to get the status. Returns: Every bit represents a relay order status. Status is 1 when is activate and 0 when deactivate. """c_lib.Relay8Module_getRelayStatus.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.c_uint8,ctypes.POINTER(ctypes.c_uint8),]c_lib.Relay8Module_getRelayStatus.restype=ctypes.c_int32status=ctypes.c_uint8(0)ret=c_lib.Relay8Module_getRelayStatus(self.obj,ctypes.c_uint8(relay_mask),ctypes.byref(status))returnstatus.value
[docs]defget_all_relay_status(self):""" Gets all relay status. Returns: Every bit represents a relay order status. Status is 1 when is activate and 0 when deactivate. """c_lib.Relay8Module_getAllRelayStatus.argtypes=[ctypes.POINTER(ctypes.c_void_p),ctypes.POINTER(ctypes.c_uint8),]c_lib.Relay8Module_getAllRelayStatus.restype=ctypes.c_int32status=ctypes.c_uint8(0)ret=c_lib.Relay8Module_getAllRelayStatus(self.obj,ctypes.byref(status))returnstatus.value