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


:breadcrumbs: <a href="../../index.html">Home</a> / <a href="../../index.html#software-delphi">Software</a> / Delphi/Lazarus - Analog In Bricklet

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

.. _analog_in_bricklet_delphi:

Delphi/Lazarus - Analog In Bricklet
===================================

This is the description of the :ref:`Delphi/Lazarus API bindings <api_bindings_delphi>` for the :ref:`Analog In Bricklet <analog_in_bricklet>`. General information and technical
specifications for the Analog In Bricklet are summarized in its :ref:`hardware description
<analog_in_bricklet_description>`.

An :ref:`installation guide <api_bindings_delphi_install>` for the Delphi/Lazarus API
bindings is part of their general description.

.. _analog_in_bricklet_delphi_examples:


Examples
--------

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

Simple
^^^^^^

`Download (ExampleSimple.pas) <https://github.com/Tinkerforge/analog-in-bricklet/raw/master/software/examples/delphi/ExampleSimple.pas>`__

.. literalinclude:: AnalogIn_Bricklet_Delphi_ExampleSimple.pas
 :language: delphi
 :linenos:
 :tab-width: 4

Callback
^^^^^^^^

`Download (ExampleCallback.pas) <https://github.com/Tinkerforge/analog-in-bricklet/raw/master/software/examples/delphi/ExampleCallback.pas>`__

.. literalinclude:: AnalogIn_Bricklet_Delphi_ExampleCallback.pas
 :language: delphi
 :linenos:
 :tab-width: 4

Threshold
^^^^^^^^^

`Download (ExampleThreshold.pas) <https://github.com/Tinkerforge/analog-in-bricklet/raw/master/software/examples/delphi/ExampleThreshold.pas>`__

.. literalinclude:: AnalogIn_Bricklet_Delphi_ExampleThreshold.pas
 :language: delphi
 :linenos:
 :tab-width: 4

.. _analog_in_bricklet_delphi_api:

API
---

Since Delphi does not support multiple return values directly, we use the
``out`` keyword to return multiple values from a function.

All functions and procedures listed below are thread-safe.




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


.. delphi:function:: constructor TBrickletAnalogIn.Create(const uid: string; ipcon: TIPConnection)

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

 .. code-block:: delphi

    analogIn := TBrickletAnalogIn.Create('YOUR_DEVICE_UID', ipcon);

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


.. delphi:function:: function TBrickletAnalogIn.GetVoltage(): word

 Returns the voltage of the sensor. The value is in mV and
 between 0V and 45V. The resolution between 0 and 6V is about 2mV.
 Between 6 and 45V the resolution is about 10mV.
 
 If you want to get the voltage periodically, it is recommended to use the
 callback :delphi:func:`OnVoltage <TBrickletAnalogIn.OnVoltage>` and set the period with 
 :delphi:func:`SetVoltageCallbackPeriod <TBrickletAnalogIn.SetVoltageCallbackPeriod>`.
 
.. delphi:function:: procedure TBrickletAnalogIn.SetRange(const range: byte)

 Sets the measurement range. Possible ranges:
 
 * 0: Automatically switched
 * 1: 0V - 6.05V, ~1.48mV resolution
 * 2: 0V - 10.32V, ~2.52mV resolution
 * 3: 0V - 36.30V, ~8.86mV resolution
 * 4: 0V - 45.00V, ~11.25mV resolution
 * 5: 0V - 3.3V, ~0.81mV resolution, new in version 2.0.3 (Plugin)
 
 The default measurement range is 0.
 
 The following constants are available for this function:
 
 * BRICKLET_ANALOG_IN_RANGE_AUTOMATIC = 0
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_6V = 1
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_10V = 2
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_36V = 3
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_45V = 4
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_3V = 5
 
 .. versionadded:: 2.0.1~(Plugin)
 
