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.

# Binding
import ctypes

try:
    c_lib = ctypes.CDLL("/usr/lib/librelay8_module.so")
except OSError:
    c_lib = None
    print("C Library not found.")


RELAY8MODULE_NUMBER_OF_RELAYS = 8
RELAY8MODULE_RELAY1 = 0x01
RELAY8MODULE_RELAY2 = 0x02
RELAY8MODULE_RELAY3 = 0x04
RELAY8MODULE_RELAY4 = 0x08
RELAY8MODULE_RELAY5 = 0x10
RELAY8MODULE_RELAY6 = 0x20
RELAY8MODULE_RELAY7 = 0x40
RELAY8MODULE_RELAY8 = 0x80
RELAY8MODULE_ALL_RELAY = 0xFF


[docs]class Relay8Module: def __init__(self): c_lib.Relay8Module_create.argtypes = None c_lib.Relay8Module_create.restype = ctypes.POINTER(ctypes.c_void_p) self.obj = c_lib.Relay8Module_create()
[docs] def init(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_int32 return c_lib.Relay8Module_init(self.obj)
[docs] def init_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_int32 return c_lib.Relay8Module_init_v(self.obj, ctypes.c_uint8(variant))
[docs] def config_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_int32 return c_lib.Relay8Module_configPulseWidth( self.obj, ctypes.c_uint8(relay_mask), ctypes.c_uint32(width_ms) )
[docs] def config_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_int32 return c_lib.Relay8Module_configAllPulseWidth( self.obj, ctypes.c_uint32(width_ms) )
[docs] def activate_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_int32 return c_lib.Relay8Module_activate(self.obj, ctypes.c_uint8(relay_mask))
[docs] def deactivate_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_int32 return c_lib.Relay8Module_deactivate(self.obj, ctypes.c_uint8(relay_mask))
[docs] def activate_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_int32 return c_lib.Relay8Module_activateAll(self.obj)
[docs] def deactivate_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_int32 return c_lib.Relay8Module_deactivateAll(self.obj)
[docs] def get_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_int32 status = ctypes.c_uint8(0) ret = c_lib.Relay8Module_getRelayStatus( self.obj, ctypes.c_uint8(relay_mask), ctypes.byref(status) ) return status.value
[docs] def get_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_int32 status = ctypes.c_uint8(0) ret = c_lib.Relay8Module_getAllRelayStatus(self.obj, ctypes.byref(status)) return status.value