Add a console with a reboot command

Couldn't just call the component "console" as that conflicted with the
ESP-IDF component, so opted for "console-wrapper".
This commit is contained in:
Camden Dixie O'Brien 2023-05-15 18:32:34 +01:00
parent e4fa25a61e
commit 0634787df3
5 changed files with 96 additions and 1 deletions

View File

@ -0,0 +1,5 @@
idf_component_register(
SRCS "console.c"
INCLUDE_DIRS "."
REQUIRES console esp_system
)

View File

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

View File

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

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "main.c" SRCS "main.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES display settings REQUIRES console_wrapper display settings
) )

View File

@ -3,11 +3,13 @@
* Copyright (c) Camden Dixie O'Brien * Copyright (c) Camden Dixie O'Brien
*/ */
#include "console.h"
#include "display.h" #include "display.h"
#include "settings.h" #include "settings.h"
void app_main(void) void app_main(void)
{ {
console_init();
settings_init(); settings_init();
display_init(); display_init();
} }