esp_alox/main/uart_handler.c

57 lines
1.7 KiB
C

#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 <stdbool.h>
#include <string.h>
#include "uart_handler.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);
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
}
void uart_read_task(void *param) {
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) {
data[len] = '\0';
ESP_LOGI(TAG, "GOT UART DATA %s", data);
uart_write_bytes(MASTER_UART, data, len);
}
}
}
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));
}