prepare to change prompt. a lot of fixes

This commit is contained in:
Данила Горнушко 2023-11-28 12:05:58 +03:00
parent 901c077207
commit 860bdd833d

View file

@ -8,6 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "esp_system.h" #include "esp_system.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h" #include "freertos/projdefs.h"
#include "linenoise/linenoise.h" #include "linenoise/linenoise.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
@ -17,6 +18,7 @@
#include "driver/usb_serial_jtag.h" #include "driver/usb_serial_jtag.h"
#include "cmd_system.h" #include "cmd_system.h"
#include "cmd_can.h" #include "cmd_can.h"
#include "can.h"
#include "fs.h" #include "fs.h"
#include "xvprintf.h" #include "xvprintf.h"
@ -27,49 +29,81 @@ static const bool use_colors = true;
#endif #endif
static const char* TAG = "console task"; static const char* TAG = "console task";
char prompt_buf[40];
esp_console_config_t console_config; esp_console_config_t console_config;
struct linenoiseState ls; struct linenoiseState ls;
char prompt[40]; SemaphoreHandle_t console_taken_sem;
static void get_prompt(char* prompt_buf) { static void update_prompt() {
static const char* text = "can_wizard > "; static char* text;
static const char* prompt_color = LOG_COLOR_E; static char* prompt_color;
// memset(prompt_buf,0,strlen(prompt_buf)); switch (curr_can_state.state) {
case CAN_NOT_INSTALLED:
text = "not installed";
prompt_color = LOG_COLOR(LOG_COLOR_RED);
break;
case CAN_STOPPED:
text = "stopped";
prompt_color = LOG_COLOR(LOG_COLOR_RED);
break;
case CAN_ERROR_ACTIVE:
text = "error active";
prompt_color = LOG_COLOR(LOG_COLOR_GREEN);
break;
case CAN_ERROR_PASSIVE:
text = "error passive";
prompt_color = LOG_COLOR(LOG_COLOR_BROWN);
break;
case CAN_BUF_OFF:
text = "bus off";
prompt_color = LOG_COLOR(LOG_COLOR_RED);
break;
case CAN_RECOVERING:
text = "recovering";
prompt_color = LOG_COLOR(LOG_COLOR_RED);
break;
}
prompt_buf[0] = '\0'; prompt_buf[0] = '\0';
if (use_colors) { if (use_colors) {
strcat(prompt_buf, prompt_color); strcat(prompt_buf, prompt_color);
strcat(prompt_buf, text); strcat(prompt_buf, text);
strcat(prompt_buf, " > ");
strcat(prompt_buf, LOG_RESET_COLOR); strcat(prompt_buf, LOG_RESET_COLOR);
} else { } else {
strcat(prompt_buf, text); strcat(prompt_buf, text);
} }
ls.prompt = prompt_buf;
} }
void console_task_tx(void* arg) { void console_task_tx(void* arg) {
static const TickType_t prompt_timeout = pdMS_TO_TICKS(200);
// static const TickType_t prompt_timeout = portMAX_DELAY;
const int fd = fileno(stdout); const int fd = fileno(stdout);
char *msg_to_print; char *msg_to_print;
size_t msg_to_print_size; size_t msg_to_print_size;
while(1) { while(1) {
msg_to_print = (char *)xRingbufferReceive(can_messages, &msg_to_print_size, portMAX_DELAY); msg_to_print = (char *)xRingbufferReceive(can_messages, &msg_to_print_size, prompt_timeout);
// update_prompt();
xSemaphoreTake(console_taken_sem, portMAX_DELAY);
xSemaphoreTake(stdout_taken_sem, portMAX_DELAY);
linenoiseHide(&ls);
if (msg_to_print != NULL) { if (msg_to_print != NULL) {
xSemaphoreTake(stdout_taken_sem, portMAX_DELAY);
linenoiseHide(&ls);
// if zero-length string - just refresh prompt. used for updating prompt // if zero-length string - just refresh prompt. used for updating prompt
if(msg_to_print[0] != '\0') { if(msg_to_print[0] != '\0') {
write(fd, msg_to_print, msg_to_print_size); write(fd, msg_to_print, msg_to_print_size);
flushWrite(); flushWrite();
} else {
ls.prompt = "new prompt > ";
} }
linenoiseShow(&ls);
xSemaphoreGive(stdout_taken_sem);
vRingbufferReturnItem(can_messages, (void *) msg_to_print); vRingbufferReturnItem(can_messages, (void *) msg_to_print);
} }
linenoiseShow(&ls);
xSemaphoreGive(stdout_taken_sem);
xSemaphoreGive(console_taken_sem);
} }
} }
void console_task_interactive(void* arg) { void console_task_interactive(void* arg) {
console_taken_sem = xSemaphoreCreateMutex();
stdout_taken_sem = xSemaphoreCreateMutex(); stdout_taken_sem = xSemaphoreCreateMutex();
char *buf = calloc(1, console_config.max_cmdline_length); char *buf = calloc(1, console_config.max_cmdline_length);
char *line; char *line;
@ -90,23 +124,24 @@ void console_task_interactive(void* arg) {
"Use UP/DOWN arrows to navigate through command history.\n" "Use UP/DOWN arrows to navigate through command history.\n"
"Press TAB when typing command name to auto-complete.\n" "Press TAB when typing command name to auto-complete.\n"
"Ctrl+C will terminate the console environment.\n"); "Ctrl+C will terminate the console environment.\n");
get_prompt(prompt);
ls.buflen = console_config.max_cmdline_length; ls.buflen = console_config.max_cmdline_length;
ls.buf = buf; ls.buf = buf;
ls.prompt = prompt; update_prompt();
linenoiseEditStart(&ls); linenoiseEditStart(&ls);
xTaskCreate(console_task_tx, "console tsk tx", 5000, NULL, CONFIG_CONSOLE_TX_PRIORITY, NULL); xTaskCreate(console_task_tx, "console tsk tx", 5000, NULL, CONFIG_CONSOLE_TX_PRIORITY, NULL);
esp_log_set_vprintf(&vxprintf); esp_log_set_vprintf(&vxprintf);
while (true) { while (true) {
line = linenoiseEditFeed(&ls); line = linenoiseEditFeed(&ls);
if (line == linenoiseEditMore) continue; if (line == linenoiseEditMore) continue;
xSemaphoreTake(console_taken_sem, portMAX_DELAY);
linenoiseEditStop(&ls); linenoiseEditStop(&ls);
if (line == NULL) { /* Break on EOF or error */ if (line == NULL) { /* Break on EOF or error */
ESP_LOGE(TAG, "Ctrl+C???"); ESP_LOGE(TAG, "Ctrl+C???");
break; break;
} }
/* Add the command to the history if not empty*/ /* Add the command to the history if not empty*/
if (strlen(line) > 0) { if ((strlen(line) > 0) && (line[0] != '^')) {
linenoiseHistoryAdd(line); linenoiseHistoryAdd(line);
/* Save command history to filesystem */ /* Save command history to filesystem */
linenoiseHistorySave(HISTORY_PATH); linenoiseHistorySave(HISTORY_PATH);
@ -126,6 +161,7 @@ void console_task_interactive(void* arg) {
/* linenoise allocates line buffer on the heap, so need to free it */ /* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line); linenoiseFree(line);
linenoiseEditStart(&ls); linenoiseEditStart(&ls);
xSemaphoreGive(console_taken_sem);
} }
ESP_LOGE(TAG, "Restarting..."); ESP_LOGE(TAG, "Restarting...");