Hi, I apologise in advance if any of the terminology is incorrect.
I’m creating my first library that stores all the functions that I will ever need for manipulating a Micro SD card. I’ve been on this all day but when calling one of the functions it fails to open the file on the card. It’s the main one I will use countless times.
In the library I have included SD.h and FS.h. In the sketch I call a function called setupSDCard(). This calls multiple functions and does the following:
- Check that the card mounts successfully
- Gets the card type
- Lists the directory and removes hidden files produced by my MacBook
- Creates a Log.txt
- Appends text to the log file
- Relists the directory to make sure the hidden Mac files have been removed and the log file has been created.
void Lexi_SD_Helpers::setupSDCard() {
/* ---------------------------------------------------
Setup SD Card and files in readiness
---------------------------------------------------*/
SPI.begin(sck, miso, mosi, cs);
if (!SD.begin(cs)) {
Serial.println("Card mount failed");
return;
} else {
Serial.println("Card Mounted successfully");
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
} else {
Serial.println("Card detected");
}
Serial.print("Card type: ");
Serial.println(cardType);
//********** List directory and remove Mac hidden files **************
listDir(SD, "/", 0);
//********** Create Log file if it doesn't exist ***************
createFile(SD, "/Log.txt");
//********** Write to log **************
appendFile(SD, "/Log.txt", "\nWriting to the /Log.txt file");
//********** Relist directory **************
listDir(SD, "/", 0);
//******************** Test all the functions ************************
//testAllFunctions();
}
Now this works as expected. Creating the file, closing it and then reopening the file and adding text to the file. So the functions being called work within the library.
Card Mounted successfully
Card detected
Card type: 2
Card Mounted successfully
Card detected
Card type: 2
Listing directory: /
DIR : .Spotlight-V100
DIR : Logs
FILE: Log.txt SIZE: 291
DIR : .fseventsd
DIR : .Trashes
Creating Dir: /Logs
Dir created
Creating file: /Log.txt
File created
Appending to file: /Log.txt
Message appended
Listing directory: /
DIR : .Spotlight-V100
DIR : Logs
FILE: Log.txt SIZE: 320
DIR : .fseventsd
DIR : .Trashes
This is where it starts going wrong and is confusing the hell out of me. If I call this:
void Lexi_SD_Helpers::appendFile(fs::FS &fs, const char *path, const char *message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
file.close();
return;
}
if (file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
using the following from outside of the library, within my sketch, and it fails to open the file……. even though it worked using the setupSDCard() function within the library. I had to include the SD.h within the sketch because it was complaining about it.
mySD.appendFile(SD, "/Log.txt", "Test");
I get this error. This tells me that I am inside of the function in the library :
Appending to file: Log.txt
Failed to open file for appending
Now, I’ve tried multiple variations of the function. I tried this which also failed,
void Lexi_SD_Helpers::appendFile(const char *path, const char *message) {
Serial.printf("Appending to file: %s\n", path);
File file = SD.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
file.close();
return;
}
if (file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
I’m confused as to why the append function works within the library but fails when called from outside of the library.
Please bear in mind that this is my first attempt at doing this so be gentle with me. If it’s something really basic that I am missing then feel free to mock me ![]()
Any help would be appreciated.
