..
 #############################################################
 # 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-c">Software</a> / C/C++ - Remote Switch Bricklet

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

.. _remote_switch_bricklet_c:

C/C++ - Remote Switch Bricklet
==============================

This is the description of the :ref:`C/C++ API bindings <api_bindings_c>` for the :ref:`Remote Switch Bricklet <remote_switch_bricklet>`. General information and technical
specifications for the Remote Switch Bricklet are summarized in its :ref:`hardware description
<remote_switch_bricklet_description>`.

An :ref:`installation guide <api_bindings_c_install>` for the C/C++ API
bindings is part of their general description.

.. _remote_switch_bricklet_c_examples:


Examples
--------

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

Switch Socket
^^^^^^^^^^^^^

`Download (example_switch_socket.c) <https://github.com/Tinkerforge/remote-switch-bricklet/raw/master/software/examples/c/example_switch_socket.c>`__

.. literalinclude:: RemoteSwitch_Bricklet_C_example_switch_socket.c
 :language: c
 :linenos:
 :tab-width: 4

.. _remote_switch_bricklet_c_api:

API
---

Every function of the C/C++ bindings returns an integer which describes an
error code. Data returned from the device, when a getter is called,
is handled via call by reference. These parameters are labeled with the
``ret_`` prefix.

Possible error codes are:

* E_OK = 0
* E_TIMEOUT = -1
* E_NO_STREAM_SOCKET = -2
* E_HOSTNAME_INVALID = -3
* E_NO_CONNECT = -4
* E_NO_THREAD = -5
* E_NOT_ADDED = -6 (unused since bindings version 2.0.0)
* E_ALREADY_CONNECTED = -7
* E_NOT_CONNECTED = -8
* E_INVALID_PARAMETER = -9
* E_NOT_SUPPORTED = -10
* E_UNKNOWN_ERROR_CODE = -11

as defined in :file:`ip_connection.h`.

All functions listed below are thread-safe.




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


.. c:function:: void remote_switch_create(RemoteSwitch *remote_switch, const char *uid, IPConnection *ipcon)

 Creates the device object ``remote_switch`` with the unique device ID ``uid`` and adds
 it to the IPConnection ``ipcon``:

 .. code-block:: c

    RemoteSwitch remote_switch;
    remote_switch_create(&remote_switch, "YOUR_DEVICE_UID", &ipcon);

 This device object can be used after the IP connection has been connected
 (see examples :ref:`above <remote_switch_bricklet_c_examples>`).

.. c:function:: void remote_switch_destroy(RemoteSwitch *remote_switch)

 Removes the device object ``remote_switch`` from its IPConnection and destroys it.
 The device object cannot be used anymore afterwards.


.. c:function:: int remote_switch_switch_socket(RemoteSwitch *remote_switch, uint8_t house_code, uint8_t receiver_code, uint8_t switch_to)

 This function is deprecated, use :c:func:`remote_switch_switch_socket_a` instead.
 
 The following defines are available for this function:
 
 * REMOTE_SWITCH_SWITCH_TO_OFF = 0
 * REMOTE_SWITCH_SWITCH_TO_ON = 1
 
.. c:function:: int remote_switch_get_switching_state(RemoteSwitch *remote_switch, uint8_t *ret_state)

 Returns the current switching state. If the current state is busy, the
 Bricklet is currently sending a code to switch a socket. It will not
 accept any calls of :c:func:`remote_switch_switch_socket` until the state changes to ready.
 
 How long the switching takes is dependent on the number of repeats, see
 :c:func:`remote_switch_set_repeats`.
 
 The following defines are available for this function:
 
 * REMOTE_SWITCH_SWITCHING_STATE_READY = 0
 * REMOTE_SWITCH_SWITCHING_STATE_BUSY = 1
 
.. c:function:: int remote_switch_switch_socket_a(RemoteSwitch *remote_switch, uint8_t house_code, uint8_t receiver_code, uint8_t switch_to)

 To switch a type A socket you have to give the house code, receiver code and the
 state (on or off) you want to switch to.
 
 The house code and receiver code have a range of 0 to 31 (5bit).
 
 A detailed description on how you can figure out the house and receiver code
 can be found :ref:`here <remote_switch_bricklet_type_a_house_and_receiver_code>`.
 
 The following defines are available for this function:
 
 * REMOTE_SWITCH_SWITCH_TO_OFF = 0
 * REMOTE_SWITCH_SWITCH_TO_ON = 1
 
 .. versionadded:: 2.0.1~(Plugin)
 
.. c:function:: int remote_switch_switch_socket_b(RemoteSwitch *remote_switch, uint32_t address, uint8_t unit, uint8_t switch_to)

 To switch a type B socket you have to give the address, unit and the state
 (on or off) you want to switch to.
 
 The address has a range of 0 to 67108863 (26bit) and the unit has a range
 of 0 to 15 (4bit). To switch all devices with the same address use 255 for
 the unit.
 
 A detailed description on how you can teach a socket the address and unit can
 be found :ref:`here <remote_switch_bricklet_type_b_address_and_unit>`.
 
 The following defines are available for this function:
 
 * REMOTE_SWITCH_SWITCH_TO_OFF = 0
 * REMOTE_SWITCH_SWITCH_TO_ON = 1
 
 .. versionadded:: 2.0.1~(Plugin)
 