.. delphi:function:: function TBrickletAnalogIn.GetRange(): byte

 Returns the measurement range as set by :delphi:func:`SetRange <TBrickletAnalogIn.SetRange>`.
 
 The following constants are available for this function:
 
 * BRICKLET_ANALOG_IN_RANGE_AUTOMATIC = 0
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_6V = 1
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_10V = 2
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_36V = 3
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_45V = 4
 * BRICKLET_ANALOG_IN_RANGE_UP_TO_3V = 5
 
 .. versionadded:: 2.0.1~(Plugin)
 


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

.. delphi:function:: function TBrickletAnalogIn.GetAnalogValue(): word

 Returns the value as read by a 12-bit analog-to-digital converter.
 The value is between 0 and 4095.
 
 .. note::
  The value returned by :delphi:func:`GetVoltage <TBrickletAnalogIn.GetVoltage>` is averaged over several samples
  to yield less noise, while :delphi:func:`GetAnalogValue <TBrickletAnalogIn.GetAnalogValue>` gives back raw
  unfiltered analog values. The only reason to use :delphi:func:`GetAnalogValue <TBrickletAnalogIn.GetAnalogValue>` is,
  if you need the full resolution of the analog-to-digital converter.
 
 If you want the analog value periodically, it is recommended to use the 
 callback :delphi:func:`OnAnalogValue <TBrickletAnalogIn.OnAnalogValue>` and set the period with 
 :delphi:func:`SetAnalogValueCallbackPeriod <TBrickletAnalogIn.SetAnalogValueCallbackPeriod>`.
 
.. delphi:function:: procedure TBrickletAnalogIn.SetAveraging(const average: byte)

 Set the length of a averaging for the voltage value.
 
 Setting the length to 0 will turn the averaging completely off. If the
 averaging is off, there is more noise on the data, but the data is without
 delay.
 
 The default value is 50.
 
 .. versionadded:: 2.0.3~(Plugin)
 
.. delphi:function:: function TBrickletAnalogIn.GetAveraging(): byte

 Returns the averaging configuration as set by :delphi:func:`SetAveraging <TBrickletAnalogIn.SetAveraging>`.
 
 .. versionadded:: 2.0.3~(Plugin)
 
.. delphi:function:: function TBrickletAnalogIn.GetAPIVersion(): array [0..2] of byte

 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.
 
