|
|
@ -1,3 +1,5 @@ |
|
|
|
#include "cJSON.h" |
|
|
|
#include "cJSON_Utils.h" |
|
|
|
#include "driver/gpio.h" |
|
|
|
#include "driver/timer.h" |
|
|
|
#include "esp_log.h" |
|
|
@ -11,6 +13,8 @@ |
|
|
|
#include "ble_spp_server_demo.h" |
|
|
|
#include "motor_drive.h" |
|
|
|
|
|
|
|
#define MAIN_LOG_TAG "MAIN" |
|
|
|
|
|
|
|
#define ble_uart_tx_size 128 |
|
|
|
#define ble_uart_rx_size 128 |
|
|
|
|
|
|
@ -33,6 +37,46 @@ bleuart_t ble_uart_init_struct = { |
|
|
|
.bleName = "yimei_ble", |
|
|
|
}; |
|
|
|
|
|
|
|
static cJSON *json_tmp; |
|
|
|
// static double encoder_befor_num; |
|
|
|
#define set_position "setPosition" |
|
|
|
#define get_status "getStatus" |
|
|
|
#define set_motor_current_size "setMotorCurrentSize" |
|
|
|
#define set_motor_to_position "setMotorToPosition" |
|
|
|
#define cmd_length_set_position 5 |
|
|
|
#define cmd_length_get_status 2 |
|
|
|
#define cmd_length_set_motor_current_size 2 |
|
|
|
#define cmd_length_set_motor_to_position 5 |
|
|
|
typedef struct { |
|
|
|
char *order; //指令名称 |
|
|
|
int index; // |
|
|
|
int speedLevel; // |
|
|
|
double position; //角度 |
|
|
|
int direction; //旋转方向 |
|
|
|
int code; //错误码 |
|
|
|
char *info; //错误码信息 |
|
|
|
char *deviceState; //设备状态 |
|
|
|
int deviceException; //设备异常编号 |
|
|
|
char *deviceExceptionInfo; //设备异常信息 |
|
|
|
|
|
|
|
bool cmd_flag; |
|
|
|
} ble_uart_receive_data_t; |
|
|
|
|
|
|
|
ble_uart_receive_data_t ble_uart_receive_data = { |
|
|
|
.order = "order", |
|
|
|
.index = 0, |
|
|
|
.speedLevel = 0, |
|
|
|
.position = 0.0, |
|
|
|
.direction = 0, |
|
|
|
.code = 0, |
|
|
|
.info = "noerror", |
|
|
|
.deviceState = "init", |
|
|
|
.deviceException = 0, |
|
|
|
.deviceExceptionInfo = "noexception", |
|
|
|
|
|
|
|
.cmd_flag = false, |
|
|
|
}; |
|
|
|
|
|
|
|
void blerxcb(uint8_t *rxmessage, size_t rxsize) { |
|
|
|
if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) { |
|
|
|
ESP_LOGW("MAIN", "rx buffer overstep"); |
|
|
@ -57,26 +101,128 @@ void wait_ble_uart_receive_ms(uint16_t wait_time_ms) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void app_main(void) { |
|
|
|
ble_spp_server_demo_app_main(&ble_uart_init_struct); |
|
|
|
bleuart_reg_cb(blerxcb); |
|
|
|
|
|
|
|
while (true) { |
|
|
|
if (ble_rx_buffer_off != 0) { |
|
|
|
modbus_uart_rx_off_before = ble_rx_buffer_off; |
|
|
|
bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) { |
|
|
|
*json_tmp = cJSON_Parse((char *)ble_uart_init_struct.rxbuf); |
|
|
|
if (*json_tmp == NULL) { |
|
|
|
ESP_LOGE(MAIN_LOG_TAG, "parse rxbuffer null or redundant symbol ',','{' "); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
wait_ble_uart_receive_ms(100); |
|
|
|
bool parse_json_to_struct(cJSON *ch) { |
|
|
|
uint8_t cmd_length = 0; |
|
|
|
while (ch != NULL) { |
|
|
|
// ESP_LOGI(MAIN_LOG_TAG, "%s", ch->string); |
|
|
|
if (strcmp(ch->string, "order") == 0) { |
|
|
|
ble_uart_receive_data.order = ch->valuestring; |
|
|
|
|
|
|
|
if (ble_rx_buffer_off == modbus_uart_rx_off_before) { |
|
|
|
ble_uart_init_struct.receive_data_processing_flag = true; |
|
|
|
ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf); |
|
|
|
if (strcmp(ch->valuestring, set_position) == 0) { |
|
|
|
cmd_length = cmd_length_set_position; |
|
|
|
} |
|
|
|
if (strcmp(ch->valuestring, get_status) == 0) { |
|
|
|
cmd_length = cmd_length_get_status; |
|
|
|
} |
|
|
|
if (strcmp(ch->valuestring, set_motor_current_size) == 0) { |
|
|
|
cmd_length = cmd_length_set_motor_current_size; |
|
|
|
} |
|
|
|
if (strcmp(ch->valuestring, set_motor_to_position) == 0) { |
|
|
|
cmd_length = cmd_length_set_motor_to_position; |
|
|
|
} |
|
|
|
|
|
|
|
if (ble_uart_init_struct.receive_data_processing_flag == true) { |
|
|
|
buffer_all_init(); |
|
|
|
ble_uart_init_struct.receive_data_processing_flag = false; |
|
|
|
ble_rx_buffer_off = 0; |
|
|
|
cmd_length--; |
|
|
|
} |
|
|
|
if (strcmp(ch->string, "index") == 0) { |
|
|
|
ble_uart_receive_data.index = ch->valueint; |
|
|
|
cmd_length--; |
|
|
|
} |
|
|
|
if (strcmp(ch->string, "speedLevel") == 0) { |
|
|
|
ble_uart_receive_data.speedLevel = ch->valueint; |
|
|
|
cmd_length--; |
|
|
|
} |
|
|
|
if (strcmp(ch->string, "position") == 0) { |
|
|
|
ble_uart_receive_data.position = ch->valuedouble; |
|
|
|
cmd_length--; |
|
|
|
} |
|
|
|
if (strcmp(ch->string, "direction") == 0) { |
|
|
|
ble_uart_receive_data.direction = ch->valueint; |
|
|
|
cmd_length--; |
|
|
|
} |
|
|
|
ch = ch->next; |
|
|
|
} |
|
|
|
|
|
|
|
if (cmd_length == 0) { |
|
|
|
ble_uart_receive_data.cmd_flag = true; |
|
|
|
} else { |
|
|
|
ESP_LOGE(MAIN_LOG_TAG, "JSON directive missing or exceeded"); |
|
|
|
} |
|
|
|
|
|
|
|
return ble_uart_receive_data.cmd_flag; |
|
|
|
} |
|
|
|
|
|
|
|
void processing_uart_rx_data() { |
|
|
|
if (ble_rx_buffer_off != 0) { |
|
|
|
modbus_uart_rx_off_before = ble_rx_buffer_off; |
|
|
|
|
|
|
|
wait_ble_uart_receive_ms(100); |
|
|
|
|
|
|
|
if (ble_rx_buffer_off == modbus_uart_rx_off_before) { |
|
|
|
ble_uart_init_struct.receive_data_processing_flag = true; |
|
|
|
ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf); |
|
|
|
if (parse_rxbuffer_and_validation_data(&json_tmp)) { |
|
|
|
if (parse_json_to_struct(json_tmp->child)) { |
|
|
|
ESP_LOGI(MAIN_LOG_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", ble_uart_receive_data.order, ble_uart_receive_data.index, ble_uart_receive_data.speedLevel, |
|
|
|
ble_uart_receive_data.position, ble_uart_receive_data.direction); |
|
|
|
// if (strcmp(ble_uart_receive_data.order, set_position) == 0) { |
|
|
|
// // ble_uart_receive_data.auto_report_flag = true; |
|
|
|
// ESP_LOGI(MAIN_LOG_TAG, set_position); |
|
|
|
|
|
|
|
// encoder_befor_num = motor_drive_read_encoder(); |
|
|
|
// if (encoder_befor_num >= 0) { |
|
|
|
// if (motor_drive_set_packages_ctr(ble_uart_receive_data.position, ble_uart_receive_data.direction) == 0) { |
|
|
|
// ets_delay_us(50000); |
|
|
|
// if (encoder_befor_num == motor_drive_read_encoder()) { |
|
|
|
// ESP_LOGW(MAIN_LOG_TAG, "motor no turning"); |
|
|
|
// } else { |
|
|
|
// ESP_LOGI(MAIN_LOG_TAG, "motor turning"); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
// // receipt_json_set_position(); |
|
|
|
// } |
|
|
|
// if (strcmp(ble_uart_receive_data.order, get_status) == 0) { |
|
|
|
// ESP_LOGI(MAIN_LOG_TAG, get_status); |
|
|
|
// receipt_json_get_status(); |
|
|
|
// } |
|
|
|
// if (strcmp(ble_uart_receive_data.order, set_motor_current_size) == 0) { |
|
|
|
// ESP_LOGI(MAIN_LOG_TAG, set_motor_current_size); |
|
|
|
// motor_drive_set_motor_current_size(); |
|
|
|
// // receipt_json_get_status(); |
|
|
|
// } |
|
|
|
// if (strcmp(ble_uart_receive_data.order, set_motor_to_position) == 0) { |
|
|
|
// ESP_LOGI(MAIN_LOG_TAG, set_motor_to_position); |
|
|
|
// motor_drive_set_motor_to_angle(ble_uart_receive_data.direction, ble_uart_receive_data.position, // |
|
|
|
// ble_uart_receive_data.speedLevel); |
|
|
|
// // receipt_json_get_status(); |
|
|
|
// } |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (ble_uart_init_struct.receive_data_processing_flag == true) { |
|
|
|
buffer_all_init(); |
|
|
|
ble_uart_init_struct.receive_data_processing_flag = false; |
|
|
|
ble_rx_buffer_off = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void app_main(void) { |
|
|
|
ble_spp_server_demo_app_main(&ble_uart_init_struct); |
|
|
|
bleuart_reg_cb(blerxcb); |
|
|
|
|
|
|
|
while (true) { |
|
|
|
processing_uart_rx_data(); |
|
|
|
} |
|
|
|
} |