1#include<stdio.h> 2 3#include"ip_connection.h" 4#include"bricklet_dual_button.h" 5 6#define HOST "localhost" 7#define PORT 4223 8#define UID "XYZ" // Change XYZ to the UID of your Dual Button Bricklet 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_BUTTON_STATE_PRESSED){16printf("Left Button: Pressed\n");17}elseif(button_l==DUAL_BUTTON_BUTTON_STATE_RELEASED){18printf("Left Button: Released\n");19}2021if(button_r==DUAL_BUTTON_BUTTON_STATE_PRESSED){22printf("Right Button: Pressed\n");23}elseif(button_r==DUAL_BUTTON_BUTTON_STATE_RELEASED){24printf("Right Button: Released\n");25}2627printf("\n");28}2930intmain(void){31// Create IP connection32IPConnectionipcon;33ipcon_create(&ipcon);3435// Create device object36DualButtondb;37dual_button_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_register_callback(&db,48DUAL_BUTTON_CALLBACK_STATE_CHANGED,49(void(*)(void))cb_state_changed,50NULL);5152printf("Press key to exit\n");53getchar();54dual_button_destroy(&db);55ipcon_destroy(&ipcon);// Calls ipcon_disconnect internally56return0;57}
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)
Callbacks can be registered to receive time critical or recurring data from the
device. The registration is done with the dual_button_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_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: