diff --git a/main/ble_spp_server_demo.c b/main/ble_spp_server_demo.c index 0bda2a2..36a9568 100644 --- a/main/ble_spp_server_demo.c +++ b/main/ble_spp_server_demo.c @@ -25,6 +25,7 @@ static bleuart_t *s_module; static blerxcb_t s_cb; +static blesetmotorcb_t s_set_motor_cb; static uint16_t table_conn_id_m; static esp_gatt_if_t table_gatts_if_m; static uint16_t table_handle_m; @@ -171,8 +172,8 @@ static const uint8_t spp_data_notify_val[20] = {0x00}; static const uint8_t spp_data_notify_ccc[2] = {0x00, 0x00}; /// SPP Service - command characteristic, read&write without response -// static const uint16_t spp_command_uuid = ESP_GATT_UUID_SPP_COMMAND_RECEIVE; -// static const uint8_t spp_command_val[10] = {0x00}; +static const uint16_t spp_command_uuid = ESP_GATT_UUID_SPP_COMMAND_RECEIVE; +static const uint8_t spp_command_val[10] = {0x00}; /// SPP Service - status characteristic, notify&read // static const uint16_t spp_status_uuid = ESP_GATT_UUID_SPP_COMMAND_NOTIFY; @@ -214,13 +215,13 @@ static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] = { {ESP_UUID_LEN_128, (uint8_t *)&spp_char_uuid128_tx, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, SPP_DATA_MAX_LEN, sizeof(spp_data_receive_val), (uint8_t *)spp_data_receive_val}}, -// // SPP - command characteristic Declaration -// [SPP_IDX_SPP_COMMAND_CHAR] = {{ESP_GATT_AUTO_RSP}, -// {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}}, + // SPP - command characteristic Declaration + [SPP_IDX_SPP_COMMAND_CHAR] = {{ESP_GATT_AUTO_RSP}, + {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}}, -// // SPP - command characteristic Value -// [SPP_IDX_SPP_COMMAND_VAL] = {{ESP_GATT_AUTO_RSP}, -// {ESP_UUID_LEN_16, (uint8_t *)&spp_command_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, SPP_CMD_MAX_LEN, sizeof(spp_command_val), (uint8_t *)spp_command_val}}, + // SPP - command characteristic Value + [SPP_IDX_SPP_COMMAND_VAL] = {{ESP_GATT_AUTO_RSP}, + {ESP_UUID_LEN_16, (uint8_t *)&spp_command_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, SPP_CMD_MAX_LEN, sizeof(spp_command_val), (uint8_t *)spp_command_val}}, // // SPP - status characteristic Declaration // [SPP_IDX_SPP_STATUS_CHAR] = {{ESP_GATT_AUTO_RSP}, @@ -545,6 +546,17 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ // uart_write_bytes(UART_NUM_0, (char *)(p_data->write.value), p_data->write.len); blerxprocess(p_data->write.value, p_data->write.len); #endif + } else if (res == SPP_IDX_SPP_COMMAND_VAL) { + ESP_LOGI("Finny", "command:%s", p_data->write.value); + if (strcmp((char *)p_data->write.value, "setPosition180") == 0) { + ESP_LOGI("Finny", "setPosition180 ok!,command:%s", p_data->write.value); + s_set_motor_cb(1, 180, 0); + } + if (strcmp((char *)p_data->write.value, "setPosition360") == 0) { + ESP_LOGI("Finny", "setPosition360 ok!,command:%s", p_data->write.value); + s_set_motor_cb(1, 360, 0); + } + } else { // TODO: } @@ -654,6 +666,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_ } void bleuart_reg_cb(blerxcb_t cb) { s_cb = cb; } +void ble_set_motor_reg_cb(blesetmotorcb_t cb) { s_set_motor_cb = cb; } void bleuart_init(bleuart_t *bleuart) { esp_err_t ret; diff --git a/main/ble_spp_server_demo.h b/main/ble_spp_server_demo.h index d1b9c61..40473fa 100644 --- a/main/ble_spp_server_demo.h +++ b/main/ble_spp_server_demo.h @@ -43,8 +43,8 @@ enum { SPP_IDX_SPP_DATA_RECV_CHAR, SPP_IDX_SPP_DATA_RECV_VAL, - // SPP_IDX_SPP_COMMAND_CHAR, - // SPP_IDX_SPP_COMMAND_VAL, + SPP_IDX_SPP_COMMAND_CHAR, + SPP_IDX_SPP_COMMAND_VAL, // SPP_IDX_SPP_STATUS_CHAR, // SPP_IDX_SPP_STATUS_VAL, @@ -79,9 +79,11 @@ typedef struct { } bleuart_t; /*接收完整JSON*/ typedef void (*blerxcb_t)(uint8_t *rx, size_t rxsize); +typedef uint8_t (*blesetmotorcb_t)(int rotation_direction, double position, int speed_level); void bleuart_init(bleuart_t *bleuart); void bleuart_reg_cb(blerxcb_t cb); +void ble_set_motor_reg_cb(blesetmotorcb_t cb); void bleuart_send_packet(uint8_t *tx, size_t txsize); /** diff --git a/main/main.c b/main/main.c index 55a19d2..ea2ac72 100644 --- a/main/main.c +++ b/main/main.c @@ -249,6 +249,7 @@ void port_do_debug_light_state() { void app_main(void) { bleuart_init(&ble_uart_init_struct); bleuart_reg_cb(blerxcb); + ble_set_motor_reg_cb(motor_run_to_postion); motor_init(&ble_uart_motor_structer); motor_reg_event_cb(motor_on_event);