.. delphi:function:: function TBrickletAnalogIn.GetResponseExpected(const functionId: byte): boolean

 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
 :delphi:func:`SetResponseExpected <TBrickletAnalogIn.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 :delphi:func:`SetResponseExpected <TBrickletAnalogIn.SetResponseExpected>`
 for the list of function ID constants available for this function.
 
.. delphi:function:: procedure TBrickletAnalogIn.SetResponseExpected(const functionId: byte; const responseExpected: boolean)

 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:
 
 * BRICKLET_ANALOG_IN_FUNCTION_SET_VOLTAGE_CALLBACK_PERIOD = 3
 * BRICKLET_ANALOG_IN_FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD = 5
 * BRICKLET_ANALOG_IN_FUNCTION_SET_VOLTAGE_CALLBACK_THRESHOLD = 7
 * BRICKLET_ANALOG_IN_FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD = 9
 * BRICKLET_ANALOG_IN_FUNCTION_SET_DEBOUNCE_PERIOD = 11
 * BRICKLET_ANALOG_IN_FUNCTION_SET_RANGE = 17
 * BRICKLET_ANALOG_IN_FUNCTION_SET_AVERAGING = 19
 
.. delphi:function:: procedure TBrickletAnalogIn.SetResponseExpectedAll(const responseExpected: boolean)

 Changes the response expected flag for all setter and callback configuration
 functions of this device at once.
 
.. delphi:function:: procedure TBrickletAnalogIn.GetIdentity(out uid: string; out connectedUid: string; out position: char; out hardwareVersion: array [0..2] of byte; out firmwareVersion: array [0..2] of byte; out deviceIdentifier: word)

 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|
 


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

.. delphi:function:: procedure TBrickletAnalogIn.SetVoltageCallbackPeriod(const period: longword)

 Sets the period in ms with which the :delphi:func:`OnVoltage <TBrickletAnalogIn.OnVoltage>` callback is triggered
 periodically. A value of 0 turns the callback off.
 
 :delphi:func:`OnVoltage <TBrickletAnalogIn.OnVoltage>` is only triggered if the voltage has changed since the
 last triggering.
 
 The default value is 0.
 
.. delphi:function:: function TBrickletAnalogIn.GetVoltageCallbackPeriod(): longword

 Returns the period as set by :delphi:func:`SetVoltageCallbackPeriod <TBrickletAnalogIn.SetVoltageCallbackPeriod>`.
 
.. delphi:function:: procedure TBrickletAnalogIn.SetAnalogValueCallbackPeriod(const period: longword)

 Sets the period in ms with which the :delphi:func:`OnAnalogValue <TBrickletAnalogIn.OnAnalogValue>` callback is triggered
 periodically. A value of 0 turns the callback off.
 
 :delphi:func:`OnAnalogValue <TBrickletAnalogIn.OnAnalogValue>` is only triggered if the analog value has changed since the
 last triggering.
 
 The default value is 0.
 
.. delphi:function:: function TBrickletAnalogIn.GetAnalogValueCallbackPeriod(): longword

 Returns the period as set by :delphi:func:`SetAnalogValueCallbackPeriod <TBrickletAnalogIn.SetAnalogValueCallbackPeriod>`.
 
.. delphi:function:: procedure TBrickletAnalogIn.SetVoltageCallbackThreshold(const option: char; const min: smallint; const max: smallint)

 Sets the thresholds for the :delphi:func:`OnVoltageReached <TBrickletAnalogIn.OnVoltageReached>` 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 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:
 
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OFF = 'x'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OUTSIDE = 'o'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_INSIDE = 'i'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_SMALLER = '<'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_GREATER = '>'
 
.. delphi:function:: procedure TBrickletAnalogIn.GetVoltageCallbackThreshold(out option: char; out min: smallint; out max: smallint)

 Returns the threshold as set by :delphi:func:`SetVoltageCallbackThreshold <TBrickletAnalogIn.SetVoltageCallbackThreshold>`.
 
 The following constants are available for this function:
 
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OFF = 'x'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OUTSIDE = 'o'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_INSIDE = 'i'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_SMALLER = '<'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_GREATER = '>'
 
.. delphi:function:: procedure TBrickletAnalogIn.SetAnalogValueCallbackThreshold(const option: char; const min: word; const max: word)

 Sets the thresholds for the :delphi:func:`OnAnalogValueReached <TBrickletAnalogIn.OnAnalogValueReached>` 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 analog value is *outside* the min and max values"
  "'i'",    "Callback is triggered when the analog value is *inside* the min and max values"
  "'<'",    "Callback is triggered when the analog value is smaller than the min value (max is ignored)"
  "'>'",    "Callback is triggered when the analog value is greater than the min value (max is ignored)"
 
 The default value is ('x', 0, 0).
 
 The following constants are available for this function:
 
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OFF = 'x'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OUTSIDE = 'o'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_INSIDE = 'i'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_SMALLER = '<'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_GREATER = '>'
 
.. delphi:function:: procedure TBrickletAnalogIn.GetAnalogValueCallbackThreshold(out option: char; out min: word; out max: word)

 Returns the threshold as set by :delphi:func:`SetAnalogValueCallbackThreshold <TBrickletAnalogIn.SetAnalogValueCallbackThreshold>`.
 
 The following constants are available for this function:
 
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OFF = 'x'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_OUTSIDE = 'o'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_INSIDE = 'i'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_SMALLER = '<'
 * BRICKLET_ANALOG_IN_THRESHOLD_OPTION_GREATER = '>'
 
.. delphi:function:: procedure TBrickletAnalogIn.SetDebouncePeriod(const debounce: longword)

 Sets the period in ms with which the threshold callbacks
 
 * :delphi:func:`OnVoltageReached <TBrickletAnalogIn.OnVoltageReached>`,
 * :delphi:func:`OnAnalogValueReached <TBrickletAnalogIn.OnAnalogValueReached>`
 
 are triggered, if the thresholds
 
 * :delphi:func:`SetVoltageCallbackThreshold <TBrickletAnalogIn.SetVoltageCallbackThreshold>`,
 * :delphi:func:`SetAnalogValueCallbackThreshold <TBrickletAnalogIn.SetAnalogValueCallbackThreshold>`
 
 keep being reached.
 
 The default value is 100.
 
.. delphi:function:: function TBrickletAnalogIn.GetDebouncePeriod(): longword

 Returns the debounce period as set by :delphi:func:`SetDebouncePeriod <TBrickletAnalogIn.SetDebouncePeriod>`.
 




.. _analog_in_bricklet_delphi_callbacks:

Callbacks
^^^^^^^^^

Callbacks can be registered to receive time critical or recurring data from
the device. The registration is done by assigning a procedure to an callback
property of the device object:

 .. code-block:: delphi

  procedure TExample.MyCallback(sender: TBrickletAnalogIn; const param: word);
  begin
    WriteLn(param);
  end;

  analogIn.OnExample := {$ifdef FPC}@{$endif}example.MyCallback;

The available callback property and their 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.

.. delphi:function:: property TBrickletAnalogIn.OnVoltage

 .. code-block:: delphi

  procedure(sender: TBrickletAnalogIn; const voltage: word) of object;


 This callback is triggered periodically with the period that is set by
 :delphi:func:`SetVoltageCallbackPeriod <TBrickletAnalogIn.SetVoltageCallbackPeriod>`. The parameter is the voltage of the
 sensor.
 
 :delphi:func:`OnVoltage <TBrickletAnalogIn.OnVoltage>` is only triggered if the voltage has changed since the
 last triggering.
 
.. delphi:function:: property TBrickletAnalogIn.OnAnalogValue

 .. code-block:: delphi

  procedure(sender: TBrickletAnalogIn; const value: word) of object;


 This callback is triggered periodically with the period that is set by
 :delphi:func:`SetAnalogValueCallbackPeriod <TBrickletAnalogIn.SetAnalogValueCallbackPeriod>`. The parameter is the analog value of the
 sensor.
 
 :delphi:func:`OnAnalogValue <TBrickletAnalogIn.OnAnalogValue>` is only triggered if the voltage has changed since the
 last triggering.
 
.. delphi:function:: property TBrickletAnalogIn.OnVoltageReached

 .. code-block:: delphi

  procedure(sender: TBrickletAnalogIn; const voltage: word) of object;


 This callback is triggered when the threshold as set by
 :delphi:func:`SetVoltageCallbackThreshold <TBrickletAnalogIn.SetVoltageCallbackThreshold>` is reached.
 The parameter is the voltage of the sensor.
 
 If the threshold keeps being reached, the callback is triggered periodically
 with the period as set by :delphi:func:`SetDebouncePeriod <TBrickletAnalogIn.SetDebouncePeriod>`.
 
.. delphi:function:: property TBrickletAnalogIn.OnAnalogValueReached

 .. code-block:: delphi

  procedure(sender: TBrickletAnalogIn; const value: word) of object;


 This callback is triggered when the threshold as set by
 :delphi:func:`SetAnalogValueCallbackThreshold <TBrickletAnalogIn.SetAnalogValueCallbackThreshold>` is reached.
 The parameter is the analog value of the sensor.
 
 If the threshold keeps being reached, the callback is triggered periodically
 with the period as set by :delphi:func:`SetDebouncePeriod <TBrickletAnalogIn.SetDebouncePeriod>`.
 


.. _analog_in_bricklet_delphi_constants:

Constants
^^^^^^^^^

.. delphi:function:: const BRICKLET_ANALOG_IN_DEVICE_IDENTIFIER

 This constant is used to identify a Analog In Bricklet.

 The :delphi:func:`GetIdentity <TBrickletAnalogIn.GetIdentity>` function and the
 :delphi:func:`OnEnumerate <TIPConnection.OnEnumerate>`
 callback of the IP Connection have a ``deviceIdentifier`` parameter to specify
 the Brick's or Bricklet's type.

