/
singlwolf
/
Radiola-2S3
Обзор
Документация
Войти
/
singlwolf
/
Radiola-2S3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/FSManager/src/fs_system.cpp
205 строк
4 KB
SinglWolf
Release 1.0.0
29 мар 2026, 14:18
29 мар 2026, 14:18
67f21c9
Код
Авторство
О чём код?
#include "fs_system.h" #include "FFat.h" #include "SD_MMC.h" #include <iomanip> #define SD_EJ 14 #define SD_CLK 11 #define SD_CMD 10 #define SD_D0 12 #define SD_D1 13 #define SD_D2 3 #define SD_D3 9 #define txtPrintInfo(fmt, ...) \ do \ { \ printf(LOG_COLOR_I); \ printf(fmt, ##__VA_ARGS__); \ printf(LOG_RESET_COLOR "\n"); \ } while (0) // #define txtPrintWarn(fmt, ...) \ do \ { \ printf(LOG_COLOR_W); \ printf(fmt, ##__VA_ARGS__); \ printf(LOG_RESET_COLOR "\n"); \ } while (0) // #define txtPrintError(fmt, ...) \ do \ { \ printf(LOG_COLOR_E); \ printf(fmt, ##__VA_ARGS__); \ printf(LOG_RESET_COLOR "\n"); \ } while (0) FS *FileFS = nullptr; bool sd = false; std::string TypeFS() { return sd ? "SD Card" : "FFat"; } std::string TotalFS() { if (sd) return humanReadableSize(SD_MMC.totalBytes()); else return humanReadableSize(FFat.totalBytes()); } std::string UsedFS() { if (sd) return humanReadableSize(SD_MMC.usedBytes()); else return humanReadableSize(FFat.usedBytes()); ; } std::string FreeFS() { if (sd) return humanReadableSize((SD_MMC.totalBytes() - SD_MMC.usedBytes())); else return humanReadableSize((FFat.totalBytes() - FFat.usedBytes())); } std::string humanReadableSize(size_t bytes) { auto res = static_cast<double>(bytes); std::string unit = " B"; if (bytes >= 1024 * 1024 * 1024) { res /= (1024.0 * 1024.0 * 1024.0); unit = " GB"; } else if (bytes >= 1024 * 1024) { res /= (1024.0 * 1024.0); unit = " MB"; } else if (bytes >= 1024) { res /= 1024.0; unit = " KB"; } std::stringstream ss; ss << std::fixed << std::setprecision(unit == " B" ? 0 : 2) << res << unit; return ss.str(); } SPIDog sdog; SPIDog::SPIDog() { _busy = false; } void SPIDog::begin() { if (_spiMutex == nullptr) { _spiMutex = xSemaphoreCreateMutex(); } if (_spiMutex == nullptr) { txtPrintError("Unable to create _Mutex!"); while (true) { delay(100); } } } bool SPIDog::init_fs_system() { pinMode(SD_EJ, INPUT_PULLUP); if (!digitalRead(SD_EJ)) { SD_MMC.setPins(SD_CLK, SD_CMD, SD_D0, SD_D1, SD_D2, SD_D3); if (!SD_MMC.begin("/sdcard", false, false, 40000, 15)) { txtPrintError("SD Card Mount Failed!"); SD_MMC.end(); } else { sd = true; uint8_t cardType = SD_MMC.cardType(); const char *_txt = "SD Card Type"; const char *type[] = {"CARD NONE", "MMC", "SDSC", "SDHC", "UNKNOWN"}; if ((cardType == CARD_NONE) || (cardType == CARD_UNKNOWN)) { txtPrintError("%s: %s", _txt, type[cardType]); sd = false; } else { txtPrintInfo("%s: %s", _txt, type[cardType]); } if (sd) { FileFS = &SD_MMC; } ////////////////////////////// } } else { txtPrintWarn("No SD Card attached!"); } if (!sd) { FileFS = &FFat; if (!FFat.begin(false, "/ffat", 15)) { txtPrintError("The partition for FFat was not found!"); while (true) { delay(100); } } } txtPrintWarn("%s Total:\t%s", TypeFS().c_str(), TotalFS().c_str()); txtPrintWarn("%s Used:\t%s", TypeFS().c_str(), UsedFS().c_str()); txtPrintWarn("%s Free:\t%s", TypeFS().c_str(), FreeFS().c_str()); return sd; } bool SPIDog::takeMutex() { if (_spiMutex == nullptr) { return false; } do { } while (xSemaphoreTake(_spiMutex, portMAX_DELAY) != pdPASS); _busy = true; return true; } void SPIDog::giveMutex() { if (_spiMutex != nullptr) { xSemaphoreGive(_spiMutex); } _busy = false; } bool SPIDog::canTake() { if (_spiMutex == nullptr) { return false; } return xSemaphoreTake(_spiMutex, 0) == pdPASS; } bool SPIDog::breakMutex(uint8_t ticks) { if (!_busy) { giveMutex(); vTaskDelay(ticks); return takeMutex(); } return false; } /////////////////////////////