My main question is if there is a way to read the response from ls into a char array.
If ls is called i.e. sd.ls("/", LS_SIZE | LS_R ); - the output is directed to Serial by default. Another way of calling would be including a stream sd.ls(&Serial, "/", LS_SIZE | LS_R ); - which also sends the output to the Serial stream. In this version another stream could be selected for the output i.e. Serial1.
However, this doesn't solve my problem. I'm using the Ethernet Shield with a MEGA 2560. To send the response to the client, I need to change SS. First reading from SD, then writing to W5100.
That means the data needs to be temporary stored.
The ideal solution would be to call the SdFat Classes printName and printFileSize from my own ls() and receiving the output into a char array.
#include <SPI.h> // Library fuer SPI Interface zum W5100 Ethernet Shield
#include "SdFat.h" // Library fuer die SD Karte auf dem W5100 Ethernet Shield
// file system object
SdFat sd;
SdFile file;
// On the Ethernet Shield, CS is pin 4. Note that even if it's not used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library functions will not work.
const int SDchipSelect = 4;
const int W5100chipSelect = 10;
// ############################################################################################################################
// ######################## Funktion enableSD #################################################################################
void enableSD() {
// Aktiviere SD (SDchipSelect LOW, W5100chipSelect HIGH)
digitalWrite(SDchipSelect,LOW);
digitalWrite(W5100chipSelect,HIGH);
} // End enableSD
// ############################################################################################################################
// ######################## Funktion enableW5100 ##############################################################################
void enableW5100() {
// Aktiviere W5100 (SDchipSelect HIGH, W5100chipSelect LOW)
digitalWrite(SDchipSelect,HIGH);
digitalWrite(W5100chipSelect,LOW);
} // End enableW5100
// ############################################################################################################################
// ######################## Funktion enableW5100 ##############################################################################
void my_ls(uint8_t indent) {
// Open next file in root. The volume working directory, vwd, is root.
// Warning, openNext starts at the current position of sd.vwd() so a
// rewind may be neccessary in your application.
sd.vwd()->rewind();
while (file.openNext(sd.vwd(), O_READ)) { // while file entries available
file.printName(&Serial);
file.printFileSize(&Serial);
Serial.write(' ');
if (file.isDir()) {
// Indicate a directory.
Serial.write('/');
}
Serial.println();
file.close();
} // End while file entries available
} // End Funktion my_ls
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// @@@@@@@@@@@@@@@@@@@@@@@@ Hauptfunktionen Setup und Loop @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// ############################################################################################################################
// ######################## Funktion Setup ####################################################################################
void setup() {
// start the serial library:
Serial.begin(9600);
// Chip Select Pins fuer SD und Ethernet definieren
pinMode(SDchipSelect,OUTPUT);
pinMode(W5100chipSelect,OUTPUT);
pinMode(53,OUTPUT);
Serial.print(F("Initializing SD card..."));
enableSD(); // Aktiviere SD (SDchipSelect LOW, W5100chipSelect HIGH)
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.begin(SDchipSelect, SPI_HALF_SPEED))
Serial.println(F("initialization failed!"));
else
Serial.println(F("initialization done."));
sd.ls("/", LS_SIZE | LS_R );
Serial.println(F("\n\nAnd now my ls:\n"));
my_ls(0);
} // End Funktion Setup
// ############################################################################################################################
// ######################## Loop Forever ######################################################################################
void loop() {
// put your main code here, to run repeatedly:
}
This code works fine, but the output is only on Serial. How can I get it redirected?
There are several ls() methods defined in the SdFat library. The one that you are using when you use ls("/", LS_SIZE | LS_R ); is the one defined in FatFileSystem.h, in the FatFileSystem class. That method calls the ls(&Serial, path, flags); variation, in the FatFile class.
You are free to copy the FatFile::ls() method's contents to your sketch, and store the data in an array, if that makes any sense.
However, this doesn't solve my problem. I'm using the Ethernet Shield with a MEGA 2560. To send the response to the client, I need to change SS. First reading from SD, then writing to W5100.
That means the data needs to be temporary stored.
It does NOT. Can't you get it through your head that the FatFile::ls() method takes a pointer to an instance of a class that derives from Print? Follow the inheritance path for the EthernetClient class.
EthernetClient derives from Client which derives from Stream which derives from Print.
So, you can call the FatFile::ls() method with an instance of the EthernetClient class, and it will output the data to the client, rather than the serial monitor.