.. c:function:: int remote_switch_dim_socket_b(RemoteSwitch *remote_switch, uint32_t address, uint8_t unit, uint8_t dim_value)

 To control a type B dimmer you have to give the address, unit and the
 dim value you want to set the dimmer to.
 
 The address has a range of 0 to 67108863 (26bit), the unit and the dim value
 has a range of 0 to 15 (4bit).
 
 A detailed description on how you can teach a dimmer the address and unit can
 be found :ref:`here <remote_switch_bricklet_type_b_address_and_unit>`.
 
 .. versionadded:: 2.0.1~(Plugin)
 
.. c:function:: int remote_switch_switch_socket_c(RemoteSwitch *remote_switch, char system_code, uint8_t device_code, uint8_t switch_to)

 To switch a type C socket you have to give the system code, device code and the
 state (on or off) you want to switch to.
 
 The system code has a range of 'A' to 'P' (4bit) and the device code has a
 range of 1 to 16 (4bit).
 
 A detailed description on how you can figure out the system and device code
 can be found :ref:`here <remote_switch_bricklet_type_c_system_and_device_code>`.
 
 The following defines are available for this function:
 
 * REMOTE_SWITCH_SWITCH_TO_OFF = 0
 * REMOTE_SWITCH_SWITCH_TO_ON = 1
 
 .. versionadded:: 2.0.1~(Plugin)
 


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

.. c:function:: int remote_switch_set_repeats(RemoteSwitch *remote_switch, uint8_t repeats)

 Sets the number of times the code is send when of the :c:func:`remote_switch_switch_socket`
 functions is called. The repeats basically correspond to the amount of time
 that a button of the remote is pressed.
 
 Some dimmers are controlled by the length of a button pressed,
 this can be simulated by increasing the repeats.
 
 The default value is 5.
 
.. c:function:: int remote_switch_get_repeats(RemoteSwitch *remote_switch, uint8_t *ret_repeats)

 Returns the number of repeats as set by :c:func:`remote_switch_set_repeats`. 
 
.. c:function:: int remote_switch_get_api_version(RemoteSwitch *remote_switch, uint8_t ret_api_version[3])

 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.
 
.. c:function:: int remote_switch_get_response_expected(RemoteSwitch *remote_switch, uint8_t function_id, bool *ret_response_expected)

 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
 :c:func:`remote_switch_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 :c:func:`remote_switch_set_response_expected`
 for the list of function ID defines available for this function.
 
.. c:function:: int remote_switch_set_response_expected(RemoteSwitch *remote_switch, uint8_t function_id, bool response_expected)

 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 defines are available for this function:
 
 * REMOTE_SWITCH_FUNCTION_SWITCH_SOCKET = 1
 * REMOTE_SWITCH_FUNCTION_SET_REPEATS = 4
 * REMOTE_SWITCH_FUNCTION_SWITCH_SOCKET_A = 6
 * REMOTE_SWITCH_FUNCTION_SWITCH_SOCKET_B = 7
 * REMOTE_SWITCH_FUNCTION_DIM_SOCKET_B = 8
 * REMOTE_SWITCH_FUNCTION_SWITCH_SOCKET_C = 9
 
.. c:function:: int remote_switch_set_response_expected_all(RemoteSwitch *remote_switch, bool response_expected)

 Changes the response expected flag for all setter and callback configuration
 functions of this device at once.
 
.. c:function:: int remote_switch_get_identity(RemoteSwitch *remote_switch, char ret_uid[8], char ret_connected_uid[8], char *ret_position, uint8_t ret_hardware_version[3], uint8_t ret_firmware_version[3], uint16_t *ret_device_identifier)

 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


.. c:function:: void remote_switch_register_callback(RemoteSwitch *remote_switch, uint8_t id, void *callback, void *user_data)

 Registers a callback with ID ``id`` to the function ``callback``. The
 ``user_data`` will be given as a parameter of the callback.

 The available IDs with corresponding function signatures are listed
 :ref:`below <remote_switch_bricklet_c_callbacks>`.




.. _remote_switch_bricklet_c_callbacks:

Callbacks
^^^^^^^^^

Callbacks can be registered to receive
time critical or recurring data from the device. The registration is done
with the :c:func:`remote_switch_register_callback` function. The parameters consist of
the device object, the callback ID, the callback function and optional
user data:

 .. code-block:: c

    void my_callback(int p, void *user_data) {
        printf("parameter: %d\n", p);
    }

    remote_switch_register_callback(&remote_switch, REMOTE_SWITCH_CALLBACK_EXAMPLE, (void *)my_callback, NULL);

The available constants with corresponding callback function signatures
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.

.. c:var:: REMOTE_SWITCH_CALLBACK_SWITCHING_DONE

 .. code-block:: c

  void callback(void *user_data)


 This callback is called whenever the switching state changes
 from busy to ready, see :c:func:`remote_switch_get_switching_state`.
 


.. _remote_switch_bricklet_c_constants:

Constants
^^^^^^^^^

.. c:var:: REMOTE_SWITCH_DEVICE_IDENTIFIER

 This constant is used to identify a Remote Switch Bricklet.

 The :c:func:`remote_switch_get_identity` function and the :c:data:`IPCON_CALLBACK_ENUMERATE`
 callback of the IP Connection have a ``device_identifier`` parameter to specify
 the Brick's or Bricklet's type.

