first console code

This commit is contained in:
Morgan 'ARR\!' Allen 2020-02-26 07:11:57 -08:00
parent 871ef2c35f
commit 5de07ad8ad
6 changed files with 149 additions and 3 deletions

14
include/cmd_lora.h Normal file
View File

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

5
include/console.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef __CONSOLE_H__
#define __CONSOLE_H__
void console_task(void *args);
void console_init();
#endif

View File

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

43
main/cmd_lora.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
#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, "<sf>", "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));
}

81
main/console.c Normal file
View File

@ -0,0 +1,81 @@
#include <stdio.h>
#include <string.h>
#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() {
}

View File

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