From 88a585408a750f43654284220ac1f11028adae3c Mon Sep 17 00:00:00 2001 From: Robert McMahon Date: Fri, 19 Dec 2025 14:30:29 -0800 Subject: [PATCH] add gps to nvs --- components/app_console/app_console.c | 76 ++++++++++++++++++++++++++++ main/main.c | 45 +++++++++++----- 2 files changed, 107 insertions(+), 14 deletions(-) diff --git a/components/app_console/app_console.c b/components/app_console/app_console.c index 2cd1450..4c92e46 100644 --- a/components/app_console/app_console.c +++ b/components/app_console/app_console.c @@ -126,6 +126,81 @@ static void register_nvs_cmd(void) { ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } +// ============================================================================ +// COMMAND: gps (Configure GPS) +// ============================================================================ +static struct { + struct arg_lit *enable; + struct arg_lit *disable; + struct arg_lit *status; + struct arg_lit *help; + struct arg_end *end; +} gps_args; + +static int cmd_gps(int argc, char **argv) { + int nerrors = arg_parse(argc, argv, (void **)&gps_args); + if (nerrors > 0) { + arg_print_errors(stderr, gps_args.end, argv[0]); + return 1; + } + + if (gps_args.help->count > 0) { + printf("Usage: gps [--enable|--disable|--status]\n"); + return 0; + } + + nvs_handle_t h; + // Open 'storage' namespace where iperf/system settings live + esp_err_t err = nvs_open("storage", NVS_READWRITE, &h); + if (err != ESP_OK) { + printf("Error opening NVS: %s\n", esp_err_to_name(err)); + return 1; + } + + // Handle Actions + bool changed = false; + if (gps_args.enable->count > 0) { + nvs_set_u8(h, "gps_enabled", 1); + printf("GPS set to ENABLED. (Reboot required)\n"); + changed = true; + } else if (gps_args.disable->count > 0) { + nvs_set_u8(h, "gps_enabled", 0); + printf("GPS set to DISABLED. (Reboot required)\n"); + changed = true; + } + + // Commit if changed + if (changed) nvs_commit(h); + + // Read back status (Default is 1/True if key missing) + uint8_t val = 1; + if (nvs_get_u8(h, "gps_enabled", &val) != ESP_OK) val = 1; + + printf("GPS NVS State: %s\n", val ? "ENABLED" : "DISABLED"); + + nvs_close(h); + + // Refresh prompt dirty state if needed (though this setting requires reboot) + end_cmd(); + return 0; +} + +static void register_gps_cmd(void) { + gps_args.enable = arg_lit0(NULL, "enable", "Enable GPS"); + gps_args.disable = arg_lit0(NULL, "disable", "Disable GPS"); + gps_args.status = arg_lit0(NULL, "status", "Show Config"); + gps_args.help = arg_lit0("h", "help", "Help"); + gps_args.end = arg_end(2); + + const esp_console_cmd_t cmd = { + .command = "gps", + .help = "Configure GPS (Enable/Disable)", + .func = &cmd_gps, + .argtable = &gps_args + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); +} + // ============================================================================ // COMMAND: iperf // ============================================================================ @@ -447,4 +522,5 @@ void app_console_register_commands(void) { register_scan_cmd(); register_wifi_cmd(); register_nvs_cmd(); + register_gps_cmd(); } diff --git a/main/main.c b/main/main.c index c76de3e..57298b7 100644 --- a/main/main.c +++ b/main/main.c @@ -9,6 +9,7 @@ #include "esp_vfs_dev.h" #include "driver/uart.h" #include "nvs_flash.h" +#include "nvs.h" // Added for NVS read #include "esp_netif.h" #include "esp_event.h" @@ -34,11 +35,8 @@ static const char *TAG = "MAIN"; static char s_cli_prompt[32] = "esp32> "; // --- Prompt Updater --- -// This is called by app_console.c commands whenever a setting is changed. void app_console_update_prompt(void) { bool dirty = false; - - // Check if any component has unsaved changes (RAM != NVS) if (wifi_ctl_param_is_unsaved()) dirty = true; if (iperf_param_is_unsaved()) dirty = true; @@ -49,6 +47,21 @@ void app_console_update_prompt(void) { } } +// --- Helper: Check NVS for GPS Enable --- +static bool is_gps_enabled(void) { + nvs_handle_t h; + uint8_t val = 1; // Default to Enabled (1) + + // Check 'storage' namespace first (where iperf/system settings live) + if (nvs_open("storage", NVS_READONLY, &h) == ESP_OK) { + if (nvs_get_u8(h, "gps_enabled", &val) != ESP_OK) { + val = 1; // Key missing = Enabled + } + nvs_close(h); + } + return (val != 0); +} + // --- System Commands --- static int cmd_restart(int argc, char **argv) { @@ -93,13 +106,21 @@ void app_main(void) { // 2. Initialize Netif & Event Loop ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); - const gps_sync_config_t gps_cfg = { - .uart_port = UART_NUM_1, - .tx_pin = GPS_TX_PIN, - .rx_pin = GPS_RX_PIN, - .pps_pin = GPS_PPS_PIN, - }; - gps_sync_init(&gps_cfg, true); + + // ------------------------------------------------------------- + // GPS Initialization (Conditional) + // ------------------------------------------------------------- + if (is_gps_enabled()) { + const gps_sync_config_t gps_cfg = { + .uart_port = UART_NUM_1, + .tx_pin = GPS_TX_PIN, + .rx_pin = GPS_RX_PIN, + .pps_pin = GPS_PPS_PIN, + }; + gps_sync_init(&gps_cfg, true); + } else { + ESP_LOGW(TAG, "GPS initialization skipped (Disabled in NVS)"); + } // 3. Hardware Init status_led_init(RGB_LED_GPIO, HAS_RGB_LED); @@ -116,13 +137,9 @@ void app_main(void) { esp_console_repl_t *repl = NULL; esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); - // --------------------------------------------------------- - // CRITICAL FIX: Use the mutable buffer, NOT a string literal - // --------------------------------------------------------- repl_config.prompt = s_cli_prompt; repl_config.max_cmdline_length = 1024; - // Install UART driver for Console (Standard IO) esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));