..
 #############################################################
 # This file was automatically generated on 2015-05-08.      #
 #                                                           #
 # If you have a bugfix for this file and want to commit it, #
 # please fix the bug in the generator. You can find a link  #
 # to the generators git repository on tinkerforge.com       #
 #############################################################

:orphan:
:breadcrumbs: <a href="../../index.html">Home</a> / <a href="../../index.html#software-python">Software</a> / Python - Industrial Dual Analog In Bricklet

.. |device_identifier_constant| replace:: There is also a :ref:`constant <industrial_dual_analog_in_bricklet_python_constants>` for the device identifier of this Bricklet.

.. _industrial_dual_analog_in_bricklet_python:

Python - Industrial Dual Analog In Bricklet
===========================================

.. note::
 This Bricklet is currently in the prototype stage and the software/hardware
 as well as the documentation is in an incomplete state.


This is the description of the :ref:`Python API bindings <api_bindings_python>` for the :ref:`Industrial Dual Analog In Bricklet <industrial_dual_analog_in_bricklet>`. General information and technical
specifications for the Industrial Dual Analog In Bricklet are summarized in its :ref:`hardware description
<industrial_dual_analog_in_bricklet_description>`.

An :ref:`installation guide <api_bindings_python_install>` for the Python API
bindings is part of their general description.

.. _industrial_dual_analog_in_bricklet_python_examples:


Examples
--------

The example code below is `Public Domain (CC0 1.0)
<http://creativecommons.org/publicdomain/zero/1.0/>`__.

.. _industrial_dual_analog_in_bricklet_python_api:

API
---

Generally, every method of the Python bindings can throw an
``tinkerforge.ip_connection.Error`` exception that has a ``value`` and a
``description`` property. ``value`` can have different values:

* Error.TIMEOUT = -1
* Error.ALREADY_CONNECTED = -7
* Error.NOT_CONNECTED = -8
* Error.INVALID_PARAMETER = -9
* Error.NOT_SUPPORTED = -10
* Error.UNKNOWN_ERROR_CODE = -11

All methods listed below are thread-safe.


The Bricklet has two input channel. Functions that are related
directly to a channel have a ``channel`` parameter to specify one of the two
channels. Valid values for the ``channel`` parameter are 0 and 1.



Basic Functions
^^^^^^^^^^^^^^^


.. py:function:: IndustrialDualAnalogIn(uid, ipcon)

 :param uid: string
 :param ipcon: IPConnection

 Creates an object with the unique device ID ``uid``:

 .. code-block:: python

    industrial_dual_analog_in = IndustrialDualAnalogIn("YOUR_DEVICE_UID", ipcon)

 This object can then be used after the IP Connection is connected
 (see examples :ref:`above <industrial_dual_analog_in_bricklet_python_examples>`).


.. py:function:: IndustrialDualAnalogIn.get_voltage(channel)

 :param channel: int
 :rtype: int

 Returns the voltage for the given channel in mV.
 
 If you want to get the voltage periodically, it is recommended to use the
 callback :py:attr:`CALLBACK_VOLTAGE <IndustrialDualAnalogIn.CALLBACK_VOLTAGE>` and set the period with 
 :py:func:`set_voltage_callback_period() <IndustrialDualAnalogIn.set_voltage_callback_period>`.
 


Advanced Functions
^^^^^^^^^^^^^^^^^^

.. py:function:: IndustrialDualAnalogIn.set_sample_rate(rate)

 :param rate: int
 :rtype: None

 Sets the sample rate. The sample rate can be between 1 sample per second
 and 976 samples per second. Decreasing the sample rate will also decrease the
 noise on the data.
 
 The following constants are available for this function:
 
 * IndustrialDualAnalogIn.SAMPLE_RATE_976_SPS = 0
 * IndustrialDualAnalogIn.SAMPLE_RATE_488_SPS = 1
 * IndustrialDualAnalogIn.SAMPLE_RATE_244_SPS = 2
 * IndustrialDualAnalogIn.SAMPLE_RATE_122_SPS = 3
 * IndustrialDualAnalogIn.SAMPLE_RATE_61_SPS = 4
 * IndustrialDualAnalogIn.SAMPLE_RATE_4_SPS = 5
 * IndustrialDualAnalogIn.SAMPLE_RATE_2_SPS = 6
 * IndustrialDualAnalogIn.SAMPLE_RATE_1_SPS = 7
 
