|
|
#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}; 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", }; motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
void process_setPosition(cJSON *rxjson) { // { "order": "setPosition", "index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
int index = 0; int speedLevel = 0; float position = 0; int direction = 0; if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) { position = cJSON_GetObjectItem(rxjson, "position")->valuedouble; } if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) { index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index")); } 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")); } motor_run_to_postion(1, position, index); }
void processrxjson(cJSON *rxjson) { if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) { return; }
const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
if (strcmp(order, "setPosition") == 0) { process_setPosition(rxjson); } else if (strcmp(order, "getStatus") == 0) { } }
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, "kRunToPosition ok!");
uint8_t buffer[128] = {0}; //组包
//上报
//{ "order": "deviceStatusReport", "index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": "", }
sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": 0, \"info\": \"success\", \"index\": %d }", 0); bleuart_notify_send(buffer,sizeof(buffer)); } }
void app_main(void) { bleuart_init(&ble_uart_init_struct); bleuart_reg_cb(blerxcb);
motor_init(&ble_uart_motor_structer); motor_reg_event_cb(motor_on_event);
while (true) { bleuart_schedule(); motor_module_schedule(); } }
|