This is probably a noob question but I was messing with this for hours and could not find an answer online. I don’t know if I am searching for this correctly or what.
I was already coding in arduino for a while now and decided it was time to learn about libraries. I already started coding a few simple libraries of my own to experiment and I think I got the basic concept of it. I was looking through other libraries as examples to learn from and I came across something interesting when looking in the arduino SD Listfiles example. To my understanding, to use a class you have to create and object or an instance of it but I wonder how does this code work? It does not create an instance of the SDclass in the main file and yet it still works. I could not get this to work in my own library.
Listfiles
This example shows how print out the files in a
directory on a SD card
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 2 Feb 2014
by Scott Fitzgerald
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
File root;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
}
void loop() {
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
I was expecting to see something like this in the main file
SDClass SD;
I did dig around in the SD.h file and the only thing I came up with is
extern SDClass SD;
at line 122. I tried this in my own code but it did not work. Can someone please help me?
Again, I am very sorry if this is a beginner question but I am trying to get my head around how libraries work. Also, this is my first post so if I am doing something wrong, advice will be much appreciated.