医美代码重构
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.
 
 
 
 

203 lines
5.6 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 SUPPORT_KEY_INT 0
#define SUPPORT_ELECTRIC_RELAY 0
#define SUPPORT_DEBUG_LIGHT 1
#define ble_uart_tx_size 128
#define ble_uart_rx_size 128
#if SUPPORT_DEBUG_LIGHT
#define DEBUG_LIGHT 12
#define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT))
#endif
#if SUPPORT_KEY_INT
#define KEY_INT1 27
#define KEY_INT2 14
#define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2))
#endif
#if 0 //预留
#define TEMPER_LINE 18 //单总线温度传感器
#define FAN 19 //风扇控制
#endif
#if SUPPORT_ELECTRIC_RELAY
#define ELECTRIC_RELAY1 25
#define ELECTRIC_RELAY2 26
#define GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL ((1ULL << ELECTRIC_RELAY1) | (1ULL << ELECTRIC_RELAY2))
#endif
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[256] = {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\": \"\" }", //
1, motor_stop_position);
bleuart_notify_send(buffer, strlen((char *)buffer));
}
}
#if SUPPORT_KEY_INT
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);
}
#endif
#if SUPPORT_ELECTRIC_RELAY
void gpio_electric_relay_init() {
gpio_config_t gpio_grb_led_structer;
gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
gpio_grb_led_structer.pin_bit_mask = GPIO_ELECTRIC_RELAY_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);
}
#endif
#if SUPPORT_DEBUG_LIGHT
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();
}
}
#endif
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);
#if SUPPORT_DEBUG_LIGHT
gpio_output_debug_light_init();
#endif
while (true) {
#if SUPPORT_DEBUG_LIGHT
port_do_debug_light_state();
#endif
bleuart_schedule();
motor_module_schedule();
}
}