From c85792c2658ce78c6136e0c9d9f4547e03d413af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=93=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D1=83=D1=88=D0=BA=D0=BE?= Date: Tue, 28 Nov 2023 13:37:17 +0300 Subject: [PATCH] added timestamps and new print function --- main/can.c | 20 +++++++++----------- main/can.h | 3 ++- main/cmd_can.c | 8 ++++---- main/xvprintf.c | 18 +++++++++++++++++- main/xvprintf.h | 4 ++++ 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/main/can.c b/main/can.c index fb5d9f8..20cc220 100644 --- a/main/can.c +++ b/main/can.c @@ -8,8 +8,6 @@ #include "freertos/ringbuf.h" #include "xvprintf.h" -static const char* LOG_TAG = "can"; - bool is_error_passive = false; SemaphoreHandle_t can_mutex; @@ -50,10 +48,10 @@ static can_status_t get_can_state() { return result; } -void can_msg_to_str(twai_message_t *can_msg, char *out_str) { +void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str) { char byte_str[3]; out_str[0] = '\0'; - sprintf(out_str, "can frame: ID: %08X dlc: %d ", (int) can_msg->identifier, can_msg->data_length_code); + sprintf(out_str, "%scan frame: ID: %08X dlc: %d ", start_str, (int) can_msg->identifier, can_msg->data_length_code); if (can_msg->data_length_code == 0) { strcat(out_str, "(no data)"); } else { @@ -67,11 +65,11 @@ void can_msg_to_str(twai_message_t *can_msg, char *out_str) { // TODO: add software filtering void can_task(void* arg) { - static const TickType_t can_task_timeout = pdMS_TO_TICKS(100); + static const TickType_t can_task_timeout = pdMS_TO_TICKS(10); uint32_t alerts = 0; can_mutex = xSemaphoreCreateMutex(); twai_message_t rx_msg; - char data_bytes_str[50]; + char data_bytes_str[70]; for (;;) { // A Task shall never return or exit. if (twai_read_alerts(&alerts, 0) == ESP_OK) { if (alerts & TWAI_ALERT_ERR_ACTIVE) { @@ -81,22 +79,22 @@ void can_task(void* arg) { is_error_passive = true; } if (alerts & TWAI_ALERT_BUS_ERROR) { - ESP_LOGE(LOG_TAG, "CAN error!"); + print_w_clr_time("CAN error!", LOG_COLOR_RED, false); } if (alerts & TWAI_ALERT_BUS_OFF) { - ESP_LOGE(LOG_TAG, "CAN went bus-off!"); + print_w_clr_time("CAN went bus-off!", LOG_COLOR_RED, false); // ESP_ERROR_CHECK(twai_initiate_recovery()); } if (alerts & TWAI_ALERT_BUS_RECOVERED) { - ESP_LOGI(LOG_TAG, "CAN recovered!"); + print_w_clr_time("CAN recovered!", LOG_COLOR_BLUE, false); // ESP_ERROR_CHECK(twai_start()); } } curr_can_state = get_can_state(); if (xSemaphoreTake(can_mutex, 0) == pdTRUE) { while (twai_receive(&rx_msg, 0) == ESP_OK) { - can_msg_to_str(&rx_msg, data_bytes_str); - xprintf(LOG_COLOR(LOG_COLOR_BLUE) "recv %s\n" LOG_RESET_COLOR, data_bytes_str); + can_msg_to_str(&rx_msg, "recv ", data_bytes_str); + print_w_clr_time(data_bytes_str, LOG_COLOR_BLUE, false); } xSemaphoreGive(can_mutex); } diff --git a/main/can.h b/main/can.h index ac81644..597090f 100644 --- a/main/can.h +++ b/main/can.h @@ -39,10 +39,11 @@ typedef struct { extern SemaphoreHandle_t can_mutex; extern volatile can_status_t curr_can_state; +extern bool timestamp_enabled; // functions void can_task(void* arg); -void can_msg_to_str(twai_message_t *can_msg, char *out_str); +void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str); #endif // MAIN_CAN_H diff --git a/main/cmd_can.c b/main/cmd_can.c index 7814dca..4c2a10b 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -41,7 +41,7 @@ static struct { static int send_can_frame(int argc, char **argv) { twai_message_t msg = {.extd = 1}; - char printf_str[50]; + char printf_str[70]; int nerrors = arg_parse(argc, argv, (void **) &cansend_args); if (nerrors != 0) { arg_print_errors(stderr, cansend_args.end, argv[0]); @@ -71,9 +71,9 @@ static int send_can_frame(int argc, char **argv) { msg.data_length_code = dt_l / 2; msg.identifier = msg_id; twai_transmit(&msg, pdMS_TO_TICKS(1000)); - can_msg_to_str(&msg, printf_str); - printf("sent %s\n", printf_str); - // free(can_msg_str_buf); + can_msg_to_str(&msg, "sent ", printf_str); + print_w_clr_time(printf_str, NULL, true); + free(can_msg_str_buf); return 0; invalid_args: printf("Invalid arguments!\n"); diff --git a/main/xvprintf.c b/main/xvprintf.c index 738c731..5e321eb 100644 --- a/main/xvprintf.c +++ b/main/xvprintf.c @@ -5,7 +5,7 @@ #include "esp_log.h" RingbufHandle_t can_messages; - +bool timestamp_enabled = true; void init_tx_ringbuf() { can_messages = xRingbufferCreate(2200, RINGBUF_TYPE_NOSPLIT); @@ -26,3 +26,19 @@ int xprintf(const char *fmt, ...) { va_start(args, fmt); return vxprintf(fmt, args); } + +int print_w_clr_time(char *msg, char *color, bool use_printf) { + print_func pr_func; + if (use_printf) pr_func = printf; + else pr_func = xprintf; + char timestamp[20]; + timestamp[0] = '\0'; + if (timestamp_enabled) { + snprintf(timestamp, 19, "[%s] ", esp_log_system_timestamp()); + } + if (color != NULL) { + return(pr_func("\033[0;%sm%s%s\033[0m\n", color, timestamp, msg)); + } else { + return(pr_func("%s%s\n", timestamp, msg)); + } +} diff --git a/main/xvprintf.h b/main/xvprintf.h index 5dc83df..d046896 100644 --- a/main/xvprintf.h +++ b/main/xvprintf.h @@ -3,11 +3,15 @@ #include "freertos/ringbuf.h" +typedef int (*print_func)(const char *fmt, ...); + extern RingbufHandle_t can_messages; +extern bool timestamp_enabled; void init_tx_ringbuf(); int vxprintf(const char *fmt, va_list args); int xprintf(const char *fmt, ...); +int print_w_clr_time(char *msg, char *color, bool use_printf); // functions