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_rgb_led_button.h" 7 8voidcheck(intrc,constchar*msg); 9voidexample_setup(TF_HAL*hal);10voidexample_loop(TF_HAL*hal);1112staticTF_RGBLEDButtonrlb;1314voidexample_setup(TF_HAL*hal){15// Create device object16check(tf_rgb_led_button_create(&rlb,NULL,hal),"create device object");1718// Set light blue color19check(tf_rgb_led_button_set_color(&rlb,0,170,234),"call set_color");20}2122voidexample_loop(TF_HAL*hal){23// Poll for callbacks24tf_hal_callback_tick(hal,0);25}
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_rgb_led_button.h" 7 8voidcheck(intrc,constchar*msg); 9voidexample_setup(TF_HAL*hal);10voidexample_loop(TF_HAL*hal);1112staticTF_RGBLEDButtonrlb;1314voidexample_setup(TF_HAL*hal){15// Create device object16check(tf_rgb_led_button_create(&rlb,NULL,hal),"create device object");1718// Get current button state19uint8_tstate;20check(tf_rgb_led_button_get_button_state(&rlb,&state),"get button state");2122if(state==TF_RGB_LED_BUTTON_BUTTON_STATE_PRESSED){23tf_hal_printf("State: Pressed\n");24}elseif(state==TF_RGB_LED_BUTTON_BUTTON_STATE_RELEASED){25tf_hal_printf("State: Released\n");26}27}2829voidexample_loop(TF_HAL*hal){30// Poll for callbacks31tf_hal_callback_tick(hal,0);32}
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_rgb_led_button.h" 7 8voidcheck(intrc,constchar*msg); 9voidexample_setup(TF_HAL*hal);10voidexample_loop(TF_HAL*hal);1112// Callback function for button state changed callback13staticvoidbutton_state_changed_handler(TF_RGBLEDButton*device,uint8_tstate,14void*user_data){15(void)device;(void)user_data;// avoid unused parameter warning1617if(state==TF_RGB_LED_BUTTON_BUTTON_STATE_PRESSED){18tf_hal_printf("State: Pressed\n");19}elseif(state==TF_RGB_LED_BUTTON_BUTTON_STATE_RELEASED){20tf_hal_printf("State: Released\n");21}22}2324staticTF_RGBLEDButtonrlb;2526voidexample_setup(TF_HAL*hal){27// Create device object28check(tf_rgb_led_button_create(&rlb,NULL,hal),"create device object");2930// Register button state changed callback to function button_state_changed_handler31tf_rgb_led_button_register_button_state_changed_callback(&rlb,32button_state_changed_handler,33NULL);34}3536voidexample_loop(TF_HAL*hal){37// Poll for callbacks38tf_hal_callback_tick(hal,0);39}
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_rgb_led_button_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_rgb_led_button_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_rgb_led_button_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_rgb_led_button_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 RGB LED Button Bricklet.
The functions tf_rgb_led_button_get_identity() and tf_hal_get_device_info()
have a device_identifier output parameter to specify
the Brick's or Bricklet's type.