diff --git a/main/main.c b/main/main.c index ab90e3c..0e6fbb0 100644 --- a/main/main.c +++ b/main/main.c @@ -20,6 +20,8 @@ #define ble_uart_tx_size 128 #define ble_uart_rx_size 128 +#define construct_cmd_packet_buffer_size 256 + typedef struct { int index; const char *order; @@ -40,23 +42,23 @@ bleuart_t ble_uart_init_struct = { }; motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1}; -void order_receipt_setPosition(int index, uint8_t setPosition_code) { - uint8_t buffer[256] = {0}; +void construct_set_position_packet_and_report(int index, uint8_t setPosition_code) { + uint8_t buffer[construct_cmd_packet_buffer_size] = {0}; ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!"); if (setPosition_code == 0) { - sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index); + snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index); bleuart_notify_send(buffer, strlen((char *)buffer)); } else { - sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index); + snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index); bleuart_notify_send(buffer, strlen((char *)buffer)); } } void construct_get_status_packet_and_report(int index, double motor_position) { - uint8_t buffer[256] = {0}; - snprintf((char *)buffer, 256, + uint8_t buffer[construct_cmd_packet_buffer_size] = {0}; + snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"running\", \"deviceException\": 0, \"deviceExceptionInfo\": " ", \"position\":%lf }", index, motor_position); @@ -64,11 +66,11 @@ void construct_get_status_packet_and_report(int index, double motor_position) { bleuart_notify_send(buffer, strlen((char *)buffer)); } -void order_receipt_setMotorCurrentSize(int index) { - ESP_LOGI(MAIN_LOG_TAG, "Command setMotorCurrentSize execution complete!"); - uint8_t buffer[256] = {0}; +void construct_set_motorCurrentSize_packet_and_report(int index) { + uint8_t buffer[construct_cmd_packet_buffer_size] = {0}; - sprintf((char *)buffer, "{ \"order\": \"receipt\", \"index\": %d }", index); + snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"index\": %d }", index); + ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer); bleuart_notify_send(buffer, strlen((char *)buffer)); } @@ -94,7 +96,7 @@ void process_setPosition(RxContext_t *context, cJSON *rxjson) { } ESP_LOGI(MAIN_LOG_TAG, "index = %d,speedLevel = %d,position = %lf,direction = %d", index, speedLevel, position, direction); setPosition_code = motor_run_to_postion(1, position, 0); - order_receipt_setPosition(index, setPosition_code); + construct_set_position_packet_and_report(index, setPosition_code); } void process_getStatus(cJSON *rxjson) { @@ -114,7 +116,7 @@ void process_setMotorCurrentSize(cJSON *rxjson) { motor_set_zero_point(); - order_receipt_setMotorCurrentSize(index); + construct_set_motorCurrentSize_packet_and_report(index); } void processrxjson(cJSON *rxjson) { @@ -155,14 +157,14 @@ void motor_on_event(motor_event_t event) { //打算使用sprintf if (event == kRunToPosition) { ESP_LOGI(MAIN_LOG_TAG, "motor_on_event: kRunToPosition"); - uint8_t buffer[256] = {0}; + uint8_t buffer[construct_cmd_packet_buffer_size] = {0}; double motor_stop_position = motor_get_position_degree(); //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": ""} - sprintf((char *)buffer, - "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", // - /*index :*/ 1, // - /*position :*/ motor_stop_position // + snprintf((char *)buffer, construct_cmd_packet_buffer_size, + "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", // + /*index :*/ 1, // + /*position :*/ motor_stop_position // ); bleuart_notify_send(buffer, strlen((char *)buffer)); }