..
 #############################################################
 # 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 - Heart Rate Bricklet

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

.. _heart_rate_bricklet_python:

Python - Heart Rate 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:`Heart Rate Bricklet <heart_rate_bricklet>`. General information and technical
specifications for the Heart Rate Bricklet are summarized in its :ref:`hardware description
<heart_rate_bricklet_description>`.

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

.. _heart_rate_bricklet_python_examples:


Examples
--------

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

Simple
^^^^^^

`Download (example_simple.py) <https://github.com/Tinkerforge/heart-rate-bricklet/raw/master/software/examples/python/example_simple.py>`__

.. literalinclude:: HeartRate_Bricklet_Python_example_simple.py
 :language: python
 :linenos:
 :tab-width: 4

Callback
^^^^^^^^

`Download (example_callback.py) <https://github.com/Tinkerforge/heart-rate-bricklet/raw/master/software/examples/python/example_callback.py>`__

.. literalinclude:: HeartRate_Bricklet_Python_example_callback.py
 :language: python
 :linenos:
 :tab-width: 4

Threshold
^^^^^^^^^

`Download (example_threshold.py) <https://github.com/Tinkerforge/heart-rate-bricklet/raw/master/software/examples/python/example_threshold.py>`__

.. literalinclude:: HeartRate_Bricklet_Python_example_threshold.py
 :language: python
 :linenos:
 :tab-width: 4

.. _heart_rate_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.




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


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

 :param uid: string
 :param ipcon: IPConnection

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

 .. code-block:: python

    heart_rate = HeartRate("YOUR_DEVICE_UID", ipcon)

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


.. py:function:: HeartRate.get_heart_rate()

 :rtype: int

 Returns the current heart rate measured.
 
 If you want to get the heart rate periodically, it is recommended 
 to use the callback :py:attr:`CALLBACK_HEART_RATE <HeartRate.CALLBACK_HEART_RATE>` and set the period with 
 :py:func:`set_heart_rate_callback_period() <HeartRate.set_heart_rate_callback_period>`.
 


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

.. py:function:: HeartRate.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:: HeartRate.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() <HeartRate.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() <HeartRate.set_response_expected>`
 for the list of function ID constants available for this function.
 
.. py:function:: HeartRate.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:
 
 * HeartRate.FUNCTION_SET_HEART_RATE_CALLBACK_PERIOD = 2
 * HeartRate.FUNCTION_SET_HEART_RATE_CALLBACK_THRESHOLD = 4
 * HeartRate.FUNCTION_SET_DEBOUNCE_PERIOD = 6
 * HeartRate.FUNCTION_ENABLE_BEAT_STATE_CHANGED_CALLBACK = 11
 * HeartRate.FUNCTION_DISABLE_BEAT_STATE_CHANGED_CALLBACK = 12
 
.. py:function:: HeartRate.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:: HeartRate.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:: HeartRate.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 <heart_rate_bricklet_python_callbacks>`.


.. py:function:: HeartRate.set_heart_rate_callback_period(period)

 :param period: int
 :rtype: None

 Sets the period in ms with which the :py:attr:`CALLBACK_HEART_RATE <HeartRate.CALLBACK_HEART_RATE>` callback is triggered
 periodically. A value of 0 turns the callback off.
 
 :py:attr:`CALLBACK_HEART_RATE <HeartRate.CALLBACK_HEART_RATE>` is only triggered if the heart rate has changed since the
 last triggering.
 
 The default value is 0.
 
.. py:function:: HeartRate.get_heart_rate_callback_period()

 :rtype: int

 Returns the period as set by :py:func:`set_heart_rate_callback_period() <HeartRate.set_heart_rate_callback_period>`.
 
.. py:function:: HeartRate.set_heart_rate_callback_threshold(option, min, max)

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

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

 :rtype: (chr, int, int)

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

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

 :param debounce: int
 :rtype: None

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

 :rtype: int

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

 :rtype: None

 Enables the :py:attr:`CALLBACK_BEAT_STATE_CHANGED <HeartRate.CALLBACK_BEAT_STATE_CHANGED>` callback.
 
.. py:function:: HeartRate.disable_beat_state_changed_callback()

 :rtype: None

 Disables the :py:attr:`CALLBACK_BEAT_STATE_CHANGED <HeartRate.CALLBACK_BEAT_STATE_CHANGED>` callback.
 
.. py:function:: HeartRate.is_beat_state_changed_callback_enabled()

 :rtype: bool

 Returns *true* if the :py:attr:`CALLBACK_BEAT_STATE_CHANGED <HeartRate.CALLBACK_BEAT_STATE_CHANGED>` callback is enabled.
 


.. _heart_rate_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() <HeartRate.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)

    heart_rate.register_callback(HeartRate.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:: HeartRate.CALLBACK_HEART_RATE

 :param heart_rate: int


 This callback is triggered periodically with the period that is set by
 :py:func:`set_heart_rate_callback_period() <HeartRate.set_heart_rate_callback_period>`. The parameter is the heart rate
 of the sensor.
 
 :py:attr:`CALLBACK_HEART_RATE <HeartRate.CALLBACK_HEART_RATE>` is only triggered if the heart rate has changed since the
 last triggering.
 
.. py:attribute:: HeartRate.CALLBACK_HEART_RATE_REACHED

 :param heart_rate: int


 This callback is triggered when the threshold as set by
 :py:func:`set_heart_rate_callback_threshold() <HeartRate.set_heart_rate_callback_threshold>` is reached.
 The parameter is the heart rate of the sensor.
 
 If the threshold keeps being reached, the callback is triggered periodically
 with the period as set by :py:func:`set_debounce_period() <HeartRate.set_debounce_period>`.
 
.. py:attribute:: HeartRate.CALLBACK_BEAT_STATE_CHANGED

 :param state: int


 This callback provides the current heart beat state.It is called
 every time a heart beat is detected. The state can either be
 
 * 0 = Falling: The falling edge of a detected heart beat.
 * 1 = Rising: The rising edge of a detected heart beat.
 
 The following constants are available for this function:
 
 * HeartRate.BEAT_STATE_FALLING = 0
 * HeartRate.BEAT_STATE_RISING = 1
 


.. _heart_rate_bricklet_python_constants:

Constants
^^^^^^^^^

.. py:attribute:: HeartRate.DEVICE_IDENTIFIER

 This constant is used to identify a Heart Rate Bricklet.

 The :py:func:`get_identity() <HeartRate.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.

