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".
This commit is contained in:
GlassOnTin 2026-03-31 14:00:56 +01:00
commit 9edf5a953c
2 changed files with 29 additions and 5 deletions

12
Gui.h
View file

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

View file

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