You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
8.7 KiB
229 lines
8.7 KiB
#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"
|
|
#include "port.h"
|
|
|
|
#define MAIN_LOG_TAG "MAIN"
|
|
|
|
#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;
|
|
} RxContext_t;
|
|
|
|
static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
|
|
static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {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",
|
|
.ble_connect_flag = false,
|
|
};
|
|
motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
|
|
|
|
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) {
|
|
snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", //
|
|
/* code :*/ setPosition_code, //
|
|
/* index :*/ index //
|
|
);
|
|
bleuart_notify_send(buffer, strlen((char *)buffer));
|
|
} else {
|
|
snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", //
|
|
/* code :*/ setPosition_code, //
|
|
/* index :*/ index //
|
|
);
|
|
bleuart_notify_send(buffer, strlen((char *)buffer));
|
|
}
|
|
}
|
|
|
|
void construct_get_status_packet_and_report(int index, double motor_position) {
|
|
uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
|
|
if (motor_turning == false) {
|
|
snprintf((char *)buffer, construct_cmd_packet_buffer_size,
|
|
"{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"%s\", \"deviceException\": 0, \"deviceExceptionInfo\": "
|
|
", \"position\":%lf }",
|
|
/* index :*/ index, //
|
|
/* deviceState :*/ "idel", //
|
|
/* positon :*/ motor_position //
|
|
);
|
|
} else {
|
|
snprintf((char *)buffer, construct_cmd_packet_buffer_size,
|
|
"{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"%s\", \"deviceException\": 0, \"deviceExceptionInfo\": "
|
|
", \"position\":%lf }",
|
|
/* index :*/ index, //
|
|
/* deviceState :*/ "rotating", //
|
|
/* positon :*/ motor_position //
|
|
);
|
|
}
|
|
|
|
ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
|
|
bleuart_notify_send(buffer, strlen((char *)buffer));
|
|
}
|
|
|
|
void construct_set_motorCurrentSize_packet_and_report(int index) {
|
|
uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
|
|
|
|
snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"index\": %d }",
|
|
/* index :*/ index //
|
|
);
|
|
ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
|
|
bleuart_notify_send(buffer, strlen((char *)buffer));
|
|
}
|
|
|
|
void construct_stop_motor_packet_and_report(int index, bool stop_flag) {
|
|
uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
|
|
uint8_t *stop_flag_str = "null";
|
|
|
|
if (stop_flag) {
|
|
stop_flag_str = "successful";
|
|
} else {
|
|
stop_flag_str = "fail";
|
|
}
|
|
|
|
snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"index\": %d \"stopFlag\": %s}",
|
|
/* index :*/ index, //
|
|
/* stopFlag :*/ stop_flag_str //
|
|
);
|
|
ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
|
|
bleuart_notify_send(buffer, strlen((char *)buffer));
|
|
}
|
|
|
|
void process_setPosition(RxContext_t *context, cJSON *rxjson) {
|
|
// { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
|
|
int speedLevel = 0;
|
|
float position = 0;
|
|
int direction = 0;
|
|
uint8_t setPosition_code;
|
|
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
|
|
position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
|
|
}
|
|
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
|
|
speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
|
|
}
|
|
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
|
|
direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
|
|
}
|
|
ESP_LOGI(MAIN_LOG_TAG, "order = %s,index = %d,speedLevel = %d,position = %lf,direction = %d", context->order, context->index, speedLevel, position, direction);
|
|
setPosition_code = motor_run_to_postion(1, position, 0);
|
|
motor_turning = true;
|
|
construct_set_position_packet_and_report(context->index, setPosition_code);
|
|
}
|
|
|
|
void process_getStatus(RxContext_t *context, cJSON *rxjson) {
|
|
double motor_position = motor_get_position_degree();
|
|
construct_get_status_packet_and_report(context->index, motor_position);
|
|
}
|
|
|
|
void process_setMotorCurrentSize(RxContext_t *context, cJSON *rxjson) {
|
|
motor_set_zero_point();
|
|
construct_set_motorCurrentSize_packet_and_report(context->index);
|
|
}
|
|
|
|
void process_stopMotor(RxContext_t *context, cJSON *rxjson) {
|
|
bool motor_stop_flag;
|
|
motor_stop_flag = motor_stop();
|
|
construct_stop_motor_packet_and_report(context->index, motor_stop_flag);
|
|
}
|
|
|
|
void processrxjson(cJSON *rxjson) {
|
|
if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
|
|
return;
|
|
}
|
|
RxContext_t rxcontext = {0};
|
|
|
|
if (cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
|
|
rxcontext.order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
|
|
}
|
|
|
|
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
|
|
rxcontext.index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
|
|
}
|
|
|
|
if (strcmp(rxcontext.order, "setPosition") == 0) {
|
|
process_setPosition(&rxcontext, rxjson);
|
|
} else if (strcmp(rxcontext.order, "getStatus") == 0) {
|
|
process_getStatus(&rxcontext, rxjson);
|
|
} else if (strcmp(rxcontext.order, "setMotorCurrentSize") == 0) {
|
|
process_setMotorCurrentSize(&rxcontext, rxjson);
|
|
} else if (strcmp(rxcontext.order, "stopMotor") == 0) {
|
|
process_setMotorCurrentSize(&rxcontext, rxjson);
|
|
}
|
|
}
|
|
|
|
void blerxcb(uint8_t *rxmessage, size_t rxsize) {
|
|
rxmessage[rxsize] = 0;
|
|
cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
|
|
if (rxmessage_json == NULL) {
|
|
ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
|
|
return;
|
|
}
|
|
processrxjson(rxmessage_json);
|
|
cJSON_Delete(rxmessage_json);
|
|
}
|
|
|
|
void motor_on_event(motor_event_t event) {
|
|
//打算使用sprintf
|
|
if (event == kRunToPosition) {
|
|
ESP_LOGI(MAIN_LOG_TAG, "motor_on_event: kRunToPosition");
|
|
motor_turning = false;
|
|
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": ""}
|
|
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));
|
|
}
|
|
}
|
|
|
|
void app_main(void) {
|
|
bleuart_init(&ble_uart_init_struct);
|
|
bleuart_reg_cb(blerxcb);
|
|
ble_set_motor_reg_cb(motor_run_to_postion);
|
|
|
|
motor_init(&ble_uart_motor_structer);
|
|
motor_reg_event_cb(motor_on_event);
|
|
|
|
gpio_output_debug_light_init();
|
|
|
|
while (true) {
|
|
if (ble_uart_init_struct.ble_connect_flag == true) {
|
|
port_do_debug_light_state(300);
|
|
} else {
|
|
port_do_debug_light_state(1000);
|
|
}
|
|
|
|
bleuart_schedule();
|
|
motor_module_schedule();
|
|
}
|
|
}
|