..
 #############################################################
 # This file was automatically generated on 2015-05-19.      #
 #                                                           #
 # 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-matlab">Software</a> / MATLAB/Octave - CO2 Bricklet

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

.. _co2_bricklet_matlab:

MATLAB/Octave - CO2 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:`MATLAB/Octave API bindings <api_bindings_matlab>` for the :ref:`CO2 Bricklet <co2_bricklet>`. General information and technical
specifications for the CO2 Bricklet are summarized in its :ref:`hardware description
<co2_bricklet_description>`.

An :ref:`installation guide <api_bindings_matlab_install>` for the MATLAB/Octave API
bindings is part of their general description.

.. _co2_bricklet_matlab_examples:


Examples
--------

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

.. _co2_bricklet_matlab_api:

API
---

Generally, every method of the MATLAB bindings that returns a value can
throw a ``TimeoutException``. This exception gets thrown if the
device did not respond. If a cable based connection is used, it is
unlikely that this exception gets thrown (assuming nobody unplugs the
device). However, if a wireless connection is used, timeouts will occur
if the distance to the device gets too big.

Beside the ``TimeoutException`` there is also a ``NotConnectedException`` that
is thrown if a method needs to communicate with the device while the
IP Connection is not connected.

Since the MATLAB bindings are based on Java and Java does not support multiple
return values and return by reference is not possible for primitive types, we
use small classes that only consist of member variables. The member variables
of the returned objects are described in the corresponding method descriptions.

The package for all Brick/Bricklet bindings and the IP Connection is
``com.tinkerforge.*``

All methods listed below are thread-safe.




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


.. matlab:function:: class BrickletCO2(String uid, IPConnection ipcon)

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

 In MATLAB:

 .. code-block:: matlab

  import com.tinkerforge.BrickletCO2;

  co2 = BrickletCO2('YOUR_DEVICE_UID', ipcon);

 In Octave:

 .. code-block:: octave_fixed

  co2 = java_new("com.tinkerforge.BrickletCO2", "YOUR_DEVICE_UID", ipcon);

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


.. matlab:function:: public int BrickletCO2::getCO2Concentration()

 Returns the measured CO2 concentration. The value is in ppb and
 between TBD and TBD.
 
 If you want to get the CO2 concentration periodically, it is recommended to use the
 callback :matlab:member:`CO2ConcentrationCallback <BrickletCO2.CO2ConcentrationCallback>` and set the period with
 :matlab:func:`setCO2ConcentrationCallbackPeriod() <BrickletCO2::setCO2ConcentrationCallbackPeriod>`.
 


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

.. matlab:function:: public short[] BrickletCO2::getAPIVersion()

 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.
 
.. matlab:function:: public boolean BrickletCO2::getResponseExpected(short functionId)

 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
 :matlab:func:`setResponseExpected() <BrickletCO2::setResponseExpected>`. 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 :matlab:func:`setResponseExpected() <BrickletCO2::setResponseExpected>`
 for the list of function ID constants available for this function.
 
.. matlab:function:: public void BrickletCO2::setResponseExpected(short functionId, boolean responseExpected)

 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:
 
 * BrickletCO2.FUNCTION_SET_CO2_CONCENTRATION_CALLBACK_PERIOD = 2
 * BrickletCO2.FUNCTION_SET_CO2_CONCENTRATION_CALLBACK_THRESHOLD = 4
 * BrickletCO2.FUNCTION_SET_DEBOUNCE_PERIOD = 6
 
.. matlab:function:: public void BrickletCO2::setResponseExpectedAll(boolean responseExpected)

 Changes the response expected flag for all setter and callback configuration
 functions of this device at once.
 
.. matlab:function:: public BrickletCO2.Identity BrickletCO2::getIdentity()

 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 object has the public member variables ``String uid``, ``String connectedUid``, ``char position``, ``short[] hardwareVersion``, ``short[] firmwareVersion`` and ``int deviceIdentifier``.



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

.. matlab:function:: public void BrickletCO2::setCO2ConcentrationCallbackPeriod(long period)

 Sets the period in ms with which the :matlab:member:`CO2ConcentrationCallback <BrickletCO2.CO2ConcentrationCallback>` callback is triggered
 periodically. A value of 0 turns the callback off.
 
 :matlab:member:`CO2ConcentrationCallback <BrickletCO2.CO2ConcentrationCallback>` is only triggered if the co2_concentration has changed since the
 last triggering.
 
 The default value is 0.
 
.. matlab:function:: public long BrickletCO2::getCO2ConcentrationCallbackPeriod()

 Returns the period as set by :matlab:func:`setCO2ConcentrationCallbackPeriod() <BrickletCO2::setCO2ConcentrationCallbackPeriod>`.
 
.. matlab:function:: public void BrickletCO2::setCO2ConcentrationCallbackThreshold(char option, short min, short max)

 Sets the thresholds for the :matlab:member:`CO2ConcentrationReachedCallback <BrickletCO2.CO2ConcentrationReachedCallback>` 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 co2_concentration is *outside* the min and max values"
  "'i'",    "Callback is triggered when the co2_concentration is *inside* the min and max values"
  "'<'",    "Callback is triggered when the co2_concentration is smaller than the min value (max is ignored)"
  "'>'",    "Callback is triggered when the co2_concentration is greater than the min value (max is ignored)"
 
 The default value is ('x', 0, 0).
 
 The following constants are available for this function:
 
 * BrickletCO2.THRESHOLD_OPTION_OFF = 'x'
 * BrickletCO2.THRESHOLD_OPTION_OUTSIDE = 'o'
 * BrickletCO2.THRESHOLD_OPTION_INSIDE = 'i'
 * BrickletCO2.THRESHOLD_OPTION_SMALLER = '<'
 * BrickletCO2.THRESHOLD_OPTION_GREATER = '>'
 