.. py:function:: IndustrialDualAnalogIn.get_sample_rate()

 :rtype: int

 Returns the sample rate as set by :py:func:`set_sample_rate() <IndustrialDualAnalogIn.set_sample_rate>`.
 
 The following constants are available for this function:
 
 * IndustrialDualAnalogIn.SAMPLE_RATE_976_SPS = 0
 * IndustrialDualAnalogIn.SAMPLE_RATE_488_SPS = 1
 * IndustrialDualAnalogIn.SAMPLE_RATE_244_SPS = 2
 * IndustrialDualAnalogIn.SAMPLE_RATE_122_SPS = 3
 * IndustrialDualAnalogIn.SAMPLE_RATE_61_SPS = 4
 * IndustrialDualAnalogIn.SAMPLE_RATE_4_SPS = 5
 * IndustrialDualAnalogIn.SAMPLE_RATE_2_SPS = 6
 * IndustrialDualAnalogIn.SAMPLE_RATE_1_SPS = 7
 
.. py:function:: IndustrialDualAnalogIn.set_calibration(offset, gain)

 :param offset: [int, int]
 :param gain: [int, int]
 :rtype: None

 Sets offset and gain of MCP3911 internal calibration registers.
 
 See MCP3911 datasheet 7.7 and 7.8. The Industrial Dual Analog In Bricklet
 is already factory calibrated by Tinkerforge. It should not be necessary
 for you to use this function
 
.. py:function:: IndustrialDualAnalogIn.get_calibration()

 :rtype: ([int, int], [int, int])

 Returns the calibration as set by :py:func:`set_calibration() <IndustrialDualAnalogIn.set_calibration>`.
 
 The returned namedtuple has the variables ``offset`` and ``gain``.

.. py:function:: IndustrialDualAnalogIn.get_adc_values()

 :rtype: [int, int]

 Returns the ADC values as given by the MCP3911 IC. This function
 is needed for proper calibration, see :py:func:`set_calibration() <IndustrialDualAnalogIn.set_calibration>`.
 
.. py:function:: IndustrialDualAnalogIn.get_api_version()

 :rtype: [int, int, int]

 Returns the version of the API definition (major, minor, revision) implemented
 by this API bindings. This is neither the release version of this API bindings
 nor does it tell you anything about the represented Brick or Bricklet.
 
.. py:function:: IndustrialDualAnalogIn.get_response_expected(function_id)

 :param function_id: int
 :rtype: bool

 Returns the response expected flag for the function specified by the function
 ID parameter. It is *true* if the function is expected to send a response,
 *false* otherwise.
 
 For getter functions this is enabled by default and cannot be disabled,
 because those functions will always send a response. For callback configuration
 functions it is enabled by default too, but can be disabled by
 :py:func:`set_response_expected() <IndustrialDualAnalogIn.set_response_expected>`. For setter functions it is disabled by default
 and can be enabled.
 
 Enabling the response expected flag for a setter function allows to detect
 timeouts and other error conditions calls of this setter as well. The
 device will then send a response for this purpose. If this flag is disabled for
 a setter function then no response is send and errors are silently ignored,
 because they cannot be detected.
 
 See :py:func:`set_response_expected() <IndustrialDualAnalogIn.set_response_expected>`
 for the list of function ID constants available for this function.
 
