|
|
#include "cJSON.h"
#include "cJSON_Utils.h"
#include "driver/gpio.h"
#include "driver/timer.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "string.h"
//
#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
static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0}; static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
uint8_t ble_rx_buffer_now_len = 0; uint8_t ble_rx_buffer_off = 0; uint32_t start_time = 0; uint16_t modbus_uart_rx_off_before = 0;
bleuart_t ble_uart_init_struct = { .txbuf = bluetooth_tx_buffer, .txbufsize = sizeof(bluetooth_tx_buffer), .rxbuf = bluetooth_rx_buffer, .rxbufsize = sizeof(bluetooth_rx_buffer), .rxpacketReceiveOvertime = 200, .receive_data_processing_flag = false, .maxClientNum = 1, .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, };
motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
void buffer_all_init() { ble_rx_buffer_now_len = 0; memset(ble_uart_init_struct.rxbuf, 0, ble_uart_init_struct.rxbufsize); }
void blerxcb(uint8_t *rxmessage, size_t rxsize) { // start_time = esp_log_timestamp();
if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) { ESP_LOGW("MAIN", "rx buffer overstep"); buffer_all_init(); return; }
for (int i = 0; i < rxsize; i++) { ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i]; ble_rx_buffer_off++; } }
void wait_ble_uart_receive_ms(uint16_t wait_time_ms) { start_time = esp_log_timestamp(); while ((esp_log_timestamp() - start_time) < wait_time_ms) { } 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; }
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 (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; }
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 receipt_json_get_status() { cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
if (!pRoot) { return; }
cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
cJSON_AddNumberToObject(pRoot, "index", ble_uart_receive_data.index); cJSON_AddStringToObject(pRoot, "deviceState", ble_uart_receive_data.deviceState); cJSON_AddNumberToObject(pRoot, "deviceException", ble_uart_receive_data.deviceException); cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", ble_uart_receive_data.deviceExceptionInfo); cJSON_AddNumberToObject(pRoot, "position", ble_uart_receive_data.position);
char *szJson = cJSON_Print(pRoot);
if (szJson != NULL) { ESP_LOGI(MAIN_LOG_TAG, "%s", szJson); free(szJson); }
cJSON_Delete(pRoot); }
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(200);
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) { // ESP_LOGI(MAIN_LOG_TAG, "%.2lf", encoder_befor_num);
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_set_zero_point(); // 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_run_to_postion(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_rx_buffer_off = 0; ble_uart_init_struct.receive_data_processing_flag = false; } } }
void app_main(void) { ble_spp_server_demo_app_main(&ble_uart_init_struct); bleuart_reg_cb(blerxcb); motor_init(&ble_uart_motor_structer);
while (true) { processing_uart_rx_data(); } }
|