feat: add format function

This commit is contained in:
Stanislav Khromov 2026-01-14 01:37:17 +01:00
parent fe766f15cb
commit eff8126db8
2 changed files with 35 additions and 0 deletions

View File

@ -40,6 +40,14 @@ class SDCardManager {
bool openFileForWrite(const char* moduleName, const String& path, FsFile& file);
bool removeDir(const char* path);
/**
* Format the SD card as FAT32/exFAT (auto-selected based on size)
* WARNING: This erases ALL data on the card!
* @param pr Optional Print destination for progress output (e.g., &Serial)
* @return true on success, false on failure
*/
bool format(Print* pr = nullptr);
static SDCardManager& getInstance() { return instance; }
private:

View File

@ -275,3 +275,30 @@ bool SDCardManager::removeDir(const char* path) {
return sd.rmdir(path);
}
bool SDCardManager::format(Print* pr) {
if (!initialized) {
if (pr) pr->println("[SD] SDCardManager: not initialized");
return false;
}
Serial.printf("[%lu] [SD] Starting format...\n", millis());
if (pr) pr->println("[SD] Formatting card, please wait...");
// SdFat's format() method handles FAT16/FAT32/exFAT selection automatically
// based on card size (<=32GB = FAT, >32GB = exFAT)
bool success = sd.format(pr);
if (success) {
Serial.printf("[%lu] [SD] Format succeeded, re-initializing...\n", millis());
if (pr) pr->println("[SD] Format complete, remounting...");
// Re-initialize after format to mount the new filesystem
initialized = false;
success = begin();
} else {
Serial.printf("[%lu] [SD] Format failed\n", millis());
if (pr) pr->println("[SD] Format failed!");
}
return success;
}