barback32/main/console.c

82 lines
2.1 KiB
C
Raw Normal View History

2020-02-26 10:11:57 -05:00
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
2020-04-23 01:43:27 -04:00
#include "main.h"
2020-02-26 10:11:57 -05:00
// main console setup and task, assume mostly copied from example
void console_task(void *args) {
// disable buffering
setvbuf(stdin, NULL, _IONBF, 0);
// setup line endings
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
const 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,
.use_ref_tick = true
};
ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0));
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
esp_console_config_t console_config = {
.max_cmdline_args = 8,
.max_cmdline_length = 256,
.hint_color = atoi(LOG_COLOR_CYAN)
};
ESP_ERROR_CHECK(esp_console_init(&console_config));
linenoiseSetMultiLine(1);
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
linenoiseHistorySetMaxLen(100);
2020-04-23 01:21:29 -04:00
esp_console_register_help_command();
2020-02-26 10:11:57 -05:00
2020-04-23 01:43:27 -04:00
char *prompt;
2020-02-26 10:11:57 -05:00
while(true) {
2020-10-13 00:37:52 -04:00
asprintf(&prompt, LOG_COLOR_I "> " LOG_RESET_COLOR);
2020-04-24 15:56:14 -04:00
2020-02-26 10:11:57 -05:00
char* line = linenoise(prompt);
2020-04-24 15:56:14 -04:00
free(prompt);
2020-02-26 10:11:57 -05:00
if (line == NULL) { /* Ignore empty lines */
continue;
}
linenoiseHistoryAdd(line);
int ret;
esp_err_t err = esp_console_run(line, &ret);
if (err == ESP_ERR_NOT_FOUND) {
printf("Unrecognized command\n");
} else if (err == ESP_ERR_INVALID_ARG) {
} else if (err == ESP_OK && ret != ESP_OK) {
printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err));
} else if (err != ESP_OK) {
printf("Internal error: %s\n", esp_err_to_name(err));
}
linenoiseFree(line);
}
}