1// This example is not self-contained. 2// It requires usage of the example driver specific to your platform. 3// See the HAL documentation. 4 5#include"src/bindings/hal_common.h" 6#include"src/bindings/bricklet_dual_button_v2.h" 7 8voidcheck(intrc,constchar*msg); 9voidexample_setup(TF_HAL*hal);10voidexample_loop(TF_HAL*hal);1112// Callback function for state changed callback13staticvoidstate_changed_handler(TF_DualButtonV2*device,uint8_tbutton_l,14uint8_tbutton_r,uint8_tled_l,uint8_tled_r,15void*user_data){16(void)device;(void)led_l;(void)led_r;(void)user_data;// avoid unused parameter warning1718if(button_l==TF_DUAL_BUTTON_V2_BUTTON_STATE_PRESSED){19tf_hal_printf("Left Button: Pressed\n");20}elseif(button_l==TF_DUAL_BUTTON_V2_BUTTON_STATE_RELEASED){21tf_hal_printf("Left Button: Released\n");22}2324if(button_r==TF_DUAL_BUTTON_V2_BUTTON_STATE_PRESSED){25tf_hal_printf("Right Button: Pressed\n");26}elseif(button_r==TF_DUAL_BUTTON_V2_BUTTON_STATE_RELEASED){27tf_hal_printf("Right Button: Released\n");28}2930tf_hal_printf("\n");31}3233staticTF_DualButtonV2db;3435voidexample_setup(TF_HAL*hal){36// Create device object37check(tf_dual_button_v2_create(&db,NULL,hal),"create device object");3839// Register state changed callback to function state_changed_handler40tf_dual_button_v2_register_state_changed_callback(&db,41state_changed_handler,42NULL);4344// Enable state changed callback45check(tf_dual_button_v2_set_state_changed_callback_configuration(&db,46true),"call set_state_changed_callback_configuration");47}4849voidexample_loop(TF_HAL*hal){50// Poll for callbacks51tf_hal_callback_tick(hal,0);52}
Most functions of the C/C++ bindings for microcontrollers return an error code
(e_code).
Possible error codes are:
TF_E\ _OK = 0
TF_E\ _TIMEOUT = -1
TF_E\ _INVALID_PARAMETER = -2
TF_E\ _NOT_SUPPORTED = -3
TF_E\ _UNKNOWN_ERROR_CODE = -4
TF_E\ _STREAM_OUT_OF_SYNC = -5
TF_E\ _INVALID_CHAR_IN_UID = -6
TF_E\ _UID_TOO_LONG = -7
TF_E\ _UID_OVERFLOW = -8
TF_E\ _TOO_MANY_DEVICES = -9
TF_E\ _DEVICE_NOT_FOUND = -10
TF_E\ _WRONG_DEVICE_TYPE = -11
TF_E\ _LOCKED = -12
TF_E\ _PORT_NOT_FOUND = -13
(as defined in errors.h) as well as the errors returned from
the hardware abstraction layer (HAL) that is used.
Use :cpp:func`tf_hal_strerror` (defined in the HAL's header file) to get
an error string for an error code.
Data returned from the device, when a getter is called,
is handled via output parameters. These parameters are labeled with the
ret_ prefix. The bindings will not write to an output parameter if NULL or nullptr
is passed. This can be used to ignore outputs that you are not interested in.
None of the functions listed below are thread-safe.
See the API bindings description for details.
config – Type: uint8_t, Range: See constants, Default: 3
Returns:
e_code – Type: int
Sets the status LED configuration. By default the LED shows
communication traffic between Brick and Bricklet, it flickers once
for every 10 received data packets.
You can also turn the LED permanently on/off or show a heartbeat.
If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
The following constants are available for this function:
ret_temperature – Type: int16_t, Unit: 1 °C, Range: [-215 to 215 - 1]
Returns:
e_code – Type: int
Returns the temperature as measured inside the microcontroller. The
value returned is not the ambient temperature!
The temperature is only proportional to the real temperature and it has bad
accuracy. Practically it is only useful as an indicator for
temperature changes.
Callbacks can be registered to receive time critical or recurring data from the
device. The registration is done with the corresponding tf_dual_button_v2_register_*_callback function.
The user_data passed to the registration function as well as the device that triggered the callback are
passed to the registered callback handler.
Only one handler can be registered to a callback at the same time.
To deregister a callback, call the tf_dual_button_v2_register_*_callback function
with NULL as handler.
Note
Using callbacks for recurring events is preferred
compared to using getters. Polling for a callback requires
writing one byte only. See here Optimizing Performance.
Warning
Calling bindings function from inside a callback handler is not allowed.
See here Thread safety.
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
tf_dual_button_v2_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 sent and errors are silently ignored,
because they cannot be detected.
The following constants are available for this function:
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.
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 sent and errors are silently ignored,
because they cannot be detected.
The following constants are available for this function:
Internal functions are used for maintenance tasks such as flashing a new firmware
of changing the UID of a Bricklet. These task should be performed using
Brick Viewer instead of using the internal functions directly.
Sets the bootloader mode and returns the status after the requested
mode change was instigated.
You can change from bootloader mode to firmware mode and vice versa. A change
from bootloader mode to firmware mode will only take place if the entry function,
device identifier and CRC are present and correct.
This function is used by Brick Viewer during flashing. It should not be
necessary to call it in a normal user program.
The following constants are available for this function:
Sets the firmware pointer for tf_dual_button_v2_write_firmware(). The pointer has
to be increased by chunks of size 64. The data is written to flash
every 4 chunks (which equals to one page of size 256).
This function is used by Brick Viewer during flashing. It should not be
necessary to call it in a normal user program.
This constant is used to identify a Dual Button Bricklet 2.0.
The functions tf_dual_button_v2_get_identity() and tf_hal_get_device_info()
have a device_identifier output parameter to specify
the Brick's or Bricklet's type.