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)