.. matlab:function:: public BrickletCO2.CO2ConcentrationCallbackThreshold BrickletCO2::getCO2ConcentrationCallbackThreshold()

 Returns the threshold as set by :matlab:func:`setCO2ConcentrationCallbackThreshold() <BrickletCO2::setCO2ConcentrationCallbackThreshold>`.
 
 The following constants are available for this function:
 
 * BrickletCO2.THRESHOLD_OPTION_OFF = 'x'
 * BrickletCO2.THRESHOLD_OPTION_OUTSIDE = 'o'
 * BrickletCO2.THRESHOLD_OPTION_INSIDE = 'i'
 * BrickletCO2.THRESHOLD_OPTION_SMALLER = '<'
 * BrickletCO2.THRESHOLD_OPTION_GREATER = '>'
 
 The returned object has the public member variables ``char option``, ``short min`` and ``short max``.

.. matlab:function:: public void BrickletCO2::setDebouncePeriod(long debounce)

 Sets the period in ms with which the threshold callbacks
 
 * :matlab:member:`CO2ConcentrationReachedCallback <BrickletCO2.CO2ConcentrationReachedCallback>`,
 
 are triggered, if the thresholds
 
 * :matlab:func:`setCO2ConcentrationCallbackThreshold() <BrickletCO2::setCO2ConcentrationCallbackThreshold>`,
 
 keep being reached.
 
 The default value is 100.
 
.. matlab:function:: public long BrickletCO2::getDebouncePeriod()

 Returns the debounce period as set by :matlab:func:`setDebouncePeriod() <BrickletCO2::setDebouncePeriod>`.
 


.. _co2_bricklet_matlab_callbacks:

Callbacks
^^^^^^^^^

Callbacks can be registered to receive time critical or recurring data from
the device. The registration is done with "set" function of MATLAB. The
parameters consist of the IP Connection object, the callback name and the
callback function. For example, it looks like this in MATLAB:

.. code-block:: matlab

    function cb_example(e)
        fprintf('Parameter: %s\n', e.param);
    end

    set(device, 'ExampleCallback', @(h, e) cb_example(e));

Due to a difference in the Octave Java support the "set" function cannot be
used in Octave. The registration is done with "add*Callback" functions of the
device object. It looks like this in Octave:

.. code-block:: octave_fixed

    function cb_example(e)
        fprintf("Parameter: %s\n", e.param);
    end

    device.addExampleCallback(@cb_example);

It is possible to add several callbacks and to remove them with the
corresponding "remove*Callback" function.

The parameters of the callback are passed to the callback function as fields of
the structure ``e``, which is derived from the ``java.util.EventObject`` class.
The available callback names with corresponding structure fields 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.



.. matlab:member:: public callback BrickletCO2.CO2ConcentrationCallback

 :param co2Concentration: int


 This callback is triggered periodically with the period that is set by
 :matlab:func:`setCO2ConcentrationCallbackPeriod() <BrickletCO2::setCO2ConcentrationCallbackPeriod>`. The parameter is the co2 concentration of the
 sensor.
 
 :matlab:member:`CO2ConcentrationCallback <BrickletCO2.CO2ConcentrationCallback>` is only triggered if the co2 concentration has changed since the
 last triggering.
 

 In MATLAB the ``set()`` function can be used to register a callback function
 to this callback.

 In Octave a callback function can be added to this callback using the
 ``addCO2ConcentrationCallback()`` function. An added callback function can be removed with
 the ``removeCO2ConcentrationCallback()`` function.


.. matlab:member:: public callback BrickletCO2.CO2ConcentrationReachedCallback

 :param co2Concentration: int


 This callback is triggered when the threshold as set by
 :matlab:func:`setCO2ConcentrationCallbackThreshold() <BrickletCO2::setCO2ConcentrationCallbackThreshold>` is reached.
 The parameter is the CO2 concentration.
 
 If the threshold keeps being reached, the callback is triggered periodically
 with the period as set by :matlab:func:`setDebouncePeriod() <BrickletCO2::setDebouncePeriod>`.
 

 In MATLAB the ``set()`` function can be used to register a callback function
 to this callback.

 In Octave a callback function can be added to this callback using the
 ``addCO2ConcentrationReachedCallback()`` function. An added callback function can be removed with
 the ``removeCO2ConcentrationReachedCallback()`` function.



.. _co2_bricklet_matlab_constants:

Constants
^^^^^^^^^

.. matlab:member:: public static final int BrickletCO2.DEVICE_IDENTIFIER

 This constant is used to identify a CO2 Bricklet.

 The :matlab:func:`getIdentity() <BrickletCO2::getIdentity>` function and the
 :matlab:member:`EnumerateCallback <IPConnection.EnumerateCallback>`
 callback of the IP Connection have a ``deviceIdentifier`` parameter to specify
 the Brick's or Bricklet's type.

