SDFileIO class

A utility for safely operating on an SD card asynchronously over the SPI interface.

Constructors, destructors, conversion operators

SDFileIO(const char* prefix = "/sdcard") explicit
~SDFileIO()

Public functions

auto exists(const string& fileName) -> bool
auto isReady() -> bool
auto mount() -> bool
void printCardInfo()
template<class FStream>
auto openFile(const string& fileName, ios_base::openmode mode, std::function<bool(FStream&)>&& fileOp) -> bool
auto writeFile(const string& fileName, const vector<string>& data) -> bool
auto writeBinFile(const string& fileName, const string& data) -> bool
auto appendFile(const string& fileName, const vector<string>& data) -> bool
auto appendBinFile(const string& fileName, const string& data) -> bool
auto readFile(const string& fileName, string& data) -> bool
auto readBinFile(const string& fileName, string& data) -> bool

Function documentation

SDFileIO::SDFileIO(const char* prefix = "/sdcard") explicit

Parameters
prefix the SD card mount point

Explicit value constructor to specify the mount point of the SD card.

SDFileIO::~SDFileIO()

Destructor. Unmounts the SD card and disables the driver since it's no longer needed.

bool SDFileIO::exists(const string& fileName)

Parameters
fileName the name of the file
Returns whether the file exists

Checks if the specified file exists on the SD card.

bool SDFileIO::isReady()

Returns the ready state of this SD card

Whether the SD card is initialized and ready for file IO.

bool SDFileIO::mount()

Returns whether mount succeeded

Mounts the SD card and virtual file systems to be accessed.

void SDFileIO::printCardInfo()

Prints information about the SD card.

template<class FStream>
bool SDFileIO::openFile(const string& fileName, ios_base::openmode mode, std::function<bool(FStream&)>&& fileOp)

Template parameters
FStream the type of stream for the file
Parameters
fileName the name of the file
mode the first open mode
fileOp the operations to perform on the opened file
Returns whether all operations completed successfully

Opens a file with the specified file name and modes, then executes the provided operations on the file. After all operations are complete, the file gets closed safely to ensure all operations are synced with the virtual file system.

bool SDFileIO::writeFile(const string& fileName, const vector<string>& data)

Parameters
fileName the name of the file
data the data to write
Returns whether the data was successfully written

Writes the provided data to the file specified by file name. This method truncates the entire file upon opening so the only data that will be in the file is what was passed as a parameter here.

bool SDFileIO::writeBinFile(const string& fileName, const string& data)

Parameters
fileName the name of the file
data the data to write
Returns whether the data was successfully written

Writes the provided binary data to the file specified by file name. This method truncates the entire file upon opening so the only data that will be in the file is what was passed as a parameter here.

bool SDFileIO::appendFile(const string& fileName, const vector<string>& data)

Parameters
fileName the name of the file
data the data to append
Returns whether the data was successfully written

Appends the provided data to the file specified by file name.

bool SDFileIO::appendBinFile(const string& fileName, const string& data)

Parameters
fileName the name of the file
data the data to write
Returns whether the data was successfully written

Appends the provided binary data to the file specified by file name.

bool SDFileIO::readFile(const string& fileName, string& data)

Parameters
fileName the name of the file
data the destination
Returns whether the data was successfully read

Reads content from the specified file into the data parameter.

bool SDFileIO::readBinFile(const string& fileName, string& data)

Parameters
fileName the name of the file
data the destination
Returns whether the data was successfully read

Reads binary content from the specified file into the data parameter.