diff --git a/README.md b/README.md index f6c6d7f..de05d6f 100644 --- a/README.md +++ b/README.md @@ -92,20 +92,12 @@ case-insensitive, you'll have to do something like this: I'm also using these libraries that don't come with TeensyDuino: -### uSDFS ### +### SdFat ### -This has long filename support, where the Teensy SD library -doesn't. It's pretty messy code, though, and I hope to abandon it -eventually. +SD card support - accelerated for the Teensy, and with long filename support. - https://github.com/WMXZ-EU/uSDFS + https://github.com/greiman/SdFat -### RadioHead ### - -This library comes with TeensyDuino, but I found the version that's -with 1.35 hangs. Using the stock v1.70 seems to be fine. - - http://www.airspayce.com/mikem/arduino/RadioHead diff --git a/teensy/teensy-filemanager.cpp b/teensy/teensy-filemanager.cpp index 4088c18..8f895b9 100644 --- a/teensy/teensy-filemanager.cpp +++ b/teensy/teensy-filemanager.cpp @@ -1,42 +1,22 @@ #include #include -#include "ff.h" // File System +#include #include "teensy-filemanager.h" #include // strcpy // FIXME: globals are yucky. -DIR dir; -FILINFO fno; -FIL fil; - -int8_t rawFd = -1; -FIL rawFil; - -static TCHAR *char2tchar( const char *charString, int nn, TCHAR *output) -{ - int ii; - for (ii=0; ii= 3) { - const char *fsuff = &fn[strlen(fn)-3]; + // skip anything that has the wrong suffix and isn't a directory + if (suffix && !e.isDirectory() && strlen(outputFN) >= 3) { + const char *fsuff = &outputFN[strlen(outputFN)-3]; if (strstr(suffix, ",")) { - // multiple suffixes to check + // multiple suffixes to check - all must be 3 chars long, FIXME bool matchesAny = false; const char *p = suffix; while (p && strlen(p)) { @@ -153,21 +122,25 @@ int8_t TeensyFileManager::readDir(const char *where, const char *suffix, char *o } p = strstr(p, ",")+1; } - if (!matchesAny) + if (!matchesAny) { + e.close(); continue; + } } else { // one suffix to check - if (strcasecmp(fsuff, suffix)) + if (strcasecmp(fsuff, suffix)) { + e.close(); continue; + } } } if (idxCount == startIdx) { - if (fno.fattrib & AM_DIR) { - strcat(fn, "/"); + if (e.isDirectory()) { + strcat(outputFN, "/"); } - strncpy(outputFN, fn, maxlen); - f_closedir(&dir); + e.close(); + f.close(); return idxCount; } @@ -195,25 +168,21 @@ bool TeensyFileManager::readTrack(int8_t fd, uint8_t *toWhere, bool isNib) return false; // open, seek, read, close. - TCHAR buf[MAXPATH]; - char2tchar(cachedNames[fd], MAXPATH, buf); - FRESULT rc = f_open(&fil, (TCHAR*) buf, FA_READ); - if (rc) { + File f = sd.open(cachedNames[fd], FILE_READ); + if (!f) { Serial.println("failed to open"); return false; } - rc = f_lseek(&fil, fileSeekPositions[fd]); - if (rc) { + if (!f.seek(fileSeekPositions[fd])) { Serial.println("readTrack: seek failed"); - f_close(&fil); + f.close(); return false; } - UINT v; - f_read(&fil, toWhere, isNib ? 0x1a00 : (256 * 16), &v); - f_close(&fil); - return (v == (isNib ? 0x1a00 : (256 * 16))); + int nRead = f.read(toWhere, isNib ? 0x1a00 : (256 * 16)); + f.close(); + return (nRead == (isNib ? 0x1a00 : (256 * 16))); } bool TeensyFileManager::readBlock(int8_t fd, uint8_t *toWhere, bool isNib) @@ -226,24 +195,21 @@ bool TeensyFileManager::readBlock(int8_t fd, uint8_t *toWhere, bool isNib) return false; // open, seek, read, close. - TCHAR buf[MAXPATH]; - char2tchar(cachedNames[fd], MAXPATH, buf); - FRESULT rc = f_open(&fil, (TCHAR*) buf, FA_READ); - if (rc) { + File f = sd.open(cachedNames[fd], FILE_READ); + if (!f) { Serial.println("failed to open"); return false; } - rc = f_lseek(&fil, fileSeekPositions[fd]); - if (rc) { + if (!f.seek(fileSeekPositions[fd])) { Serial.println("readBlock: seek failed"); - f_close(&fil); + f.close(); return false; } - UINT v; - f_read(&fil, toWhere, isNib ? 416 : 256, &v); - f_close(&fil); - return (v == (isNib ? 416 : 256)); + + int nRead = f.read(toWhere, isNib ? 416 : 256); + f.close(); + return (nRead == (isNib ? 416 : 256)); } bool TeensyFileManager::writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib) @@ -260,14 +226,16 @@ bool TeensyFileManager::writeBlock(int8_t fd, uint8_t *fromWhere, bool isNib) return false; // open, seek, write, close. - TCHAR buf[MAXPATH]; - char2tchar(cachedNames[fd], MAXPATH, buf); - FRESULT rc = f_open(&fil, (TCHAR*) buf, FA_WRITE); - rc = f_lseek(&fil, fileSeekPositions[fd]); - UINT v; - f_write(&fil, fromWhere, 256, &v); - f_close(&fil); - return (v == 256); + File f = sd.open(cachedNames[fd], FILE_WRITE); + if (!f || + !f.seek(fileSeekPositions[fd])) { + f.close(); + return false; + } + + int nWritten = f.write(fromWhere, 256); + f.close(); + return (nWritten == 256); } bool TeensyFileManager::writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib) @@ -280,20 +248,22 @@ bool TeensyFileManager::writeTrack(int8_t fd, uint8_t *fromWhere, bool isNib) return false; // open, seek, write, close. - TCHAR buf[MAXPATH]; - char2tchar(cachedNames[fd], MAXPATH, buf); - FRESULT rc = f_open(&fil, (TCHAR*) buf, FA_WRITE); - rc = f_lseek(&fil, fileSeekPositions[fd]); - UINT v; - f_write(&fil, fromWhere, isNib ? 0x1a00 : (256*16), &v); - f_close(&fil); - return (v == (isNib ? 0x1a00 : (256*16))); + File f = sd.open(cachedNames[fd], FILE_WRITE); + if (!f) + return false; + + if (!f.seek(fileSeekPositions[fd])) { + f.close(); + return false; + } + + int nWritten = f.write(fromWhere, isNib ? 0x1a00 : (256*16)); + f.close(); + return (nWritten == (isNib ? 0x1a00 : (256*16))); } bool TeensyFileManager::_prepCache(int8_t fd) { - FRESULT rc; - if (rawFd == -1 || rawFd != fd) { @@ -301,16 +271,13 @@ bool TeensyFileManager::_prepCache(int8_t fd) if (rawFd != -1) { // Close the old one if we had one Serial.println("closing old cache file"); - f_close(&rawFil); + rawFile.close(); rawFd = -1; } Serial.println("opening new cache file"); // Open the new one - TCHAR buf[MAXPATH]; - char2tchar(cachedNames[fd], MAXPATH, buf); - rc = f_open(&rawFil, (TCHAR*) buf, FA_READ|FA_WRITE|FA_OPEN_ALWAYS); - if (rc) { + if (!sd.open(cachedNames[fd], O_RDWR | O_CREAT)) { Serial.print("_prepCache: failed to open "); Serial.println(cachedNames[fd]); return false; @@ -322,7 +289,7 @@ bool TeensyFileManager::_prepCache(int8_t fd) // Serial.println("reopning same cache"); } - return (!rc); + return true; // FIXME error handling } uint8_t TeensyFileManager::readByteAt(int8_t fd, uint32_t pos) @@ -334,21 +301,14 @@ uint8_t TeensyFileManager::readByteAt(int8_t fd, uint32_t pos) if (cachedNames[fd][0] == 0) return false; - FRESULT rc; - _prepCache(fd); - rc = f_lseek(&rawFil, pos); - if (rc) { + if (!rawFile.seek(pos)) { Serial.println("readByteAt: seek failed"); return false; } uint8_t b; - UINT v; - f_read(&rawFil, &b, 1, &v); - - // FIXME: check v == 1 & handle error - return b; + return (rawFile.read(&b, 1) == 1); } bool TeensyFileManager::writeByteAt(int8_t fd, uint8_t v, uint32_t pos) @@ -360,15 +320,12 @@ bool TeensyFileManager::writeByteAt(int8_t fd, uint8_t v, uint32_t pos) if (cachedNames[fd][0] == 0) return false; - FRESULT rc; - _prepCache(fd); - rc = f_lseek(&rawFil, pos); - UINT ret; - f_write(&rawFil, &v, 1, &ret); + if (!rawFile.seek(pos)) + return false; - return (ret == 1); + return (rawFile.write(&v, 1) == 1); } uint8_t TeensyFileManager::readByte(int8_t fd) @@ -380,22 +337,17 @@ uint8_t TeensyFileManager::readByte(int8_t fd) if (cachedNames[fd][0] == 0) return false; - FRESULT rc; - _prepCache(fd); uint32_t pos = fileSeekPositions[fd]; - rc = f_lseek(&rawFil, pos); - if (rc) { + if (!rawFile.seek(pos)) { Serial.println("readByteAt: seek failed"); return false; } - uint8_t b; - UINT v; - f_read(&rawFil, &b, 1, &v); - fileSeekPositions[fd]++; + uint8_t b; + rawFile.read(&b, 1); // FIXME: check v == 1 & handle error return b; @@ -414,22 +366,20 @@ bool TeensyFileManager::writeByte(int8_t fd, uint8_t v) return false; } - FRESULT rc; - _prepCache(fd); uint32_t pos = fileSeekPositions[fd]; - rc = f_lseek(&rawFil, pos); - UINT ret; - f_write(&rawFil, &v, 1, &ret); + if (!rawFile.seek(pos)) { + return false; + } + + if (rawFile.write(&v, 1) != 1) { + return false; + } fileSeekPositions[fd]++; - if (ret != 1) { - Serial.println("Write failed"); - } - - return (ret == 1); + return true; } diff --git a/teensy/teensy-filemanager.h b/teensy/teensy-filemanager.h index c0f0a1f..0c91413 100644 --- a/teensy/teensy-filemanager.h +++ b/teensy/teensy-filemanager.h @@ -31,6 +31,8 @@ class TeensyFileManager : public FileManager { bool _prepCache(int8_t fd); private: + bool enabled; + int8_t numCached; }; diff --git a/teensy/teensy.ino b/teensy/teensy.ino index 5fe48cc..984d248 100644 --- a/teensy/teensy.ino +++ b/teensy/teensy.ino @@ -1,5 +1,4 @@ #include -#include // uSDFS #include #include #include @@ -23,7 +22,6 @@ uint32_t nextInstructionMicros; uint32_t startMicros; -FATFS fatfs; /* File system object */ BIOS bios; enum { @@ -72,9 +70,6 @@ void setup() Serial.println("Error while setting RTC"); } - TCHAR *device = (TCHAR *)_T("0:/"); - f_mount (&fatfs, device, 0); /* Mount/Unmount a logical drive */ - pinMode(RESETPIN, INPUT); digitalWrite(RESETPIN, HIGH);