Source code for emod_controller_python_binding.ainput7relay2_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/libai7r2_module.so")
except OSError:
    c_lib = None
    print("C Library not found.")

AI7R2MODULE_NUMBER_OF_AI_INPUTS = 7
AI7R2MODULE_NUMBER_OF_RELAYS = 2
AI7R2MODULE_INPUT01 = 0x0001
AI7R2MODULE_INPUT02 = 0x0002
AI7R2MODULE_INPUT03 = 0x0004
AI7R2MODULE_INPUT04 = 0x0008
AI7R2MODULE_INPUT05 = 0x0010
AI7R2MODULE_INPUT06 = 0x0020
AI7R2MODULE_INPUT07 = 0x0040
AI7R2MODULE_ALL_INPUT = 0x007F

AI7R2MODULE_RELAY1 = 0x01
AI7R2MODULE_RELAY2 = 0x02
AI7R2MODULE_ALL_RELAY = 0x03

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

[docs]class AInput7Relay2Module: def __init__(self): c_lib.AInput7Relay2Module_create.argtypes = None c_lib.AInput7Relay2Module_create.restype = ctypes.POINTER(ctypes.c_void_p) self.obj = c_lib.AInput7Relay2Module_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.AInput7Relay2Module_init.argtypes = [ctypes.POINTER(ctypes.c_void_p)] c_lib.AInput7Relay2Module_init.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_init.argtypes = [ ctypes.POINTER(ctypes.c_void_p), AInput7Relay2ModuleCallback_Type, ctypes.POINTER(ctypes.c_void_p), ] c_lib.AInput7Relay2Module_init.restype = ctypes.c_int ret = c_lib.AInput7Relay2Module_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.AInput7Relay2Module_init_v.argtypes = [ ctypes.POINTER(ctypes.c_void_p), AInput7Relay2ModuleCallback_Type, ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.AInput7Relay2Module_init_v.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_init_v( self.obj, callback, self.obj, ctypes.c_uint8(variant) )
[docs] def config_sample_rate(self, ms_period): """ Configure sample rate at which you want to check the analog input data. Args: ms_period: time in milliseconds, which represents the sampling time, where the ADC will sample the seven channels. Minimum value is 1 ms. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.AInput7Relay2Module_configSampleRate.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configSampleRate.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_configSampleRate( self.obj, ctypes.c_uint32(ms_period) )
[docs] def config_analog_input(self, input_mask): """ Configure an input or set of inputs to select them for acquiring current (4 to 20 mA) or voltage (0 to 10 V). Args: input_mask: input or list of analog inputs, as enumerated above in the constants section, to configure them for acquiring current. The rest of inputs will be configured for acquiring voltage. Returns: It returns if the initialization was successfull (0) or not (!=0). """ c_lib.AInput7Relay2Module_configInput.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ] c_lib.AInput7Relay2Module_configInput.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_configInput( self.obj, ctypes.c_uint16(input_mask) )
[docs] def get_analog_input_config(self): """ Gets configured inputs mask. Each input indicates if it is configured to acquire voltage (0) or current (1). Returns: It returns the current input mask configuration (V/A) for each input as a mask. """ c_lib.AInput7Relay2Module_getInputConfig.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint16), ] c_lib.AInput7Relay2Module_getInputConfig.restype = ctypes.c_int32 config = ctypes.c_uint16(0) ret = c_lib.AInput7Relay2Module_getInputConfig(self.obj, ctypes.byref(config)) return config.value
[docs] def get_analog_input(self, input_num): """ Gets last measured value of a specified analog input.. Args: input_num: analog input number to get the data, as enumerated above in the constants section. Returns: It returns last measured input data. If data has not been received yet, NO_NEW_DATA_READ is returned. Parameter is initialized inside the class. When input was configured as current, value is shown in mA; otherwise it's represented in V. When an input is configured in current mode and **sampled_data is 0, it means that the current loop is broken. """ c_lib.AInput7Relay2Module_getAnalogInput.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.POINTER(ctypes.c_float), ] c_lib.AInput7Relay2Module_getAnalogInput.restype = ctypes.c_int32 data = ctypes.c_float(0) ret = c_lib.AInput7Relay2Module_getAnalogInput( self.obj, ctypes.c_uint16(input_num), ctypes.byref(data) ) return data.value
[docs] def get_all_analog_inputs(self): """ Gets last measured values for all analog inputs. Returns: It returns last measured input number data for each analog input. If an analog input data has not been measured yet, NO_NEW_DATA_READ is returned in that input. Parameter is initialized inside the class. When input was configured as current, value is shown in mA; otherwise it's represented in V. When an input is configured in current mode and **sampled_data is 0, it means that the current loop is broken. """ c_lib.AInput7Relay2Module_getAllAnalogInput.argtypes = [ ctypes.POINTER(ctypes.c_void_p), (ctypes.c_float * AI7R2MODULE_NUMBER_OF_AI_INPUTS), ] c_lib.AInput7Relay2Module_getAllAnalogInput.restype = ctypes.c_int32 FloatArray = ctypes.c_float * AI7R2MODULE_NUMBER_OF_AI_INPUTS data = FloatArray() ret = c_lib.AInput7Relay2Module_getAllAnalogInput(self.obj, data) return data
[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.AInput7Relay2Module_configPulseWidth.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configPulseWidth.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_configAllPulseWidth.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configAllPulseWidth.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_activate.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.AInput7Relay2Module_activate.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_deactivate.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ] c_lib.AInput7Relay2Module_deactivate.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_activateAll.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.AInput7Relay2Module_activateAll.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_deactivateAll.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.AInput7Relay2Module_deactivateAll.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_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.AInput7Relay2Module_getRelayStatus.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint8, ctypes.POINTER(ctypes.c_uint8), ] c_lib.AInput7Relay2Module_getRelayStatus.restype = ctypes.c_int32 status = ctypes.c_uint8(0) ret = c_lib.AInput7Relay2Module_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.AInput7Relay2Module_getAllRelayStatus.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint8), ] c_lib.AInput7Relay2Module_getAllRelayStatus.restype = ctypes.c_int32 status = ctypes.c_uint8(0) ret = c_lib.AInput7Relay2Module_getAllRelayStatus( self.obj, ctypes.byref(status) ) return status.value
[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.AInput7Relay2Module_configEventAtTimeInterval.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint16, ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configEventAtTimeInterval.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_configEventAtTimeInterval( self.obj, ctypes.c_uint16(time_interval), ctypes.c_uint32(event_mask) )
[docs] def config_event_on_value_change(self, threshold, event_mask): """ 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.AInput7Relay2Module_configEventOnValueChange.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configEventOnValueChange.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_configEventOnValueChange( self.obj, ctypes.c_uint32(threshold), ctypes.c_uint32(event_mask) )
[docs] def config_event_within_range(self, low_limit, high_limit, event_mask): """ Configures module for calling init callback_func when input value enters the specified range. Args: low_limit: low limit of the range in samples. high_limit: high limit of the range in samples. 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.AInput7Relay2Module_configEventWithinRange.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configEventWithinRange.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_configEventWithinRange( self.obj, ctypes.c_uint32(low_limit), ctypes.c_uint32(high_limit), ctypes.c_uint32(event_mask), )
[docs] def config_event_out_of_range(self, low_limit, high_limit, event_mask): """ Configures module for calling init callback_func when input value gets out of the specified range. Args: low_limit: low limit of the range in samples. high_limit: high limit of the range in samples. 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.AInput7Relay2Module_configEventOutOfRange.argtypes = [ ctypes.POINTER(ctypes.c_void_p), ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ] c_lib.AInput7Relay2Module_configEventOutOfRange.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_configEventOutOfRange( self.obj, ctypes.c_uint32(low_limit), ctypes.c_uint32(high_limit), ctypes.c_uint32(event_mask), )
[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.AInput7Relay2Module_configEventOutOfRange.argtypes = [ ctypes.POINTER(ctypes.c_void_p) ] c_lib.AInput7Relay2Module_configEventOutOfRange.restype = ctypes.c_int32 return c_lib.AInput7Relay2Module_resetEventConfiguration(self.obj)