.. py:function:: IndustrialDualAnalogIn.set_response_expected(function_id, response_expected)

 :param function_id: int
 :param response_expected: bool
 :rtype: None

 Changes the response expected flag of the function specified by the
 function ID parameter. This flag can only be changed for setter (default value:
 *false*) and callback configuration functions (default value: *true*). For
 getter functions it is always enabled and callbacks it is always disabled.
 
 Enabling the response expected flag for a setter function allows to detect
 timeouts and other error conditions calls of this setter as well. The
 device will then send a response for this purpose. If this flag is disabled for
 a setter function then no response is send and errors are silently ignored,
 because they cannot be detected.
 
 The following function ID constants are available for this function:
 
 * IndustrialDualAnalogIn.FUNCTION_SET_VOLTAGE_CALLBACK_PERIOD = 2
 * IndustrialDualAnalogIn.FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD = 4
 * IndustrialDualAnalogIn.FUNCTION_SET_DEBOUNCE_PERIOD = 6
 * IndustrialDualAnalogIn.FUNCTION_SET_SAMPLE_RATE = 8
 * IndustrialDualAnalogIn.FUNCTION_SET_CALIBRATION = 10
 
.. py:function:: IndustrialDualAnalogIn.set_response_expected_all(response_expected)

 :param response_expected: bool
 :rtype: None

 Changes the response expected flag for all setter and callback configuration
 functions of this device at once.
 
.. py:function:: IndustrialDualAnalogIn.get_identity()

 :rtype: (str, str, chr, [int, int, int], [int, int, int], int)

 Returns the UID, the UID where the Bricklet is connected to, 
 the position, the hardware and firmware version as well as the
 device identifier.
 
 The position can be 'a', 'b', 'c' or 'd'.
 
 The device identifier numbers can be found :ref:`here <device_identifier>`.
 |device_identifier_constant|
 
 The returned namedtuple has the variables ``uid``, ``connected_uid``, ``position``, ``hardware_version``, ``firmware_version`` and ``device_identifier``.



Callback Configuration Functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


.. py:function:: IndustrialDualAnalogIn.register_callback(id, callback)

 :param id: int
 :param callback: callable
 :rtype: None

 Registers a callback with ID *id* to the function *callback*. The available
 IDs with corresponding function signatures are listed
 :ref:`below <industrial_dual_analog_in_bricklet_python_callbacks>`.


.. py:function:: IndustrialDualAnalogIn.set_voltage_callback_period(channel, period)

 :param channel: int
 :param period: int
 :rtype: None

 Sets the period in ms with which the :py:attr:`CALLBACK_VOLTAGE <IndustrialDualAnalogIn.CALLBACK_VOLTAGE>` callback is triggered
 periodically for the given channel. A value of 0 turns the callback off.
 
 :py:attr:`CALLBACK_VOLTAGE <IndustrialDualAnalogIn.CALLBACK_VOLTAGE>` is only triggered if the voltage has changed since the
 last triggering.
 
 The default value is 0.
 
.. py:function:: IndustrialDualAnalogIn.get_voltage_callback_period(channel)

 :param channel: int
 :rtype: int

 Returns the period as set by :py:func:`set_voltage_callback_period() <IndustrialDualAnalogIn.set_voltage_callback_period>`.
 
.. py:function:: IndustrialDualAnalogIn.set_voltage_callback_threshold(channel, option, min, max)

 :param channel: int
 :param option: chr
 :param min: int
 :param max: int
 :rtype: None

 Sets the thresholds for the :py:attr:`CALLBACK_VOLTAGE_REACHED <IndustrialDualAnalogIn.CALLBACK_VOLTAGE_REACHED>` callback for the given
 channel.
 
 The following options are possible:
 
 .. csv-table::
  :header: "Option", "Description"
  :widths: 10, 100
 
  "'x'",    "Callback is turned off"
  "'o'",    "Callback is triggered when the voltage is *outside* the min and max values"
  "'i'",    "Callback is triggered when the voltage is *inside* the min and max values"
  "'<'",    "Callback is triggered when the voltage is smaller than the min value (max is ignored)"
  "'>'",    "Callback is triggered when the voltage is greater than the min value (max is ignored)"
 
 The default value is ('x', 0, 0).
 
 The following constants are available for this function:
 
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_OFF = 'x'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_OUTSIDE = 'o'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_INSIDE = 'i'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_SMALLER = '<'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_GREATER = '>'
 
