Cli: added uart tx buffering, removed debug logs

This commit is contained in:
DrZlo13 2021-12-03 17:16:41 +10:00
parent 8d4678d17b
commit 6884bfa2b1
2 changed files with 24 additions and 7 deletions

View File

@ -10,10 +10,15 @@
#define CLI_UART_CTS_PIN (UART_PIN_NO_CHANGE) #define CLI_UART_CTS_PIN (UART_PIN_NO_CHANGE)
#define CLI_UART_BAUD_RATE (115200) #define CLI_UART_BAUD_RATE (115200)
#define CLI_UART_BUF_SIZE (128) #define CLI_UART_BUF_SIZE (128)
#define CLI_UART_RX_BUF_SIZE (64)
static Cli* cli_uart; static Cli* cli_uart;
static QueueHandle_t cli_uart_queue; static QueueHandle_t cli_uart_queue;
static uint8_t cli_rx_buffer[CLI_UART_RX_BUF_SIZE];
static size_t cli_rx_index = 0;
static void cli_uart_write(const uint8_t* data, size_t data_size, void* context); static void cli_uart_write(const uint8_t* data, size_t data_size, void* context);
static void cli_uart_flush(void* context);
static void cli_uart_rx_task(void* pvParameters) { static void cli_uart_rx_task(void* pvParameters) {
uart_event_t event; uart_event_t event;
@ -47,6 +52,7 @@ static void cli_uart_rx_task(void* pvParameters) {
void cli_uart_init() { void cli_uart_init() {
cli_uart = cli_init(); cli_uart = cli_init();
cli_set_write_cb(cli_uart, cli_uart_write); cli_set_write_cb(cli_uart, cli_uart_write);
cli_set_flush_cb(cli_uart, cli_uart_flush);
uart_config_t uart_config = { uart_config_t uart_config = {
.baud_rate = CLI_UART_BAUD_RATE, .baud_rate = CLI_UART_BAUD_RATE,
@ -77,5 +83,20 @@ void cli_uart_init() {
} }
static void cli_uart_write(const uint8_t* data, size_t data_size, void* context) { static void cli_uart_write(const uint8_t* data, size_t data_size, void* context) {
uart_write_bytes(CLI_UART_PORT_NUM, data, data_size); for(size_t i = 0; i < data_size; i++) {
cli_rx_buffer[cli_rx_index] = data[i];
cli_rx_index++;
if(cli_rx_index == CLI_UART_RX_BUF_SIZE) {
cli_uart_flush(NULL);
}
}
} }
static void cli_uart_flush(void* context) {
if(cli_rx_index > 0) {
uart_write_bytes(CLI_UART_PORT_NUM, cli_rx_buffer, cli_rx_index);
}
cli_rx_index = 0;
}

View File

@ -60,14 +60,12 @@ void cli_set_flush_cb(Cli* cli, CliFlush flush_cb) {
} }
void cli_write(Cli* cli, const uint8_t* data, size_t data_size) { void cli_write(Cli* cli, const uint8_t* data, size_t data_size) {
ESP_LOGI("cli", "write");
if(cli->write_cb != NULL) { if(cli->write_cb != NULL) {
cli->write_cb(data, data_size, cli->context); cli->write_cb(data, data_size, cli->context);
} }
} }
void cli_flush(Cli* cli) { void cli_flush(Cli* cli) {
ESP_LOGI("cli", "flush");
if(cli->flush_cb != NULL) { if(cli->flush_cb != NULL) {
cli->flush_cb(cli->context); cli->flush_cb(cli->context);
} }
@ -149,7 +147,6 @@ static void cli_handle_backspace(Cli* cli) {
if(mstring_size(cli->line) > 0) { if(mstring_size(cli->line) > 0) {
// Other side // Other side
cli_write_str(cli, "\e[D\e[1P"); cli_write_str(cli, "\e[D\e[1P");
cli_flush(cli);
// Our side // Our side
mstring_set_strn(cli->line, mstring_get_cstr(cli->line), mstring_size(cli->line) - 1); mstring_set_strn(cli->line, mstring_get_cstr(cli->line), mstring_size(cli->line) - 1);
cli->cursor_position--; cli->cursor_position--;
@ -159,8 +156,6 @@ static void cli_handle_backspace(Cli* cli) {
} }
void cli_handle_char(Cli* cli, uint8_t c) { void cli_handle_char(Cli* cli, uint8_t c) {
ESP_LOGI("cli", "%c", c);
switch(c) { switch(c) {
case CliSymbolAsciiCR: case CliSymbolAsciiCR:
if(mstring_size(cli->line) == 0) { if(mstring_size(cli->line) == 0) {
@ -200,10 +195,11 @@ void cli_handle_char(Cli* cli, uint8_t c) {
cli_write_str(cli, "\e[4h"); cli_write_str(cli, "\e[4h");
cli_write_char(cli, c); cli_write_char(cli, c);
cli_write_str(cli, "\e[4l"); cli_write_str(cli, "\e[4l");
cli_flush(cli);
} }
break; break;
default: default:
break; break;
} }
cli_flush(cli);
} }