#include "driver/gpio.h" #include "driver/uart.h" #include "esp_log.h" #include "freertos/idf_additions.h" #include "hal/uart_types.h" #include "nvs_flash.h" #include "portmacro.h" #include #include #include "uart_handler.h" #include "uart_prot.h" static const char *TAG = "ALOX - UART"; void init_uart() { uart_config_t uart_config = {.baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE}; uart_driver_install(MASTER_UART, BUF_SIZE * 2, 0, 0, NULL, 0); uart_param_config(MASTER_UART, &uart_config); uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); message_handler_init(); xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL); } void uart_read_task(void *param) { QueueHandle_t inputQueue = message_handler_get_input_queue(); uint8_t *data = (uint8_t *)malloc(BUF_SIZE); int len = 0; while (1) { len = 0; len = uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS)); if (len > 0) { for (int i = 0; i < len; ++i) { BaseType_t res = xQueueSend(inputQueue, &data[i], 0); if (res == errQUEUE_FULL) { ESP_LOGW(TAG, "inputQueue full"); } } } } } void uart_status_task(void *param) { while (1) { uart_write_bytes(MASTER_UART, "c1,status,0\n\r", sizeof("c1,status,0\n\r")); vTaskDelay(1000 / portTICK_PERIOD_MS); } } void send_client_info(int clientid, bool isAvailable, TickType_t lastPing) { char buf[128]; snprintf(buf, sizeof(buf), "c%d,status,2,%d,%u\r\n", clientid, isAvailable ? 1 : 0, (unsigned int)lastPing); uart_write_bytes(MASTER_UART, buf, strlen(buf)); } void esp_send_message_hook(ESPTOPCBaseMessage *msg) { // serialize + send via UART uint8_t buffer[128]; uart_write_bytes(UART_NUM_1, (const char *)buffer, sizeof(ESPTOPCBaseMessage)); }