diff --git a/components/console_wrapper/CMakeLists.txt b/components/console_wrapper/CMakeLists.txt new file mode 100644 index 0000000..7ed1f25 --- /dev/null +++ b/components/console_wrapper/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "console.c" + INCLUDE_DIRS "." + REQUIRES console esp_system +) diff --git a/components/console_wrapper/console.c b/components/console_wrapper/console.c new file mode 100644 index 0000000..b78c09e --- /dev/null +++ b/components/console_wrapper/console.c @@ -0,0 +1,63 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#include "console.h" + +#include "esp_console.h" +#include "esp_log.h" +#include "esp_system.h" + +#define TAG "Console" + +static esp_console_repl_t *repl; + +static int reboot_command_func(int argc, char **argv) +{ + (void)argc; + (void)argv; + (void)esp_restart(); + return 0; +} + +void console_init(void) +{ + esp_err_t error; + + error = esp_console_register_help_command(); + if (error != ESP_OK) + ESP_LOGE(TAG, "Error registering help command: %04x", error); + + esp_console_repl_config_t repl_config + = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); + repl_config.prompt = ">"; + esp_console_dev_uart_config_t uart_config + = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); + error = esp_console_new_repl_uart(&uart_config, &repl_config, &repl); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error initializing console REPL: %04x", error); + return; + } + + error = esp_console_start_repl(repl); + if (error != ESP_OK) + ESP_LOGE(TAG, "Error starting console REPL: %04x", error); + + console_register( + "reboot", "Reboot the system", "reboot", reboot_command_func); +} + +void console_register( + const char *name, const char *help, const char *hint, CommandFunc func) +{ + const esp_console_cmd_t command = { + .command = name, + .help = help, + .hint = hint, + .func = func, + }; + const esp_err_t error = esp_console_cmd_register(&command); + if (error != ESP_OK) + ESP_LOGE(TAG, "Error registering command %s: %04x", name, error); +} diff --git a/components/console_wrapper/console.h b/components/console_wrapper/console.h new file mode 100644 index 0000000..a7b0792 --- /dev/null +++ b/components/console_wrapper/console.h @@ -0,0 +1,25 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#ifndef CONSOLE_H +#define CONSOLE_H + +typedef int (*CommandFunc)(int argc, char **argv); + +/** + * Initialize and start the console. + */ +void console_init(void); + +/** + * Register a console command. + * + * The name, help and hint should all be null-terminated strings. Hint + * should list possible arguments. + */ +void console_register( + const char *name, const char *help, const char *hint, CommandFunc func); + +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 46a97e0..4f0dfa0 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "main.c" INCLUDE_DIRS "." - REQUIRES display settings + REQUIRES console_wrapper display settings ) diff --git a/main/main.c b/main/main.c index c8e9df8..4820186 100644 --- a/main/main.c +++ b/main/main.c @@ -3,11 +3,13 @@ * Copyright (c) Camden Dixie O'Brien */ +#include "console.h" #include "display.h" #include "settings.h" void app_main(void) { + console_init(); settings_init(); display_init(); }