Revised to support ability to have both SD and USB drives mounted.

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of FATFileSystem by mbed official

Revision:
8:f08059355141
Parent:
4:3ff2606d5713
--- a/FATFileHandle.cpp	Fri Dec 11 16:16:25 2015 +0000
+++ b/FATFileHandle.cpp	Sat Mar 12 23:58:38 2016 +0000
@@ -25,6 +25,42 @@
 
 #include "FATFileHandle.h"
 
+//#define DEBUG "FtFH"
+// ...
+// INFO("Stuff to show %d", var); // new-line is automatically appended
+//
+#if (defined(DEBUG) && !defined(TARGET_LPC11U24))
+#include "mbed.h"
+#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...)  std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+static void HexDump(const char * title, const uint8_t * p, int count)
+{
+    int i;
+    char buf[100] = "0000: ";
+
+    if (*title)
+        INFO("%s", title);
+    for (i=0; i<count; ) {
+        sprintf(buf + strlen(buf), "%02X ", *(p+i));
+        if ((++i & 0x0F) == 0x00) {
+            INFO("%s", buf);
+            if (i < count)
+                sprintf(buf, "%04X: ", i);
+            else
+                buf[0] = '\0';
+        }
+    }
+    if (strlen(buf))
+        INFO("%s", buf);
+}
+#else
+#define INFO(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#define HexDump(a, b, c)
+#endif
+
 FATFileHandle::FATFileHandle(FIL fh) {
     _fh = fh;
 }
@@ -39,18 +75,18 @@
     UINT n;
     FRESULT res = f_write(&_fh, buffer, length, &n);
     if (res) {
-        debug_if(FFS_DBG, "f_write() failed: %d", res);
+        INFO("f_write() failed: %d", res);
         return -1;
     }
     return n;
 }
 
 ssize_t FATFileHandle::read(void* buffer, size_t length) {
-    debug_if(FFS_DBG, "read(%d)\n", length);
+    INFO("read(%d)", length);
     UINT n;
     FRESULT res = f_read(&_fh, buffer, length, &n);
     if (res) {
-        debug_if(FFS_DBG, "f_read() failed: %d\n", res);
+        INFO("f_read() failed: %d", res);
         return -1;
     }
     return n;
@@ -68,10 +104,10 @@
     }
     FRESULT res = f_lseek(&_fh, position);
     if (res) {
-        debug_if(FFS_DBG, "lseek failed: %d\n", res);
+        INFO("lseek failed: %d", res);
         return -1;
     } else {
-        debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
+        INFO("lseek OK, returning %i", _fh.fptr);
         return _fh.fptr;
     }
 }
@@ -79,7 +115,7 @@
 int FATFileHandle::fsync() {
     FRESULT res = f_sync(&_fh);
     if (res) {
-        debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
+        INFO("f_sync() failed: %d", res);
         return -1;
     }
     return 0;
@@ -88,3 +124,4 @@
 off_t FATFileHandle::flen() {
     return _fh.fsize;
 }
+