|
@ -20,51 +20,8 @@ |
|
|
/*********************************************************************************************************************** |
|
|
/*********************************************************************************************************************** |
|
|
* ****************************************************user_define**************************************************** * |
|
|
* ****************************************************user_define**************************************************** * |
|
|
***********************************************************************************************************************/ |
|
|
***********************************************************************************************************************/ |
|
|
typedef struct bluetooth_processer |
|
|
|
|
|
{ |
|
|
|
|
|
char *bluetooth_processer_rx_buf; |
|
|
|
|
|
uint8_t bluetooth_processer_rx_buf_size; // |
|
|
|
|
|
|
|
|
|
|
|
int bluetooth_baundrate_one_packet_delay_ms; |
|
|
|
|
|
void (*port_delay_ms)(uint64_t us); |
|
|
|
|
|
|
|
|
|
|
|
char *order; //指令名称 |
|
|
|
|
|
int index; // |
|
|
|
|
|
int speed_level; // |
|
|
|
|
|
double position; //角度 |
|
|
|
|
|
int direction; //旋转方向 |
|
|
|
|
|
int code; //错误码 |
|
|
|
|
|
char *info; //错误码信息 |
|
|
|
|
|
char *deviceState; //设备状态 |
|
|
|
|
|
int deviceException; //设备异常编号 |
|
|
|
|
|
char *deviceExceptionInfo; //设备异常信息 |
|
|
|
|
|
|
|
|
|
|
|
bool cmd_flag; |
|
|
|
|
|
bool actively_report_flag; |
|
|
|
|
|
|
|
|
|
|
|
} bluetooth_processer_t; |
|
|
|
|
|
|
|
|
|
|
|
#define profile_b_buffer_size 128 |
|
|
|
|
|
char bluetooth_rx_buffer[profile_b_buffer_size] = {0}; |
|
|
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; |
|
|
|
|
|
uint32_t total = 0; |
|
|
|
|
|
#define timer_group0_interval_num 10000 |
|
|
|
|
|
// #define timer_group1_interval_num 5 |
|
|
|
|
|
#define timer_interval_s (uint32_t)1000000 |
|
|
|
|
|
#define timer_interval_ms (uint32_t)1000 |
|
|
|
|
|
#define timer_interval_us (uint32_t)1 |
|
|
|
|
|
#define kbluetooth_baundrate_one_packet_delay_ms 200 |
|
|
|
|
|
|
|
|
|
|
|
#define cmd_length_set_position 5 |
|
|
|
|
|
#define cmd_length_get_status 2 |
|
|
|
|
|
// #define cmd_length_device_status_report 6 |
|
|
|
|
|
|
|
|
|
|
|
#define set_position "setPosition" |
|
|
|
|
|
#define get_status "getStatus" |
|
|
|
|
|
|
|
|
|
|
|
ble_gatts_str_t ble_gatts_a_str = { |
|
|
ble_gatts_str_t ble_gatts_a_str = { |
|
|
.attr_handle = 0, |
|
|
.attr_handle = 0, |
|
@ -79,7 +36,6 @@ void buffer_all_init(); |
|
|
void bluetooth_gatts_try_process_data(); |
|
|
void bluetooth_gatts_try_process_data(); |
|
|
void timer_set_pause_and_counter_zero(int group, int timer); |
|
|
void timer_set_pause_and_counter_zero(int group, int timer); |
|
|
void port_timer_delay_ms(uint64_t delay); |
|
|
void port_timer_delay_ms(uint64_t delay); |
|
|
void start_receive_data_to_buffer(uint16_t length, uint8_t *value); |
|
|
|
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp); |
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp); |
|
|
bool parse_json_to_struct(cJSON *ch); |
|
|
bool parse_json_to_struct(cJSON *ch); |
|
|
void receipt_json_set_position(); |
|
|
void receipt_json_set_position(); |
|
@ -92,6 +48,9 @@ bluetooth_processer_t s_bluetooth_processer = { |
|
|
.bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms, |
|
|
.bluetooth_baundrate_one_packet_delay_ms = kbluetooth_baundrate_one_packet_delay_ms, |
|
|
.port_delay_ms = port_timer_delay_ms, |
|
|
.port_delay_ms = port_timer_delay_ms, |
|
|
|
|
|
|
|
|
|
|
|
.bluetooth_rx_buffer_start_receving = false, |
|
|
|
|
|
.bluetooth_rx_buffer_processing = false, |
|
|
|
|
|
|
|
|
.order = "order", |
|
|
.order = "order", |
|
|
.index = 0, |
|
|
.index = 0, |
|
|
.speed_level = 0, |
|
|
.speed_level = 0, |
|
@ -151,10 +110,10 @@ void timer_group_init(int group, int timer, bool auto_reload, int timer_interval |
|
|
***********************************************************************************************************************/ |
|
|
***********************************************************************************************************************/ |
|
|
void app_main(void) |
|
|
void app_main(void) |
|
|
{ |
|
|
{ |
|
|
esp_ble_gatts_init(&ble_gatts_a_str); |
|
|
|
|
|
|
|
|
esp_ble_gatts_init(&ble_gatts_a_str, &s_bluetooth_processer); |
|
|
// motor_init(); |
|
|
// motor_init(); |
|
|
// motor_encoder_init(); |
|
|
// motor_encoder_init(); |
|
|
// timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms); |
|
|
|
|
|
|
|
|
timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms); |
|
|
|
|
|
|
|
|
// uint8_t temp[5] = "hello"; |
|
|
// uint8_t temp[5] = "hello"; |
|
|
// esp_gatt_rsp_t rsp; |
|
|
// esp_gatt_rsp_t rsp; |
|
@ -173,18 +132,18 @@ void app_main(void) |
|
|
// ESP_LOGI(GATTS_TAG, "%d,%d,%d", ble_gatts_a_str.attr_handle, ble_gatts_a_str.send_conn_id, ble_gatts_a_str.send_gatts_if); |
|
|
// ESP_LOGI(GATTS_TAG, "%d,%d,%d", ble_gatts_a_str.attr_handle, ble_gatts_a_str.send_conn_id, ble_gatts_a_str.send_gatts_if); |
|
|
// esp_ble_gatts_send_indicate(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, |
|
|
// esp_ble_gatts_send_indicate(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, |
|
|
// ble_gatts_a_str.attr_handle, 5, temp, true); |
|
|
// ble_gatts_a_str.attr_handle, 5, temp, true); |
|
|
// esp_ble_gatts_send_response(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, 1, |
|
|
|
|
|
// ESP_GATT_OK, &rsp); |
|
|
|
|
|
// ets_delay_us(1000000); |
|
|
|
|
|
|
|
|
// esp_ble_gatts_send_response(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, 1, |
|
|
|
|
|
// ESP_GATT_OK, &rsp); |
|
|
|
|
|
// ets_delay_us(1000000); |
|
|
// } |
|
|
// } |
|
|
// esp_ble_gatts_set_attr_value(ble_gatts_a_str.constructor_param->read.handle, 5, temp); |
|
|
// esp_ble_gatts_set_attr_value(ble_gatts_a_str.constructor_param->read.handle, 5, temp); |
|
|
|
|
|
|
|
|
// bluetooth_gatts_try_process_data(); |
|
|
|
|
|
// if (s_bluetooth_processer.actively_report_flag) |
|
|
|
|
|
// { |
|
|
|
|
|
// receipt_json_get_status(); |
|
|
|
|
|
// s_bluetooth_processer.actively_report_flag = false; |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
bluetooth_gatts_try_process_data(); |
|
|
|
|
|
if (s_bluetooth_processer.actively_report_flag) |
|
|
|
|
|
{ |
|
|
|
|
|
receipt_json_get_status(); |
|
|
|
|
|
s_bluetooth_processer.actively_report_flag = false; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return; |
|
|
return; |
|
@ -193,11 +152,7 @@ void app_main(void) |
|
|
/*********************************************************************************************************************** |
|
|
/*********************************************************************************************************************** |
|
|
* *************************************************user_funtion_def************************************************** * |
|
|
* *************************************************user_funtion_def************************************************** * |
|
|
***********************************************************************************************************************/ |
|
|
***********************************************************************************************************************/ |
|
|
void buffer_all_init() |
|
|
|
|
|
{ |
|
|
|
|
|
bluetooth_rx_buffer_len = 0; |
|
|
|
|
|
memset(bluetooth_rx_buffer, 0, profile_b_buffer_size); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void timer_set_pause_and_counter_zero(int group, int timer) |
|
|
void timer_set_pause_and_counter_zero(int group, int timer) |
|
|
{ |
|
|
{ |
|
@ -225,12 +180,12 @@ void bluetooth_gatts_try_process_data() |
|
|
cJSON *json_tmp; |
|
|
cJSON *json_tmp; |
|
|
// cJSON *ch; |
|
|
// cJSON *ch; |
|
|
//开始接收 |
|
|
//开始接收 |
|
|
if (bluetooth_rx_buffer_start_receving) |
|
|
|
|
|
|
|
|
if (s_bluetooth_processer.bluetooth_rx_buffer_start_receving) |
|
|
{ |
|
|
{ |
|
|
//开启定时器 |
|
|
//开启定时器 |
|
|
s_bluetooth_processer.port_delay_ms(s_bluetooth_processer.bluetooth_baundrate_one_packet_delay_ms); |
|
|
s_bluetooth_processer.port_delay_ms(s_bluetooth_processer.bluetooth_baundrate_one_packet_delay_ms); |
|
|
// port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms); |
|
|
// port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms); |
|
|
bluetooth_rx_buffer_processing = true; |
|
|
|
|
|
|
|
|
s_bluetooth_processer.bluetooth_rx_buffer_processing = true; |
|
|
|
|
|
|
|
|
//打印输出 |
|
|
//打印输出 |
|
|
// ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf); |
|
|
// ESP_LOGI(GATTS_TAG, "%s", s_bluetooth_processer.bluetooth_processer_rx_buf); |
|
@ -269,8 +224,8 @@ void bluetooth_gatts_try_process_data() |
|
|
buffer_all_init(); |
|
|
buffer_all_init(); |
|
|
//未在处理数据 |
|
|
//未在处理数据 |
|
|
s_bluetooth_processer.cmd_flag = false; |
|
|
s_bluetooth_processer.cmd_flag = false; |
|
|
bluetooth_rx_buffer_start_receving = false; |
|
|
|
|
|
bluetooth_rx_buffer_processing = false; |
|
|
|
|
|
|
|
|
s_bluetooth_processer.bluetooth_rx_buffer_start_receving = false; |
|
|
|
|
|
s_bluetooth_processer.bluetooth_rx_buffer_processing = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -288,26 +243,6 @@ void port_timer_delay_ms(uint64_t delay) |
|
|
timer_pause(TIMER_GROUP_0, TIMER_0); |
|
|
timer_pause(TIMER_GROUP_0, TIMER_0); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void start_receive_data_to_buffer(uint16_t length, uint8_t *value) |
|
|
|
|
|
{ |
|
|
|
|
|
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) |
|
|
|
|
|
{ |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!bluetooth_rx_buffer_processing) |
|
|
|
|
|
{ |
|
|
|
|
|
//写入到buffer |
|
|
|
|
|
for (int i = 0; i < length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
bluetooth_rx_buffer[bluetooth_rx_buffer_len++] = value[i]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) |
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) |
|
|
{ |
|
|
{ |
|
|
*json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]); |
|
|
*json_tmp = cJSON_Parse(&bluetooth_rx_buffer[0]); |
|
|