added timestamps and new print function

This commit is contained in:
Данила Горнушко 2023-11-28 13:37:17 +03:00
parent 09a14f390d
commit c85792c265
5 changed files with 36 additions and 17 deletions

View file

@ -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);
}

View file

@ -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

View file

@ -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");

View file

@ -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));
}
}

View file

@ -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