1#include<stdio.h> 2 3#include"ip_connection.h" 4#include"bricklet_dual_button_v2.h" 5 6#define HOST "localhost" 7#define PORT 4223 8#define UID "XYZ" // Change XYZ to the UID of your Dual Button Bricklet 2.0 910// Callback function for state changed callback11voidcb_state_changed(uint8_tbutton_l,uint8_tbutton_r,uint8_tled_l,uint8_tled_r,12void*user_data){13(void)led_l;(void)led_r;(void)user_data;// avoid unused parameter warning1415if(button_l==DUAL_BUTTON_V2_BUTTON_STATE_PRESSED){16printf("Left Button: Pressed\n");17}elseif(button_l==DUAL_BUTTON_V2_BUTTON_STATE_RELEASED){18printf("Left Button: Released\n");19}2021if(button_r==DUAL_BUTTON_V2_BUTTON_STATE_PRESSED){22printf("Right Button: Pressed\n");23}elseif(button_r==DUAL_BUTTON_V2_BUTTON_STATE_RELEASED){24printf("Right Button: Released\n");25}2627printf("\n");28}2930intmain(void){31// Create IP connection32IPConnectionipcon;33ipcon_create(&ipcon);3435// Create device object36DualButtonV2db;37dual_button_v2_create(&db,UID,&ipcon);3839// Connect to brickd40if(ipcon_connect(&ipcon,HOST,PORT)<0){41fprintf(stderr,"Could not connect\n");42return1;43}44// Don't use device before ipcon is connected4546// Register state changed callback to function cb_state_changed47dual_button_v2_register_callback(&db,48DUAL_BUTTON_V2_CALLBACK_STATE_CHANGED,49(void(*)(void))cb_state_changed,50NULL);5152// Enable state changed callback53dual_button_v2_set_state_changed_callback_configuration(&db,true);5455printf("Press key to exit\n");56getchar();57dual_button_v2_destroy(&db);58ipcon_destroy(&ipcon);// Calls ipcon_disconnect internally59return0;60}
Most functions of the C/C++ bindings return an error code (e_code).
Data returned from the device, when a getter is called,
is handled via output parameters. 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 C/C++ bindings version 2.0.0)
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 dual_button_v2_register_callback() function:
The available constants with corresponding 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.
Virtual functions don't communicate with the device itself, but operate only on
the API bindings device object. They can be called without the corresponding
IP Connection object being connected.
Returns the version of the API definition 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.
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
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 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.