SD card manager from microreader

This commit is contained in:
CaptainFrito
2025-12-04 19:20:21 +07:00
parent be0cb2bb34
commit 11b112da6f
3 changed files with 251 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
#ifndef SDCARD_MANAGER_H
#define SDCARD_MANAGER_H
#include <Arduino.h>
#include <vector>
class SDCardManager {
public:
SDCardManager(uint8_t epd_sclk, uint8_t sd_miso, uint8_t epd_mosi, uint8_t sd_cs, uint8_t eink_cs);
bool begin();
bool ready() const;
std::vector<String> listFiles(const char* path = "/", int maxFiles = 200);
// Read the entire file at `path` into a String. Returns empty string on failure.
String readFile(const char* path);
// Low-memory helpers:
// Stream the file contents to a `Print` (e.g. `Serial`, or any `Print`-derived object).
// Returns true on success, false on failure.
bool readFileToStream(const char* path, Print& out, size_t chunkSize = 256);
// Read up to `bufferSize-1` bytes into `buffer`, null-terminating it. Returns bytes read.
size_t readFileToBuffer(const char* path, char* buffer, size_t bufferSize, size_t maxBytes = 0);
// Write a string to `path` on the SD card. Overwrites existing file.
// Returns true on success.
bool writeFile(const char* path, const String& content);
// Ensure a directory exists, creating it if necessary. Returns true on success.
bool ensureDirectoryExists(const char* path);
private:
uint8_t epd_sclk;
uint8_t sd_miso;
uint8_t epd_mosi;
uint8_t sd_cs;
uint8_t eink_cs;
bool initialized = false;
};
#endif