add gps to nvs

This commit is contained in:
Robert McMahon 2025-12-19 14:30:29 -08:00
parent e5baa7cec5
commit 88a585408a
2 changed files with 107 additions and 14 deletions

View File

@ -126,6 +126,81 @@ static void register_nvs_cmd(void) {
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); 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 // COMMAND: iperf
// ============================================================================ // ============================================================================
@ -447,4 +522,5 @@ void app_console_register_commands(void) {
register_scan_cmd(); register_scan_cmd();
register_wifi_cmd(); register_wifi_cmd();
register_nvs_cmd(); register_nvs_cmd();
register_gps_cmd();
} }

View File

@ -9,6 +9,7 @@
#include "esp_vfs_dev.h" #include "esp_vfs_dev.h"
#include "driver/uart.h" #include "driver/uart.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "nvs.h" // Added for NVS read
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_event.h" #include "esp_event.h"
@ -34,11 +35,8 @@ static const char *TAG = "MAIN";
static char s_cli_prompt[32] = "esp32> "; static char s_cli_prompt[32] = "esp32> ";
// --- Prompt Updater --- // --- Prompt Updater ---
// This is called by app_console.c commands whenever a setting is changed.
void app_console_update_prompt(void) { void app_console_update_prompt(void) {
bool dirty = false; bool dirty = false;
// Check if any component has unsaved changes (RAM != NVS)
if (wifi_ctl_param_is_unsaved()) dirty = true; if (wifi_ctl_param_is_unsaved()) dirty = true;
if (iperf_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 --- // --- System Commands ---
static int cmd_restart(int argc, char **argv) { static int cmd_restart(int argc, char **argv) {
@ -93,13 +106,21 @@ void app_main(void) {
// 2. Initialize Netif & Event Loop // 2. Initialize Netif & Event Loop
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); 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, // GPS Initialization (Conditional)
.rx_pin = GPS_RX_PIN, // -------------------------------------------------------------
.pps_pin = GPS_PPS_PIN, if (is_gps_enabled()) {
}; const gps_sync_config_t gps_cfg = {
gps_sync_init(&gps_cfg, true); .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 // 3. Hardware Init
status_led_init(RGB_LED_GPIO, HAS_RGB_LED); 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_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); 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.prompt = s_cli_prompt;
repl_config.max_cmdline_length = 1024; 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_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)); ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));