Skip to content

Instantly share code, notes, and snippets.

@SpComb
Created March 6, 2021 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpComb/a4ac76e63f835840720168136620542b to your computer and use it in GitHub Desktop.
Save SpComb/a4ac76e63f835840720168136620542b to your computer and use it in GitHub Desktop.
#include <FreeRTOS.h>
#include <driver/uart.h>
#include <esp_vfs_dev.h>
#include <stdio.h>
#define UART_RX_BUFFER_SIZE 256
#define UART_TX_BUFFER_SIZE 1024
#define UART_RX_LINE_ENDINGS_MODE ESP_LINE_ENDINGS_CRLF // convert CRLF to LF
#define UART_TX_LINE_ENDINGS_MODE ESP_LINE_ENDINGS_CRLF // convert LF to CRLF
void help()
{
printf(" help: Show commands\n");
}
void cli()
{
int c;
printf("> ");
while ((c = fgetc(stdin)) != EOF) {
// echo
fputc(c, stdout);
if (c == '\b') {
fprintf(stdout, " \b");
} else if (c == '\r') {
continue;
} else if (c == '\n') {
return help();
} else {
}
}
}
void app_main()
{
uart_port_t uart_port = CONFIG_ESP_CONSOLE_UART_NUM;
uart_config_t uart_config = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
};
esp_err_t err;
if ((err = uart_param_config(uart_port, &uart_config))) {
abort();
}
if ((err = uart_driver_install(uart_port, UART_RX_BUFFER_SIZE, UART_TX_BUFFER_SIZE, 0, NULL, 0))) {
abort();
}
// configure stdio to use buffered/interrupt-driven uart driver with CRLF <-> LF conversion
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
esp_vfs_dev_uart_set_rx_line_endings(UART_RX_LINE_ENDINGS_MODE);
esp_vfs_dev_uart_set_tx_line_endings(UART_TX_LINE_ENDINGS_MODE);
// unbuffered input
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IOLBF, 0);
while (true) {
cli();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment