From 9edf5a953c98f16061c5a4c11b4cf408bf727b03 Mon Sep 17 00:00:00 2001 From: GlassOnTin Date: Tue, 31 Mar 2026 14:00:56 +0100 Subject: [PATCH] Add SD card file listing via debug command F Implements gui_list_files_fn callback to list SD card contents as JSON via the serial debug protocol. Replaces the stub that returned "use_log_command". --- Gui.h | 12 +++++++----- RNode_Firmware.ino | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Gui.h b/Gui.h index 4f9215e..dc1a265 100644 --- a/Gui.h +++ b/Gui.h @@ -158,6 +158,9 @@ extern volatile uint32_t imu_step_count; // Sensor logger toggle — set by .ino after IMULogger.h is included typedef bool (*gui_log_toggle_fn_t)(); static gui_log_toggle_fn_t gui_log_toggle_fn = NULL; +// SD file listing — set by .ino after SD is available +typedef void (*gui_list_files_fn_t)(); +static gui_list_files_fn_t gui_list_files_fn = NULL; static bool gui_imu_logging = false; // Forward declarations for IMULogger.h variables (defined later in compilation) extern bool imu_logging; @@ -1322,13 +1325,12 @@ static void gui_cmd_execute() { break; } - case 'F': { // List files on SD card (handled via function pointer) + case 'F': { // List files on SD card Serial.write(hdr, 4); - if (gui_log_toggle_fn) { - // Reuse the SD infrastructure from IMU logger - Serial.println("{\"error\":\"use_log_command\"}"); + if (gui_list_files_fn) { + gui_list_files_fn(); } else { - Serial.println("{\"error\":\"not_available\"}"); + Serial.println("{\"error\":\"no_sd\"}"); } Serial.flush(); break; diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 362e446..b38caa7 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -2236,6 +2236,28 @@ void loop() { return false; } }; + gui_list_files_fn = []() { + if (shared_spi_mutex) xSemaphoreTake(shared_spi_mutex, portMAX_DELAY); + SPI.begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS); + if (SD.begin(SD_CS, SPI, 4000000, "/sd", 5)) { + Serial.print("{\"files\":["); + File root = SD.open("/"); + bool first = true; + File f; + while ((f = root.openNextFile())) { + if (!first) Serial.print(","); + Serial.printf("{\"name\":\"%s\",\"size\":%lu}", f.name(), (unsigned long)f.size()); + first = false; + f.close(); + } + root.close(); + SD.end(); + Serial.println("]}"); + } else { + Serial.println("{\"error\":\"sd_init_failed\"}"); + } + if (shared_spi_mutex) xSemaphoreGive(shared_spi_mutex); + }; #endif // Enable step counter (low power, always-on)