From 319e797132ca7812a82f9974f4454927bccfd4e5 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Wed, 22 Apr 2020 22:43:27 -0700 Subject: [PATCH] adding in stored config --- include/main.h | 8 ++++++++ main/console.c | 16 +++++++++------- main/main.c | 23 ++++++++++++++++++++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/main.h b/include/main.h index 71cddd1..e8d2cf1 100644 --- a/include/main.h +++ b/include/main.h @@ -1,9 +1,17 @@ #ifndef __MAIN_H_ #define __MAIN_H_ +#include "nvs_flash.h" + #include "esp32-lora.h" extern uint8_t mac[6]; extern lora32_cfg_t lora; +struct lorcomm_cfg { + lora32_cfg_t *lora; + nvs_handle_t *nvs_handle; +} lorcomm_cfg; + + #endif diff --git a/main/console.c b/main/console.c index 84dfcf2..b78f9e4 100644 --- a/main/console.c +++ b/main/console.c @@ -11,6 +11,7 @@ #include "esp32-lora.h" #include "cmd_lora.h" +#include "main.h" // main console setup and task, assume mostly copied from example void console_task(void *args) { @@ -49,16 +50,17 @@ void console_task(void *args) { linenoiseHistorySetMaxLen(100); esp_console_register_help_command(); - cmd_bw_register(args); - cmd_cr_register(args); - cmd_sf_register(args); - cmd_preamble_register(args); - cmd_send_register(args); - cmd_dump_register(args); + cmd_bw_register(lorcomm_cfg.lora); + cmd_cr_register(lorcomm_cfg.lora); + cmd_sf_register(lorcomm_cfg.lora); + cmd_preamble_register(lorcomm_cfg.lora); + cmd_send_register(lorcomm_cfg.lora); + cmd_dump_register(lorcomm_cfg.lora); - const char* prompt = LOG_COLOR_I "lorcomm> " LOG_RESET_COLOR; + char *prompt; while(true) { + asprintf(&prompt, LOG_COLOR_I "%s > " LOG_RESET_COLOR, "lorcomm"); char* line = linenoise(prompt); if (line == NULL) { /* Ignore empty lines */ diff --git a/main/main.c b/main/main.c index 2b40152..894dc9e 100644 --- a/main/main.c +++ b/main/main.c @@ -6,6 +6,8 @@ #include "driver/uart.h" #include "esp_log.h" #include "esp_debug_helpers.h" +#include "nvs_flash.h" +#include "esp_vfs_dev.h" #include "main.h" #include "lorcomm.h" @@ -87,6 +89,23 @@ receive_cleanup: } void app_main(void) { + esp_err_t err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK( err ); + + nvs_handle_t nvs_handle; + err = nvs_open("config", NVS_READWRITE, &nvs_handle); + if (err != ESP_OK) { + printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err)); + + lorcomm_cfg.nvs_handle = NULL; + } else { + lorcomm_cfg.nvs_handle = &nvs_handle; + } + esp_efuse_mac_get_default((uint8_t*)&mac); ESP_LOGI(TAG, "MAC: [%02X:%02X:%02X:%02X:%02X:%02X]", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); @@ -114,7 +133,9 @@ void app_main(void) { lora32_dump_regs(&lora); - xTaskCreate(console_task, "console", 4048, &lora, tskIDLE_PRIORITY + 3, NULL); + lorcomm_cfg.lora = &lora; + + xTaskCreate(console_task, "console", 4048, &lorcomm_cfg, tskIDLE_PRIORITY + 3, NULL); //lora32_send(&lora, (uint8_t*)"hi\0", 3);