Browse Source

Implement openNextFile for Teensy3

main
PaulStoffregen 9 years ago
parent
commit
f62f98c92d
3 changed files with 97 additions and 6 deletions
  1. +5
    -3
      SD_t3.h
  2. +91
    -3
      dir_t3.cpp
  3. +1
    -0
      file_t3.cpp

+ 5
- 3
SD_t3.h View File

// This Teensy 3.x optimized version is a work-in-progress. // This Teensy 3.x optimized version is a work-in-progress.
// Uncomment this line to use the Teensy version. Otherwise, // Uncomment this line to use the Teensy version. Otherwise,
// the normal SD library is used. // the normal SD library is used.
//#define USE_TEENSY3_OPTIMIZED_CODE
#define USE_TEENSY3_OPTIMIZED_CODE


/* Why reinvent the SD library wheel... /* Why reinvent the SD library wheel...
* 1: Allow reading files from within interrupts * 1: Allow reading files from within interrupts
operator bool() { operator bool() {
return (type < FILE_INVALID); return (type < FILE_INVALID);
} }
char * name();
char * name() {
return namestr;
}
bool isDirectory() { bool isDirectory() {
return (type == FILE_DIR) || (type == FILE_DIR_ROOT16); return (type == FILE_DIR) || (type == FILE_DIR_ROOT16);
} }
uint32_t dirent_lba; // dir sector for this file uint32_t dirent_lba; // dir sector for this file
uint8_t dirent_index; // dir index within sector (0 to 15) uint8_t dirent_index; // dir index within sector (0 to 15)
uint8_t type; // file vs dir uint8_t type; // file vs dir
char namestr[12];
char namestr[13];
friend class SDClass; friend class SDClass;
static inline uint32_t cluster_number(uint32_t n) { static inline uint32_t cluster_number(uint32_t n) {
return n >> (SDClass::sector2cluster + 9); return n >> (SDClass::sector2cluster + 9);

+ 91
- 3
dir_t3.cpp View File

File root = rootDir; File root = rootDir;
File f; File f;


if (mode > FILE_READ) return f;
if (mode > FILE_READ) return f; // TODO: writing not yet supported


if (strcmp(path, "/") == 0) {
f = rootDir;
return f;
}
// TODO: this needs the path traversal feature // TODO: this needs the path traversal feature


//Serial.println("SDClass::open"); //Serial.println("SDClass::open");
return f; return f;
} }


bool SDClass::exists(const char *path)
{
File f = open(path);
return (bool)f;
}

File File::openNextFile(uint8_t mode) File File::openNextFile(uint8_t mode)
{ {
File f; File f;
uint32_t lba, sector_offset, sector_index, sector_count;


if (mode > FILE_READ) return f;

//Serial.print("File::openNextFile, offset=");
//Serial.println(offset);
if (mode > FILE_READ) return f; // TODO: writing not yet supported
if (type == FILE_DIR_ROOT16) {
//Serial.print(" fat16 dir");
sector_offset = offset >> 9;
lba = start_cluster + sector_offset;
sector_count = length >> 9;
} else if (type == FILE_DIR) {
//Serial.print(" subdir");
sector_offset = cluster_offset(offset) >> 9;
lba = custer_to_sector(current_cluster) + sector_offset;
sector_count = (1 << SDClass::sector2cluster);
} else {
return f;
}
//Serial.print(" sector_offset=");
//Serial.println(sector_offset);
//Serial.print(" lba=");
//Serial.println(lba);
//Serial.print(" sector_count=");
//Serial.println(sector_count);
sector_index = (offset >> 5) & 15;
//Serial.print(" sector_index=");
//Serial.println(sector_index);
while (1) {
SDCache sector;
sector_t *s = sector.read(lba);
if (!s) return f;
fatdir_t *dirent = s->dir + sector_index;
while (sector_index < 16) {
//Serial.print(" sector_index=");
//Serial.println(sector_index);
if (dirent->attrib != ATTR_LONG_NAME) {
uint8_t b0 = dirent->name[0];
//Serial.print(" b0=");
//Serial.println(b0);
if (b0 == 0) return f;
if (b0 != 0xE5 && memcmp(dirent->name, ". ", 11) != 0
&& memcmp(dirent->name, ".. ", 11) != 0) {
f.init(dirent);
sector.release();
offset += 32;
if (cluster_offset(offset) == 0) {
next_cluster(); // TODO: handle error
}
return f;
}
}
offset += 32;
dirent++;
sector_index++;
}
sector.release();
sector_index = 0;
if (++sector_offset >= sector_count) {
if (type == FILE_DIR_ROOT16) {
break;
} else {
if (!next_cluster()) break;
lba = custer_to_sector(current_cluster);
sector_offset = 0;
}
}
}
return f; return f;
} }


start_cluster = (dirent->cluster_high << 16) | dirent->cluster_low; start_cluster = (dirent->cluster_high << 16) | dirent->cluster_low;
current_cluster = start_cluster; current_cluster = start_cluster;
type = (dirent->attrib & ATTR_DIRECTORY) ? FILE_DIR : FILE_READ; type = (dirent->attrib & ATTR_DIRECTORY) ? FILE_DIR : FILE_READ;
char *p = namestr;
const char *s = dirent->name;
for (int i=0; i < 8; i++) {
if (*s == ' ') break;
*p++ = *s++;
}
s = dirent->name + 8;
if (*s != ' ') {
*p++ = '.';
*p++ = *s++;
if (*s != ' ') *p++ = *s++;
if (*s != ' ') *p++ = *s++;
}
*p = 0;
//Serial.printf("File::init, cluster = %d, length = %d\n", start_cluster, length); //Serial.printf("File::init, cluster = %d, length = %d\n", start_cluster, length);
} }



+ 1
- 0
file_t3.cpp View File

File::File() File::File()
{ {
type = FILE_INVALID; type = FILE_INVALID;
namestr[0] = 0;
} }


File::~File(void) File::~File(void)

Loading…
Cancel
Save