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.
147 lines
4.4 KiB
147 lines
4.4 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 KEY_INT1 27
|
|
#define KEY_INT2 14
|
|
#define DEBUG_LIGHT 12
|
|
#define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2))
|
|
#define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT))
|
|
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", "ble_uart_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, 0);
|
|
}
|
|
|
|
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", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": "", }
|
|
sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": 0, \"info\": \"success\", \"index\": %d }", 1);
|
|
bleuart_notify_send(buffer, strlen((char *)buffer));
|
|
}
|
|
}
|
|
|
|
void gpio_input_key_init() {
|
|
gpio_config_t gpio_grb_led_structer;
|
|
|
|
gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
|
|
gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
|
|
gpio_grb_led_structer.pin_bit_mask = GPIO_KEY_INPUT_PIN_SEL;
|
|
gpio_grb_led_structer.pull_down_en = 0;
|
|
gpio_grb_led_structer.pull_up_en = 0;
|
|
|
|
gpio_config(&gpio_grb_led_structer);
|
|
}
|
|
|
|
void gpio_output_debug_light_init() {
|
|
gpio_config_t gpio_grb_led_structer;
|
|
|
|
gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
|
|
gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT;
|
|
gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL;
|
|
gpio_grb_led_structer.pull_down_en = 0;
|
|
gpio_grb_led_structer.pull_up_en = 0;
|
|
|
|
gpio_config(&gpio_grb_led_structer);
|
|
}
|
|
|
|
void port_do_debug_light_state() {
|
|
static uint32_t debug_light_time;
|
|
if (port_haspassedms(debug_light_time) > 500) {
|
|
gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT));
|
|
debug_light_time = port_get_ticket();
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
gpio_output_debug_light_init();
|
|
while (true) {
|
|
port_do_debug_light_state();
|
|
bleuart_schedule();
|
|
motor_module_schedule();
|
|
}
|
|
}
|