00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #define IPCON_EXPOSE_INTERNALS
00013
00014 #include "bricklet_dual_button.h"
00015
00016 #include <string.h>
00017
00018 #ifdef __cplusplus
00019 extern "C" {
00020 #endif
00021
00022
00023
00024 typedef void (*StateChangedCallbackFunction)(uint8_t, uint8_t, uint8_t, uint8_t, void *);
00025
00026 #if defined _MSC_VER || defined __BORLANDC__
00027 #pragma pack(push)
00028 #pragma pack(1)
00029 #define ATTRIBUTE_PACKED
00030 #elif defined __GNUC__
00031 #ifdef _WIN32
00032
00033
00034 #define ATTRIBUTE_PACKED __attribute__((gcc_struct, packed))
00035 #else
00036 #define ATTRIBUTE_PACKED __attribute__((packed))
00037 #endif
00038 #else
00039 #error unknown compiler, do not know how to enable struct packing
00040 #endif
00041
00042 typedef struct {
00043 PacketHeader header;
00044 uint8_t led_l;
00045 uint8_t led_r;
00046 } ATTRIBUTE_PACKED SetLEDState_;
00047
00048 typedef struct {
00049 PacketHeader header;
00050 } ATTRIBUTE_PACKED GetLEDState_;
00051
00052 typedef struct {
00053 PacketHeader header;
00054 uint8_t led_l;
00055 uint8_t led_r;
00056 } ATTRIBUTE_PACKED GetLEDStateResponse_;
00057
00058 typedef struct {
00059 PacketHeader header;
00060 } ATTRIBUTE_PACKED GetButtonState_;
00061
00062 typedef struct {
00063 PacketHeader header;
00064 uint8_t button_l;
00065 uint8_t button_r;
00066 } ATTRIBUTE_PACKED GetButtonStateResponse_;
00067
00068 typedef struct {
00069 PacketHeader header;
00070 uint8_t button_l;
00071 uint8_t button_r;
00072 uint8_t led_l;
00073 uint8_t led_r;
00074 } ATTRIBUTE_PACKED StateChangedCallback_;
00075
00076 typedef struct {
00077 PacketHeader header;
00078 uint8_t led;
00079 uint8_t state;
00080 } ATTRIBUTE_PACKED SetSelectedLEDState_;
00081
00082 typedef struct {
00083 PacketHeader header;
00084 } ATTRIBUTE_PACKED GetIdentity_;
00085
00086 typedef struct {
00087 PacketHeader header;
00088 char uid[8];
00089 char connected_uid[8];
00090 char position;
00091 uint8_t hardware_version[3];
00092 uint8_t firmware_version[3];
00093 uint16_t device_identifier;
00094 } ATTRIBUTE_PACKED GetIdentityResponse_;
00095
00096 #if defined _MSC_VER || defined __BORLANDC__
00097 #pragma pack(pop)
00098 #endif
00099 #undef ATTRIBUTE_PACKED
00100
00101 static void dual_button_callback_wrapper_state_changed(DevicePrivate *device_p, Packet *packet) {
00102 StateChangedCallbackFunction callback_function;
00103 void *user_data = device_p->registered_callback_user_data[DUAL_BUTTON_CALLBACK_STATE_CHANGED];
00104 StateChangedCallback_ *callback = (StateChangedCallback_ *)packet;
00105 *(void **)(&callback_function) = device_p->registered_callbacks[DUAL_BUTTON_CALLBACK_STATE_CHANGED];
00106
00107 if (callback_function == NULL) {
00108 return;
00109 }
00110
00111 callback_function(callback->button_l, callback->button_r, callback->led_l, callback->led_r, user_data);
00112 }
00113
00114 void dual_button_create(DualButton *dual_button, const char *uid, IPConnection *ipcon) {
00115 DevicePrivate *device_p;
00116
00117 device_create(dual_button, uid, ipcon->p, 2, 0, 0);
00118
00119 device_p = dual_button->p;
00120
00121 device_p->response_expected[DUAL_BUTTON_FUNCTION_SET_LED_STATE] = DEVICE_RESPONSE_EXPECTED_FALSE;
00122 device_p->response_expected[DUAL_BUTTON_FUNCTION_GET_LED_STATE] = DEVICE_RESPONSE_EXPECTED_ALWAYS_TRUE;
00123 device_p->response_expected[DUAL_BUTTON_FUNCTION_GET_BUTTON_STATE] = DEVICE_RESPONSE_EXPECTED_ALWAYS_TRUE;
00124 device_p->response_expected[DUAL_BUTTON_CALLBACK_STATE_CHANGED] = DEVICE_RESPONSE_EXPECTED_ALWAYS_FALSE;
00125 device_p->response_expected[DUAL_BUTTON_FUNCTION_SET_SELECTED_LED_STATE] = DEVICE_RESPONSE_EXPECTED_FALSE;
00126 device_p->response_expected[DUAL_BUTTON_FUNCTION_GET_IDENTITY] = DEVICE_RESPONSE_EXPECTED_ALWAYS_TRUE;
00127
00128 device_p->callback_wrappers[DUAL_BUTTON_CALLBACK_STATE_CHANGED] = dual_button_callback_wrapper_state_changed;
00129 }
00130
00131 void dual_button_destroy(DualButton *dual_button) {
00132 device_release(dual_button->p);
00133 }
00134
00135 int dual_button_get_response_expected(DualButton *dual_button, uint8_t function_id, bool *ret_response_expected) {
00136 return device_get_response_expected(dual_button->p, function_id, ret_response_expected);
00137 }
00138
00139 int dual_button_set_response_expected(DualButton *dual_button, uint8_t function_id, bool response_expected) {
00140 return device_set_response_expected(dual_button->p, function_id, response_expected);
00141 }
00142
00143 int dual_button_set_response_expected_all(DualButton *dual_button, bool response_expected) {
00144 return device_set_response_expected_all(dual_button->p, response_expected);
00145 }
00146
00147 void dual_button_register_callback(DualButton *dual_button, uint8_t id, void *callback, void *user_data) {
00148 device_register_callback(dual_button->p, id, callback, user_data);
00149 }
00150
00151 int dual_button_get_api_version(DualButton *dual_button, uint8_t ret_api_version[3]) {
00152 return device_get_api_version(dual_button->p, ret_api_version);
00153 }
00154
00155 int dual_button_set_led_state(DualButton *dual_button, uint8_t led_l, uint8_t led_r) {
00156 DevicePrivate *device_p = dual_button->p;
00157 SetLEDState_ request;
00158 int ret;
00159
00160 ret = packet_header_create(&request.header, sizeof(request), DUAL_BUTTON_FUNCTION_SET_LED_STATE, device_p->ipcon_p, device_p);
00161
00162 if (ret < 0) {
00163 return ret;
00164 }
00165
00166 request.led_l = led_l;
00167 request.led_r = led_r;
00168
00169 ret = device_send_request(device_p, (Packet *)&request, NULL);
00170
00171
00172 return ret;
00173 }
00174
00175 int dual_button_get_led_state(DualButton *dual_button, uint8_t *ret_led_l, uint8_t *ret_led_r) {
00176 DevicePrivate *device_p = dual_button->p;
00177 GetLEDState_ request;
00178 GetLEDStateResponse_ response;
00179 int ret;
00180
00181 ret = packet_header_create(&request.header, sizeof(request), DUAL_BUTTON_FUNCTION_GET_LED_STATE, device_p->ipcon_p, device_p);
00182
00183 if (ret < 0) {
00184 return ret;
00185 }
00186
00187
00188 ret = device_send_request(device_p, (Packet *)&request, (Packet *)&response);
00189
00190 if (ret < 0) {
00191 return ret;
00192 }
00193 *ret_led_l = response.led_l;
00194 *ret_led_r = response.led_r;
00195
00196
00197
00198 return ret;
00199 }
00200
00201 int dual_button_get_button_state(DualButton *dual_button, uint8_t *ret_button_l, uint8_t *ret_button_r) {
00202 DevicePrivate *device_p = dual_button->p;
00203 GetButtonState_ request;
00204 GetButtonStateResponse_ response;
00205 int ret;
00206
00207 ret = packet_header_create(&request.header, sizeof(request), DUAL_BUTTON_FUNCTION_GET_BUTTON_STATE, device_p->ipcon_p, device_p);
00208
00209 if (ret < 0) {
00210 return ret;
00211 }
00212
00213
00214 ret = device_send_request(device_p, (Packet *)&request, (Packet *)&response);
00215
00216 if (ret < 0) {
00217 return ret;
00218 }
00219 *ret_button_l = response.button_l;
00220 *ret_button_r = response.button_r;
00221
00222
00223
00224 return ret;
00225 }
00226
00227 int dual_button_set_selected_led_state(DualButton *dual_button, uint8_t led, uint8_t state) {
00228 DevicePrivate *device_p = dual_button->p;
00229 SetSelectedLEDState_ request;
00230 int ret;
00231
00232 ret = packet_header_create(&request.header, sizeof(request), DUAL_BUTTON_FUNCTION_SET_SELECTED_LED_STATE, device_p->ipcon_p, device_p);
00233
00234 if (ret < 0) {
00235 return ret;
00236 }
00237
00238 request.led = led;
00239 request.state = state;
00240
00241 ret = device_send_request(device_p, (Packet *)&request, NULL);
00242
00243
00244 return ret;
00245 }
00246
00247 int dual_button_get_identity(DualButton *dual_button, 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) {
00248 DevicePrivate *device_p = dual_button->p;
00249 GetIdentity_ request;
00250 GetIdentityResponse_ response;
00251 int ret;
00252
00253 ret = packet_header_create(&request.header, sizeof(request), DUAL_BUTTON_FUNCTION_GET_IDENTITY, device_p->ipcon_p, device_p);
00254
00255 if (ret < 0) {
00256 return ret;
00257 }
00258
00259
00260 ret = device_send_request(device_p, (Packet *)&request, (Packet *)&response);
00261
00262 if (ret < 0) {
00263 return ret;
00264 }
00265 strncpy(ret_uid, response.uid, 8);
00266 strncpy(ret_connected_uid, response.connected_uid, 8);
00267 *ret_position = response.position;
00268 memcpy(ret_hardware_version, response.hardware_version, 3 * sizeof(uint8_t));
00269 memcpy(ret_firmware_version, response.firmware_version, 3 * sizeof(uint8_t));
00270 *ret_device_identifier = leconvert_uint16_from(response.device_identifier);
00271
00272
00273
00274 return ret;
00275 }
00276
00277 #ifdef __cplusplus
00278 }
00279 #endif