From 5de07ad8ad230cbb03a12b9fa25e9ac27e83359b Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Wed, 26 Feb 2020 07:11:57 -0800 Subject: [PATCH] first console code --- include/cmd_lora.h | 14 ++++++++ include/console.h | 5 +++ main/CMakeLists.txt | 6 ++-- main/cmd_lora.c | 43 ++++++++++++++++++++++++ main/console.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ main/main.c | 3 +- 6 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 include/cmd_lora.h create mode 100644 include/console.h create mode 100644 main/cmd_lora.c create mode 100644 main/console.c diff --git a/include/cmd_lora.h b/include/cmd_lora.h new file mode 100644 index 0000000..914d829 --- /dev/null +++ b/include/cmd_lora.h @@ -0,0 +1,14 @@ +#ifndef __CMD_LORA_H_ +#define __CMD_LORA_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void cmd_lora_register(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/console.h b/include/console.h new file mode 100644 index 0000000..a103499 --- /dev/null +++ b/include/console.h @@ -0,0 +1,5 @@ +#ifndef __CONSOLE_H__ +#define __CONSOLE_H__ +void console_task(void *args); +void console_init(); +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cf2c455..6065c0a 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,4 @@ -idf_component_register(SRCS "main.c" - INCLUDE_DIRS ".") +set(COMPONENT_SRCS "main.c console.c cmd_lora.c") +set(COMPONENT_ADD_INCLUDEDIRS ". ../include") + +register_component() diff --git a/main/cmd_lora.c b/main/cmd_lora.c new file mode 100644 index 0000000..e45e28d --- /dev/null +++ b/main/cmd_lora.c @@ -0,0 +1,43 @@ +#include +#include +#include "esp_log.h" +#include "esp_console.h" +#include "argtable3/argtable3.h" + +#include "esp32-lora.h" + +static struct { + struct arg_int *spreadfactor; + struct arg_end *end; + lora32_cfg_t *lora; +} sf_args; + +int set_sf(int argc, char **argv) { + int nerrors = arg_parse(argc, argv, (void **) &sf_args); + + if (nerrors != 0) { + arg_print_errors(stderr, sf_args.end, argv[0]); + + return 1; + } + + lora32_set_spreadfactor((lora32_cfg_t*)sf_args.lora, sf_args.spreadfactor->ival[0]); + + return 0; +} + +void cmd_lora_register(lora32_cfg_t *lora) { + sf_args.spreadfactor = arg_int0(NULL, NULL, "", "Spreadfactor"); + sf_args.end = arg_end(1); + sf_args.lora = lora; + + const esp_console_cmd_t sf_cmd = { + .command = "spreadfactor", + .help = "Set spreadfactor", + .hint = NULL, + .func = &set_sf, + .argtable = &sf_args + }; + + ESP_ERROR_CHECK(esp_console_cmd_register(&sf_cmd)); +} diff --git a/main/console.c b/main/console.c new file mode 100644 index 0000000..55858d9 --- /dev/null +++ b/main/console.c @@ -0,0 +1,81 @@ +#include +#include + +#include "esp_system.h" +#include "esp_log.h" +#include "esp_console.h" +#include "esp_vfs_dev.h" +#include "driver/uart.h" +#include "linenoise/linenoise.h" +#include "argtable3/argtable3.h" +#include "esp32-lora.h" + +#include "cmd_lora.h" + +// main console setup and task, assume mostly copied from example +void console_task(void *args) { + // disable buffering + setvbuf(stdin, NULL, _IONBF, 0); + + // setup line endings + esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); + esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); + + const uart_config_t uart_config = { + .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .use_ref_tick = true + }; + + ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config)); + + ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0)); + + esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + + esp_console_config_t console_config = { + .max_cmdline_args = 8, + .max_cmdline_length = 256, + .hint_color = atoi(LOG_COLOR_CYAN) + }; + + ESP_ERROR_CHECK(esp_console_init(&console_config)); + + linenoiseSetMultiLine(1); + linenoiseSetCompletionCallback(&esp_console_get_completion); + linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint); + linenoiseHistorySetMaxLen(100); + + cmd_lora_register(args); + + const char* prompt = LOG_COLOR_I "lorcomm> " LOG_RESET_COLOR; + + while(true) { + char* line = linenoise(prompt); + + if (line == NULL) { /* Ignore empty lines */ + continue; + } + + linenoiseHistoryAdd(line); + + int ret; + esp_err_t err = esp_console_run(line, &ret); + + if (err == ESP_ERR_NOT_FOUND) { + printf("Unrecognized command\n"); + } else if (err == ESP_ERR_INVALID_ARG) { + } else if (err == ESP_OK && ret != ESP_OK) { + printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err)); + } else if (err != ESP_OK) { + printf("Internal error: %s\n", esp_err_to_name(err)); + } + + linenoiseFree(line); + } +} + +void console_init() { +} diff --git a/main/main.c b/main/main.c index 46f823b..2a94189 100644 --- a/main/main.c +++ b/main/main.c @@ -8,6 +8,7 @@ #include "esp_debug_helpers.h" #include "esp32-lora.h" +#include "console.h" static const char *TAG = "lora-bigbin"; @@ -182,7 +183,7 @@ void app_main(void) { send_message(&lora, "an even, super duuper, very much, longer message. and some\0"); - vTaskDelay(1000 / portTICK_PERIOD_MS); + xTaskCreate(console_task, "console", 4048, &lora, tskIDLE_PRIORITY + 3, NULL); while(1) { vTaskDelay(100 / portTICK_PERIOD_MS);