Merge branch 'feat-sdcard-info' of https://github.com/jpirnay/crosspoint-reader into mybuild

This commit is contained in:
jpirnay
2026-03-17 21:27:10 +01:00
11 changed files with 228 additions and 9 deletions
+34
View File
@@ -4,6 +4,7 @@
#include <FS.h> // need to be included before SdFat.h for compatibility with FS.h's File class
#include <Logging.h>
#include <SDCardManager.h>
#include <SdFat.h>
#include <cassert>
@@ -11,6 +12,9 @@
HalStorage HalStorage::instance;
// Local SdFat instance for volume stats only
static SdFat halStorageSdFat;
HalStorage::HalStorage() {
storageMutex = xSemaphoreCreateMutex();
assert(storageMutex != nullptr);
@@ -54,6 +58,36 @@ bool HalStorage::writeFile(const char* path, const String& content) {
bool HalStorage::ensureDirectoryExists(const char* path) { HAL_STORAGE_WRAPPED_CALL(ensureDirectoryExists, path); }
uint64_t HalStorage::sdTotalBytes() const {
StorageLock lock;
// Try to initialize if not already
if (!halStorageSdFat.begin()) return 0;
auto* vol = halStorageSdFat.vol();
if (!vol) return 0;
return (uint64_t)vol->clusterCount() * vol->bytesPerCluster();
}
uint64_t HalStorage::sdUsedBytes() const {
StorageLock lock;
if (!halStorageSdFat.begin()) return 0;
auto* vol = halStorageSdFat.vol();
if (!vol) return 0;
const int32_t freeClusters = vol->freeClusterCount();
if (freeClusters < 0) return 0; // error reading FAT
const uint64_t clusterCount = vol->clusterCount();
uint64_t cappedFreeClusters = freeClusters < 0 ? 0 : (uint64_t)freeClusters;
if (cappedFreeClusters > clusterCount) cappedFreeClusters = clusterCount;
const uint64_t bytesPerCluster = vol->bytesPerCluster();
return (clusterCount - cappedFreeClusters) * bytesPerCluster;
}
uint64_t HalStorage::sdFreeBytes() const {
uint64_t total = sdTotalBytes();
uint64_t used = sdUsedBytes();
if (total <= used) return 0;
return total - used;
}
class HalFile::Impl {
public:
Impl(FsFile&& fsFile) : file(std::move(fsFile)) {}