Source code for emod_controller_python_binding.dinput5relay2_module

# Python binding to a C library for the 5di2pr module
#
# 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/libdi5r2_module.so")
except OSError:
    c_lib = None
    print("C Library not found.")
    
DI5PR2MODULE_NUMBER_OF_DI_INPUTS = 5
DI5PR2MODULE_NUMBER_OF_RELAYS = 2
DI5PR2MODULE_INPUT01 = 0x0001
DI5PR2MODULE_INPUT02 = 0x0002
DI5PR2MODULE_INPUT03 = 0x0004
DI5PR2MODULE_INPUT04 = 0x0008
DI5PR2MODULE_INPUT05 = 0x0010
DI5PR2MODULE_ALL_INPUT = 0x001F

DI5PR2MODULE_RELAY1 = 0x01
DI5PR2MODULE_RELAY2 = 0x02
DI5PR2MODULE_ALL_RELAY = 0x03

DInput5Relay2ModuleCallback_Type = ctypes.CFUNCTYPE(
    None,
    ctypes.POINTER(ctypes.c_uint8),
    ctypes.c_uint16,
    ctypes.c_uint8,
    ctypes.POINTER(ctypes.c_void_p),
)


[docs]class DInput5Relay2Module: def __init__(self): c_lib.DInput5Relay2Module_create.argtypes = None c_lib.DInput5Relay2Module_create.restype = ctypes.POINTER(ctypes.c_void_p) self.obj = c_lib.DInput5Relay2Module_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.DInput5Relay2Module_init.argtypes = [ctypes.POINTER(ctypes.c_void_p)] c_lib.DInput5Relay2Module_init.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_init(self.obj)
[docs] def init_with_callback(self, callback): """ 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: callback: function that will be called when a configured event occurs. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_init.argtypes = [ ctypes.POINTER(ctypes.c_void_p), DInput5Relay2ModuleCallback_Type, ctypes.POINTER(ctypes.c_void_p), ] c_lib.DInput5Relay2Module_init.restype = ctypes.c_int ret = c_lib.DInput5Relay2Module_init(self.obj, callback, self.obj) return ret
[docs] def init_v(self, callback, 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: callback: function that will be called when a configured event occurs. 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.DInput5Relay2Module_init_v.argtypes = [ ctypes.POINTER(ctypes.c_void_p), DInput5Relay2ModuleCallback_Type, ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.DInput5Relay2Module_init_v.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_init_v( self.obj, callback, self.obj, ctypes.c_uint8(variant) )
[docs] def get_digital_input_status(self, input_mask): """ Gets a digital input status. Args: input_mask: digital input number to get the status, as enumerated above in the constants section. Returns: status: returns last received input number status (0:deactivated, 1: activated). new_data: returns if new data has arrived since the last function call. If status has not been received yet, NO_NEW_DATA_READ is returned. Parameter is initialized inside the class. """ c_lib.DInput5Relay2Module_getStatus.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_bool), ] c_lib.DInput5Relay2Module_getStatus.restype = ctypes.c_int32 status = ctypes.c_uint8(0) new_data = ctypes.c_bool(False) ret = c_lib.DInput5Relay2Module_getStatus( self.obj, ctypes.c_uint16(input_mask), ctypes.byref(status), ctypes.byref(new_data), ) return [status.value, new_data.value]
[docs] def get_digital_input_all_status(self): """ Gets all digital input status. Returns: status: returns last received input number status (0:deactivated, 1: activated) for each digital input. new_data: returns if new data has arrived since the last function call. If a digital input status has not been received yet, NO_NEW_DATA_READ is returned in that input. Parameter is initialized inside the class. """ c_lib.DInput5Relay2Module_getAllStatus.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_bool), ] c_lib.DInput5Relay2Module_getAllStatus.restype = ctypes.c_int32 Uint8Array = ctypes.c_uint8 * DI5PR2MODULE_NUMBER_OF_DI_INPUTS status = Uint8Array() new_data = ctypes.c_bool(False) ret = c_lib.DInput5Relay2Module_getAllStatus( self.obj, status, ctypes.byref(new_data) ) return [status, new_data.value]
[docs] def get_pulse_count(self, input_mask): """ Gets number of pulses occurred in an input after a reset. Args: input_mask: digital input number to get the status, as enumerated above in the constants section. Returns: It returns the number of pulses. """ c_lib.DInput5Relay2Module_getPulseCount.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.POINTER(ctypes.c_uint32), ] c_lib.DInput5Relay2Module_getPulseCount.restype = ctypes.c_int32 count = ctypes.c_uint32(0) ret = c_lib.DInput5Relay2Module_getPulseCount( self.obj, ctypes.c_uint16(input_mask), ctypes.byref(count) ) return count.value
[docs] def get_all_pulse_count(self): """ Gets number of pulses occurred in all inputs. Returns: It returns the number of pulses for each digital input. """ c_lib.DInput5Relay2Module_getAllPulseCount.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint32), ] c_lib.DInput5Relay2Module_getAllPulseCount.restype = ctypes.c_int32 Uint32Array = ctypes.c_uint32 * DI5PR2MODULE_NUMBER_OF_DI_INPUTS counts = Uint32Array() ret = c_lib.DInput5Relay2Module_getAllPulseCount(self.obj, counts) return counts
[docs] def reset_pulse_count(self, input_mask): """ Resets the number of pulses in an input. Args: input_mask: digital input number to get the status, as enumerated above in the constants section. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_resetPulseCount.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ] c_lib.DInput5Relay2Module_resetPulseCount.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_resetPulseCount( self.obj, ctypes.c_uint16(input_mask) )
[docs] def reset_all_pulse_count(self): """ Resets the number of pulses in all inputs.. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_resetAllPulseCount.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.DInput5Relay2Module_resetAllPulseCount.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_resetAllPulseCount(self.obj)
[docs] def get_pulse_width(self, input_mask): """ Gets last pulse width occurred in an input.. Args: input_mask: digital input number to get the status, as enumerated above in the constants section. Returns: It returns pulse width (in ms). """ c_lib.DInput5Relay2Module_getPulseWidth.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.POINTER(ctypes.c_uint32), ] c_lib.DInput5Relay2Module_getPulseWidth.restype = ctypes.c_int32 width = ctypes.c_uint32(0) ret = c_lib.DInput5Relay2Module_getPulseWidth( self.obj, ctypes.c_uint16(input_mask), ctypes.byref(width) ) return width.value
[docs] def get_all_pulse_width(self): """ Gets last pulse width occurred in all inputs.. Returns: It returns an an array of counts representing the last pulse width occurred in each input. """ c_lib.DInput5Relay2Module_getAllPulseWidth.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint32), ] c_lib.DInput5Relay2Module_getAllPulseWidth.restype = ctypes.c_int32 Uint32Array = ctypes.c_uint32 * DI5PR2MODULE_NUMBER_OF_DI_INPUTS widths = Uint32Array() ret = c_lib.DInput5Relay2Module_getAllPulseWidth(self.obj, widths) return widths
[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.DInput5Relay2Module_configPulseWidth.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ctypes.c_uint32, ] c_lib.DInput5Relay2Module_configPulseWidth.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_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.DInput5Relay2Module_configAllPulseWidth.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ] c_lib.DInput5Relay2Module_configAllPulseWidth.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_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.DInput5Relay2Module_activate.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.DInput5Relay2Module_activate.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_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.DInput5Relay2Module_deactivate.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.DInput5Relay2Module_deactivate.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_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.DInput5Relay2Module_activateAll.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.DInput5Relay2Module_activateAll.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_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.DInput5Relay2Module_deactivateAll.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.DInput5Relay2Module_deactivateAll.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_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.DInput5Relay2Module_getRelayStatus.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ctypes.POINTER(ctypes.c_uint8), ] c_lib.DInput5Relay2Module_getRelayStatus.restype = ctypes.c_int32 status = ctypes.c_uint8(0) ret = c_lib.DInput5Relay2Module_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.DInput5Relay2Module_getAllRelayStatus.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint8), ] c_lib.DInput5Relay2Module_getAllRelayStatus.restype = ctypes.c_int32 status = ctypes.c_uint8(0) ret = c_lib.DInput5Relay2Module_getAllRelayStatus( self.obj, ctypes.byref(status) ) return status.value
[docs] def set_pulse_filter_time(self, input_mask, ms_time): """ Sets the pulse filter time. All pulses with a shorter time will be discarded. Args: input_mask: digital input number to get the status, as enumerated above in the constants section. ms_time: 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.DInput5Relay2Module_setPulseFilterTime.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.c_uint32, ] c_lib.DInput5Relay2Module_setPulseFilterTime.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_setPulseFilterTime( self.obj, ctypes.c_uint16(input_mask), ctypes.c_uint32(ms_time) )
[docs] def set_all_pulse_filter_time(self, ms_time): """ Sets the pulse filter time for all inputs. Args: ms_time: 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.DInput5Relay2Module_setPulseFilterTime.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ] c_lib.DInput5Relay2Module_setPulseFilterTime.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_setAllPulseFilterTime( self.obj, ctypes.c_uint32(ms_time) )
[docs] def get_pulse_filter_time(self, input_mask): """ Gets current pulse filter time. Args: input_mask: digital input number to get the status, as enumerated above in the constants section. Returns: It returns pulse width (in ms). """ c_lib.DInput5Relay2Module_getPulseFilterTime.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.POINTER(ctypes.c_uint32), ] c_lib.DInput5Relay2Module_getPulseFilterTime.restype = ctypes.c_int32 ms_time = ctypes.c_uint32(0) ret = c_lib.DInput5Relay2Module_getPulseFilterTime( self.obj, ctypes.c_uint16(input_mask), ctypes.byref(ms_time) ) return ms_time.value
[docs] def get_all_pulse_filter_time(self): """ Gets current pulse filter time for all inputs. Returns: It returns pulse width (in ms) for each input. """ c_lib.DInput5Relay2Module_getAllPulseFilterTime.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint32), ] c_lib.DInput5Relay2Module_getAllPulseFilterTime.restype = ctypes.c_int32 Uint32Array = ctypes.c_uint32 * DI5PR2MODULE_NUMBER_OF_DI_INPUTS ms_times = Uint32Array() ret = c_lib.DInput5Relay2Module_getAllPulseFilterTime(self.obj, ms_times) return ms_times
[docs] def config_event_at_time_interval(self, time_interval, event_mask): """ Configures module for calling init callback_func at specified period. Args: time_interval: period in milliseconds. Minimum value is 500ms. event_mask: bit mask representing inputs that must trigger the event. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_configEventAtTimeInterval.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.c_uint32, ] c_lib.DInput5Relay2Module_configEventAtTimeInterval.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_configEventAtTimeInterval( self.obj, ctypes.c_uint16(time_interval), ctypes.c_uint32(event_mask) )
[docs] def switch_mode_digital_input(self, input_number, mode): """ Usually, this function only needs to be called at the beginning in the configuration stage. Args: input_mask: digital input number to be configured. mode: desired mode for the input; PULSE_COUNTER or PULSE_WIDTH as enumerated above in the constants section. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_switchToMode.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.c_uint32, ] c_lib.DInput5Relay2Module_switchToMode.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_switchToMode( self.obj, ctypes.c_uint16(input_number), ctypes.c_uint32(mode) )
[docs] def config_event_on_new_data(self): """ Configures module for calling init callback_func when new data on input occurs. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_configEventOnNewData.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.DInput5Relay2Module_configEventOnNewData.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_configEventOnNewData(self.obj)
[docs] def config_event_on_value_change(self, event_mask, threshold): """ Configures module for calling init callback_func when input changes its value by a specified amount. Args: threshold: amount of samples that triggers the event. event_mask: bit mask representing inputs that must trigger the event. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_configEventOnValueChange.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ctypes.c_uint32, ] c_lib.DInput5Relay2Module_configEventOnValueChange.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_configEventOnValueChange( self.obj, ctypes.c_uint32(event_mask), ctypes.c_uint32(threshold) )
[docs] def reset_event_configuration(self): """ Resets all previously configured events. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.DInput5Relay2Module_resetEventConfig.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.DInput5Relay2Module_resetEventConfig.restype = ctypes.c_int32 return c_lib.DInput5Relay2Module_resetEventConfig(self.obj)