Go Down

Topic: Renaming files on the SD card (Read 7183 times) previous topic - next topic

feipoa

Ok.  I will try this.  To improve my understanding, could you point me to the documentation that states the functionality of all possible open flags?  So far, I've seen:

0_RDWR
0_CREAT
0_SYNC
0_AT_END
0_WRITE
0_APPEND

fat16lib

You need to read the html documentation.  Click on the classes tab.  Select the SdFile class.  Look at open().

Here is what you would find for open flags
Quote

    [in]   oflag   Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list

O_READ - Open for reading.

O_RDONLY - Same as O_READ.

O_WRITE - Open for writing.

O_WRONLY - Same as O_WRITE.

O_RDWR - Open for reading and writing.

O_APPEND - If set, the file offset shall be set to the end of the file prior to each write.

O_AT_END - Set the initial position at the end of the file.

O_CREAT - If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created

O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.

O_SYNC - Call sync() after each write. This flag should not be used with write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. These functions do character at a time writes so sync() will be called after each byte.

O_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.

feipoa

#17
Sep 24, 2011, 10:25 pm Last Edit: Sep 24, 2011, 10:30 pm by feipoa Reason: 1
Thank you for this.  But still, I still cannot get SdFat to work like SD for whatever reason.  While it works in the initial setup() it fails in loop().  It cannot seem to open or write to an SD file and even           ListFiles(client, LS_SIZE) fails to list the existing files on the SD card.  When switching back to SD lib, everything works.

Maybe the problem is obvious to you, or maybe I'm out of RAM.  The program doesn't hang though, it still continues to print to HTTP, but ListFIles doesn't work and nothing gets written to the card.

Code split into two continuous pieces as follows:

feipoa

#18
Sep 24, 2011, 10:27 pm Last Edit: Sep 25, 2011, 05:06 am by feipoa Reason: 1
Code: [Select]
#include <Wire.h>
#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>

Sd2Card card;
SdFat sd;
SdVolume volume;
SdFile root;
SdFile file;
SdFile dataFile;
SdFile now;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,20 };

Server server(80);

void setup() {

 if (!sd.init(SPI_HALF_SPEED, 4)) sd.initErrorHalt();
 volume.init(&card);            
 root.openRoot(&volume);
 Ethernet.begin(mac, ip);
 server.begin();  

}


feipoa

#19
Sep 24, 2011, 10:28 pm Last Edit: Sep 25, 2011, 05:08 am by feipoa Reason: 1
Code: [Select]
 //HTTP Server
 
 char clientline[40];  
 int index = 0;

 Client client = server.available();  
 if (client) {  
   boolean current_line_is_blank = true;
   index = 0;  
   
   while (client.connected()) {  
     if (client.available()) {  
       char c = client.read();
       
       if (c != '\n' && c != '\r') {  
         clientline[index] = c;
         index++;  
         if (index >= 40)  
           index = 40 - 1;  
         continue;  
       }
           
       clientline[index] = 0;
               
       if (strstr(clientline, "GET / ") != 0) {
         client.println("HTTP/1.1 200 OK");    
         client.println("Content-Type: text/html");
         client.println();
         client.print("<!DOCTYPE html>");
         client.print("Hello");
         client.println();
         client.println("<h3>Files:</h3>");

         ListFiles(client, LS_SIZE);

         client.print("</html>");
     }
       
     else if (strstr(clientline, "GET /") != 0) {  
       char *filename;
         
       filename = clientline + 5;
       (strstr(clientline, " HTTP"))[0] = 0;

       if (! file.open(&root, filename, O_READ)) {
         client.println("HTTP/1.1 404 Not Found");
         client.println("Content-Type: text/html");
         client.println();
         client.println("<h2>File Not Found!</h2>");
         break;  
       }
       
       client.println("HTTP/1.1 200 OK");
       client.println("Content-Type: text/plain");
       client.println();
         
       int16_t c;
       while ((c = file.read()) > 0 ) {  
           client.print((char)c);
       }
         
       file.close();
     }
     else {  
       client.println("HTTP/1.1 404 Not Found");
       client.println("Content-Type: text/html");
       client.println();
       client.println("<h2>File Not Found!</h2>");
     }
     break;  
   }  
 }
   delay(100);
   client.stop();
 }
}

