From f19f43dce14cd614c325dcf323ae6cd5c94eaf41 Mon Sep 17 00:00:00 2001 From: MacDue Date: Wed, 2 Feb 2022 13:39:19 +0000 Subject: [PATCH 1/2] unity: Better readline for interactive test menu This commit replaces the use of esp_rom_uart_rx_string() with a new simple readline function that supports echoing back the input, along with backspaces. --- components/unity/unity_port_esp32.c | 34 ++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/components/unity/unity_port_esp32.c b/components/unity/unity_port_esp32.c index 752f8c3ac3..e31e05bd92 100644 --- a/components/unity/unity_port_esp32.c +++ b/components/unity/unity_port_esp32.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include +#include #include "unity.h" #include "sdkconfig.h" #include "hal/cpu_hal.h" @@ -28,6 +29,37 @@ void unity_flush(void) esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); } +#define iscontrol(c) ((c) <= '\x1f' || (c) == '\x7f') + +static void esp_unity_readline(char* dst, size_t len) +{ + /* Read line from console with support for echoing and backspaces */ + size_t write_index = 0; + for (;;) { + char c = 0; + bool got_char = esp_rom_uart_rx_one_char((uint8_t*)&c) == 0; + if (!got_char) { + continue; + } + if (c == '\r' || c == '\n') { + unity_putc('\n'); + dst[write_index] = '\0'; + return; + } else if (c == '\b') { + if (write_index > 0) { + write_index--; + // Delete previously entered character + esp_rom_uart_tx_one_char('\b'); + esp_rom_uart_tx_one_char(' '); + esp_rom_uart_tx_one_char('\b'); + } + } else if (write_index < len - 1 && !iscontrol(c)) { + unity_putc(c); + dst[write_index++] = c; + } + } +} + /* To start a unit test from a GDB session without console input, * - set a break at unity_gets ('hb unity_gets') * - resume the program ('c') @@ -56,7 +88,7 @@ void unity_gets(char *dst, size_t len) while (esp_rom_uart_rx_one_char(&ignore) == 0) { } /* Read input */ - esp_rom_uart_rx_string((uint8_t *) dst, len); + esp_unity_readline(dst, len); } void unity_exec_time_start(void) From ef25590277f831f763c175d57f5bc1563a3e8c50 Mon Sep 17 00:00:00 2001 From: MacDue Date: Thu, 3 Feb 2022 10:09:32 +0000 Subject: [PATCH 2/2] unity: unity_gets remove unnecessary UINT8_MAX clamp + extra comments --- components/unity/unity_port_esp32.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/components/unity/unity_port_esp32.c b/components/unity/unity_port_esp32.c index e31e05bd92..abb092aac2 100644 --- a/components/unity/unity_port_esp32.c +++ b/components/unity/unity_port_esp32.c @@ -42,18 +42,20 @@ static void esp_unity_readline(char* dst, size_t len) continue; } if (c == '\r' || c == '\n') { + /* Add null terminator and return on newline */ unity_putc('\n'); dst[write_index] = '\0'; return; } else if (c == '\b') { if (write_index > 0) { + /* Delete previously entered character */ write_index--; - // Delete previously entered character esp_rom_uart_tx_one_char('\b'); esp_rom_uart_tx_one_char(' '); esp_rom_uart_tx_one_char('\b'); } - } else if (write_index < len - 1 && !iscontrol(c)) { + } else if (len > 0 && write_index < len - 1 && !iscontrol(c)) { + /* Write a max of len - 1 characters to allow for null terminator */ unity_putc(c); dst[write_index++] = c; } @@ -79,14 +81,9 @@ void unity_gets(char *dst, size_t len) memset(unity_input_from_gdb, 0, sizeof(unity_input_from_gdb)); return; } - /* esp_rom_uart_rx_string length argument is uint8_t */ - if (len >= UINT8_MAX) { - len = UINT8_MAX; - } /* Flush anything already in the RX buffer */ uint8_t ignore; - while (esp_rom_uart_rx_one_char(&ignore) == 0) { - } + while (esp_rom_uart_rx_one_char(&ignore) == 0) { } /* Read input */ esp_unity_readline(dst, len); }