.. py:function:: IndustrialDualAnalogIn.get_voltage_callback_threshold(channel)

 :param channel: int
 :rtype: (chr, int, int)

 Returns the threshold as set by :py:func:`set_voltage_callback_threshold() <IndustrialDualAnalogIn.set_voltage_callback_threshold>`.
 
 The following constants are available for this function:
 
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_OFF = 'x'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_OUTSIDE = 'o'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_INSIDE = 'i'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_SMALLER = '<'
 * IndustrialDualAnalogIn.THRESHOLD_OPTION_GREATER = '>'
 
 The returned namedtuple has the variables ``option``, ``min`` and ``max``.

.. py:function:: IndustrialDualAnalogIn.set_debounce_period(debounce)

 :param debounce: int
 :rtype: None

 Sets the period in ms with which the threshold callback
 
 * :py:attr:`CALLBACK_VOLTAGE_REACHED <IndustrialDualAnalogIn.CALLBACK_VOLTAGE_REACHED>`
 
 is triggered, if the threshold
 
 * :py:func:`set_voltage_callback_threshold() <IndustrialDualAnalogIn.set_voltage_callback_threshold>`
 
 keeps being reached.
 
 The default value is 100.
 
.. py:function:: IndustrialDualAnalogIn.get_debounce_period()

 :rtype: int

 Returns the debounce period as set by :py:func:`set_debounce_period() <IndustrialDualAnalogIn.set_debounce_period>`.
 


.. _industrial_dual_analog_in_bricklet_python_callbacks:

Callbacks
^^^^^^^^^

Callbacks can be registered to receive
time critical or recurring data from the device. The registration is done
with the :py:func:`register_callback() <IndustrialDualAnalogIn.register_callback>` function of
the device object. The first parameter is the callback ID and the second
parameter the callback function:

.. code-block:: python

    def my_callback(param):
        print(param)

    industrial_dual_analog_in.register_callback(IndustrialDualAnalogIn.CALLBACK_EXAMPLE, my_callback)

The available constants with inherent number and type of parameters are
described below.

.. note::
 Using callbacks for recurring events is *always* preferred
 compared to using getters. It will use less USB bandwidth and the latency
 will be a lot better, since there is no round trip time.

.. py:attribute:: IndustrialDualAnalogIn.CALLBACK_VOLTAGE

 :param channel: int
 :param voltage: int


 This callback is triggered periodically with the period that is set by
 :py:func:`set_voltage_callback_period() <IndustrialDualAnalogIn.set_voltage_callback_period>`. The parameter is the voltage of the
 channel.
 
 :py:attr:`CALLBACK_VOLTAGE <IndustrialDualAnalogIn.CALLBACK_VOLTAGE>` is only triggered if the voltage has changed since the
 last triggering.
 
.. py:attribute:: IndustrialDualAnalogIn.CALLBACK_VOLTAGE_REACHED

 :param channel: int
 :param voltage: int


 This callback is triggered when the threshold as set by
 :py:func:`set_voltage_callback_threshold() <IndustrialDualAnalogIn.set_voltage_callback_threshold>` is reached.
 The parameter is the voltage of the channel.
 
 If the threshold keeps being reached, the callback is triggered periodically
 with the period as set by :py:func:`set_debounce_period() <IndustrialDualAnalogIn.set_debounce_period>`.
 


.. _industrial_dual_analog_in_bricklet_python_constants:

Constants
^^^^^^^^^

.. py:attribute:: IndustrialDualAnalogIn.DEVICE_IDENTIFIER

 This constant is used to identify a Industrial Dual Analog In Bricklet.

 The :py:func:`get_identity() <IndustrialDualAnalogIn.get_identity>` function and the
 :py:attr:`CALLBACK_ENUMERATE <IPConnection.CALLBACK_ENUMERATE>`
 callback of the IP Connection have a ``device_identifier`` parameter to specify
 the Brick's or Bricklet's type.