void ListFiles(Client client, uint8_t flags) {
 dir_t p;
 root.rewind();
 client.println("<ul>");
 while (root.readDir(p) > 0) {
   if (p.name[0] == DIR_NAME_FREE) break;
   if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
   if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
   client.print("<li><a href=\"");
   for (uint8_t i = 0; i < 11; i++) {
     if (p.name[i] == ' ') continue;
     if (i == 8) {
       client.print('.');
     }
     client.print(p.name[i]);
   }
   client.print("\">");
   for (uint8_t i = 0; i < 11; i++) {
     if (p.name[i] == ' ') continue;
     if (i == 8) {
       client.print('.');
     }
     client.print(p.name[i]);
   }
   client.print("</a>");
   if (DIR_IS_SUBDIR(&p)) {
     client.print('/');
   }
   if (flags & LS_DATE) {
      root.printFatDate(p.lastWriteDate);
      client.print(' ');
      root.printFatTime(p.lastWriteTime);
   }
   if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
     client.print(' ');
     client.print(p.fileSize);
   }
   client.println("</li>");
 }
 client.println("</ul>");
}

fat16lib

You are not reading or looking at examples.

You must use:
Code: [Select]
SdFat sd;

Don't use any of this stuff in new versions of SdFat  when you use the above.
Code: [Select]

Sd2Card card;
SdVolume volume;
SdFile root;

Remove this from setup:
Code: [Select]
  volume.init(&card);             
  root.openRoot(&volume);

You are duplicating RAM use and functionality by putting in both.  Things won't be initialized correctly.

Look at the examples in the extras/examplesV1 folder and the SdFat/examples folder.



feipoa

#21
Sep 25, 2011, 01:39 am Last Edit: Sep 25, 2011, 02:02 am by feipoa Reason: 1
I did not find any example code on how to generate an http listing using sd.ls() from the new SdFat library.

The ListFiles(Client client, uint8_t flags) function that I'm using is from ladyada (http://www.ladyada.net/learn/arduino/ethfiles.html) and is derived using Sd2Card card; SdVolume volume; SdFile root.

Unless someone wants to volunteer some new example code, I am, for now, going to double-up on the use of
card.init(SPI_HALF_SPEED, 4))
and
sd.init(SPI_HALF_SPEED, 4))

So far things are working correctly.

fat16lib

ladyada used an older version of SdFat, much older.

I changed SdFat since she wrote that article.  I am the author of SdFat so I know how it works.

card.init(), volume.init() and openRoot() are now called in SdFat::init() in your call:

Code: [Select]
  sd.init(SPI_HALF_SPEED, 4))

init() should not be called for two instances of Sd2card and two instances of SdVolume.

root should not be opened twice.

Doing these things will cause problems.






feipoa

#23
Sep 25, 2011, 02:46 am Last Edit: Sep 25, 2011, 02:50 am by feipoa Reason: 1
It is working with both card.init and sd.init... for now.

I am not an expert in C/C++ and for my project timeframe, I do not have time to be.  If you know what exactly needs to be altered in the ListFiles() function, please let me know.

From what I've gathered, I need to delete volume and root designations and use class SdFat sd. Use the sd class in sd.init to initialise the SD card.  However, I do not know what to change root.* in the ListFiles() function to.  See below.

Code: [Select]
#include <SdFat.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void setup() {

card.init(SPI_HALF_SPEED, 4);
}

void loop() {

ListFiles(client, LS_SIZE);

}

void ListFiles(Client client, uint8_t flags) {
 dir_t p;
 root.rewind();
 client.println("<ul>");
 while (root.readDir(p) > 0) {
   if (p.name[0] == DIR_NAME_FREE) break;
   if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
   if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
   client.print("<li><a href=\"");
   for (uint8_t i = 0; i < 11; i++) {
     if (p.name[i] == ' ') continue;
     if (i == 8) {
       client.print('.');
     }
     client.print(p.name[i]);
   }
   client.print("\">");
   for (uint8_t i = 0; i < 11; i++) {
     if (p.name[i] == ' ') continue;
     if (i == 8) {
       client.print('.');
     }
     client.print(p.name[i]);
   }
   client.print("</a>");
   if (DIR_IS_SUBDIR(&p)) {
     client.print('/');
   }
   if (flags & LS_DATE) {
      root.printFatDate(p.lastWriteDate);
      client.print(' ');
      root.printFatTime(p.lastWriteTime);
   }
   if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
     client.print(' ');
     client.print(p.fileSize);
   }
   client.println("</li>");
 }
 client.println("</ul>");
}

Go Up