diff --git a/main/gatts_demo.c b/main/gatts_demo.c index d03df1b..2088279 100644 --- a/main/gatts_demo.c +++ b/main/gatts_demo.c @@ -201,6 +201,7 @@ void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble #define profile_b_buffer_size 100 char bluetooth_rx_buffer[profile_b_buffer_size] = {0}; uint8_t bluetooth_rx_buffer_len = 0; +bool bluetooth_rx_buffer_start_receving = false; bool bluetooth_rx_buffer_processing = false; bool flag = true; @@ -711,7 +712,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_ ***********************************************************************************************************************/ // static bool IRAM_ATTR timer_group_isr_callback(void *args) // { -// bluetooth_rx_buffer_processing = true; +// bluetooth_rx_buffer_start_receving = true; // return pdFALSE; // } @@ -873,24 +874,25 @@ void bluetooth_gatts_try_process_data() cJSON *json_tmp; cJSON *ch; //接收完毕 - if (bluetooth_rx_buffer_processing) + if (bluetooth_rx_buffer_start_receving) { //开启定时器 - port_timer_delay_ms(100); + port_timer_delay_ms(200); + bluetooth_rx_buffer_processing = true; //打印输出 ESP_LOGI(GATTS_TAG, "%s", &bluetooth_rx_buffer[0]); - //处理数据 + //解析数据 json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]); + + //验证解析数据是否正确 if (json_tmp == NULL) { ESP_LOGE("ERROR", "json_tmp null"); cJSON_Delete(json_tmp); buffer_all_init(); + bluetooth_rx_buffer_start_receving = false; bluetooth_rx_buffer_processing = false; - while (1) - { - } return; } ch = json_tmp->child; @@ -900,37 +902,33 @@ void bluetooth_gatts_try_process_data() ESP_LOGI(GATTS_TAG, "%s", ch->string); if (strcmp(ch->string, "order") == 0) { - ESP_LOGI("!!!!!!!!", "##############"); + ESP_LOGI("receve", "order"); + } + if (strcmp(ch->string, "index") == 0) + { + ESP_LOGI("receve", "index"); + } + if (strcmp(ch->string, "speedLevel") == 0) + { + ESP_LOGI("receve", "speedLevel"); + } + if (strcmp(ch->string, "position") == 0) + { + ESP_LOGI("receve", "position"); + } + if (strcmp(ch->string, "direction") == 0) + { + ESP_LOGI("receve", "direction"); } ch = ch->next; } - // if (validation_param(json_tmp, "order")) - // { - // ESP_LOGI("JSON", "order value:%s", cJSON_GetObjectItem(json_tmp, "order")->valuestring); - // } - // if (validation_param(json_tmp, "index")) - // { - // ESP_LOGI("JSON", "index value:%d", cJSON_GetObjectItem(json_tmp, "index")->valueint); - // } - // if (validation_param(json_tmp, "speedLevel")) - // { - // ESP_LOGI("JSON", "speedLevel value:%d", cJSON_GetObjectItem(json_tmp, "speedLevel")->valueint); - // } - // if (validation_param(json_tmp, "position")) - // { - // ESP_LOGI("JSON", "position value:%lf", cJSON_GetObjectItem(json_tmp, "position")->valuedouble); - // } - // if (validation_param(json_tmp, "direction")) - // { - // ESP_LOGI("JSON", "direction value:%d", cJSON_GetObjectItem(json_tmp, "direction")->valueint); - // } - //释放空间 cJSON_Delete(json_tmp); // buffer置0 buffer_all_init(); //未在处理数据 + bluetooth_rx_buffer_start_receving = false; bluetooth_rx_buffer_processing = false; } } @@ -951,7 +949,7 @@ void port_timer_delay_ms(uint64_t delay) void start_receive_data_to_buffer(uint16_t length, uint8_t *value) { - bluetooth_rx_buffer_processing = true; + bluetooth_rx_buffer_start_receving = true; timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0); //判断是否buffer越界 if ((length + bluetooth_rx_buffer_len) > profile_b_buffer_size) @@ -959,9 +957,12 @@ void start_receive_data_to_buffer(uint16_t length, uint8_t *value) return; } - //写入到buffer - for (int i = 0; i < length; i++) + if (!bluetooth_rx_buffer_processing) { - bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = value[i]; + //写入到buffer + for (int i = 0; i < length; i++) + { + bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = value[i]; + } } } \ No newline at end of file