Skip to content

Commit

Permalink
feat: add command to read messages over UART0
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Dec 17, 2024
1 parent 37e259b commit 5a86b7b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
2 changes: 1 addition & 1 deletion firmware/components/uart_bridge/include/uart_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ esp_err_t uart_bridge_begin(int baud_rate, int buffer_size);
*
* @return ESP_OK on success, ESP_FAIL otherwise.
*/
esp_err_t uart_bridge_read(char* buffer, int buffer_size);
esp_err_t uart_bridge_read(char* buffer, int buffer_size, int timeout_ms);

/**
* @brief Write data to the UART bridge.
Expand Down
10 changes: 6 additions & 4 deletions firmware/components/uart_bridge/uart_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ esp_err_t uart_bridge_begin(int baud_rate, int buffer_size) {
return ESP_OK;
}

esp_err_t uart_bridge_read(char* buffer, int buffer_size) {
esp_err_t uart_bridge_read(char* buffer, int buffer_size, int timeout_ms) {
int bytes_read = uart_read_bytes(UART_NUM_0, (uint8_t*) buffer, buffer_size,
1000 / portTICK_PERIOD_MS);
timeout_ms / portTICK_PERIOD_MS);
if (bytes_read < 0) {
ESP_LOGE(TAG, "Failed to read data from UART (error code: %d)", bytes_read);
ESP_LOGE(TAG, "Failed to read data from UART, bytes read: %d", bytes_read);
return ESP_FAIL;
} else if (bytes_read == 0) {
ESP_LOGI(TAG, "No data read from UART");
return ESP_FAIL;
}

return ESP_OK;
}

Expand Down
12 changes: 6 additions & 6 deletions firmware/main/modules/cmd_control/cmd_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
static struct {
struct arg_str* app;
struct arg_end* end;
} launch_args;
} message_args;

static void launch_app_task(char* entry_cmd) {
menus_module_set_menu_over_cmd(entry_cmd);
vTaskDelete(NULL);
}

static int launch_app(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &launch_args);
int nerrors = arg_parse(argc, argv, (void**) &message_args);
xTaskCreate(launch_app_task, "launch_app_task", 4096,
launch_args.app->sval[0], 15, NULL);
message_args.app->sval[0], 15, NULL);
return 0;
}

void launch_cmd_register() {
launch_args.app = arg_str1(NULL, NULL, "<app>", "Name of the app to launch");
launch_args.end = arg_end(1);
message_args.app = arg_str1(NULL, NULL, "<app>", "Name of the app to launch");
message_args.end = arg_end(1);

esp_console_cmd_t launch_cmd = {.command = "launch",
.help = "Launch an app",
.hint = NULL,
.func = &launch_app,
.argtable = &launch_args};
.argtable = &message_args};

ESP_ERROR_CHECK(esp_console_cmd_register(&launch_cmd));
}
89 changes: 79 additions & 10 deletions firmware/main/modules/cmd_control/cmd_uart_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,102 @@
#include "menus_module.h"
#include "uart_bridge.h"

static const char* TAG = "cmd_uart_bridge";

static struct {
struct arg_str* message;
struct arg_end* end;
} launch_args;
} message_args;

static struct {
struct arg_str* buffer_size;
struct arg_int* timeout_ms;
struct arg_end* end;
} get_messages_args;

// Structure to hold parameters for get_messages_task
typedef struct {
const char* buffer_size;
int timeout_ms;
} get_messages_params_t;

static void send_message(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &launch_args);
int nerrors = arg_parse(argc, argv, (void**) &message_args);
if (nerrors != 0) {
arg_print_errors(stderr, launch_args.end, argv[0]);
arg_print_errors(stderr, message_args.end, argv[0]);
return;
}
assert(launch_args.message->count == 1);
const char* message = launch_args.message->sval[0];
assert(message_args.message->count == 1);
const char* message = message_args.message->sval[0];

uart_bridge_write(message, strlen(message));
}

static void get_messages_task(void* args) {
get_messages_params_t* params = (get_messages_params_t*) args;
const char* buffer_size = params->buffer_size;
const int timeout_ms = params->timeout_ms;

ESP_LOGI(TAG, "Buffer size: %s", buffer_size);
ESP_LOGI(TAG, "Timeout: %d", timeout_ms);

char buffer[atoi(buffer_size)];
while (true) {
esp_err_t err = uart_bridge_read(buffer, sizeof(buffer), timeout_ms);
if (err == ESP_OK) {
printf("Received: %s\n", buffer);

// Clear buffer
memset(buffer, 0, sizeof(buffer));
}
}
free(params);
}

static void get_messages(int argc, char** argv) {
int nerrors = arg_parse(argc, argv, (void**) &get_messages_args);
if (nerrors != 0) {
arg_print_errors(stderr, get_messages_args.end, argv[0]);
return;
}
assert(get_messages_args.buffer_size->count == 1);
assert(get_messages_args.timeout_ms->count == 1);
const char* buffer_size = get_messages_args.buffer_size->sval[0];
const int timeout_ms = *get_messages_args.timeout_ms->ival;

get_messages_params_t* params = malloc(sizeof(get_messages_params_t));
params->buffer_size = buffer_size;
params->timeout_ms = timeout_ms;

xTaskCreate(get_messages_task, "get_messages_task", 4096, (void*) params, 15,
NULL);
}

void register_uart_bridge_commands() {
launch_args.message = arg_str1(NULL, NULL, "<message>", "Message to send");
launch_args.end = arg_end(1);
message_args.message = arg_str1(NULL, NULL, "<message>", "Message to send");
message_args.end = arg_end(1);

esp_console_cmd_t launch_cmd = {
esp_console_cmd_t uart_bridge_print_cmd = {
.command = "print",
.help = "Send a message over external UART pins on the MININO",
.hint = NULL,
.func = &send_message,
.argtable = &launch_args};
.argtable = &message_args};

get_messages_args.buffer_size =
arg_str1(NULL, NULL, "<buffer_size>",
"Size in bytes of the buffer to read data into");
get_messages_args.timeout_ms = arg_int1(
NULL, NULL, "<timeout_ms>", "Timeout in milliseconds for reading data");
get_messages_args.end = arg_end(2);

esp_console_cmd_t uart_bridge_get_messages_cmd = {
.command = "get_messages",
.help = "Get messages from external UART pins on the MININO",
.hint = NULL,
.func = &get_messages,
.argtable = &get_messages_args};

ESP_ERROR_CHECK(esp_console_cmd_register(&launch_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&uart_bridge_print_cmd));
ESP_ERROR_CHECK(esp_console_cmd_register(&uart_bridge_get_messages_cmd));
}

0 comments on commit 5a86b7b

Please sign in to comment.