Class that contain only FATFileSystem

Fork of USBFileSystem by Erik -

Revision:
0:dabe3383ef23
Child:
1:4ba08d11e36e
diff -r 000000000000 -r dabe3383ef23 USBFileSystem.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBFileSystem.cpp	Wed Jul 31 19:15:55 2013 +0000
@@ -0,0 +1,121 @@
+#include "USBFileSystem.h"
+
+#define USBWRITE_TIMEOUT 100
+
+USBFileSystem::USBFileSystem(const char* n) : FATFileSystem_ds(n) {
+  localFunction = NULL;
+  usbFunction = NULL;
+  usbfree = true;
+  local_count = 0;
+}
+
+
+FileHandle* USBFileSystem::open(const char* name, int flags){    
+    char n[64];
+    sprintf(n, "%d:/%s", _fsid, name);
+    bool write = true;
+    
+    /* POSIX flags -> FatFS open mode */
+    BYTE openmode;
+    if (flags & O_RDWR) {
+        openmode = FA_READ|FA_WRITE;
+        localOpen(true);
+    } else if(flags & O_WRONLY) {
+        openmode = FA_WRITE;
+        localOpen(true);
+    } else {
+        openmode = FA_READ;
+        write = false;
+    }
+    if(flags & O_CREAT) {
+        if(flags & O_TRUNC) {
+            openmode |= FA_CREATE_ALWAYS;
+        } else {
+            openmode |= FA_OPEN_ALWAYS;
+        }
+    }
+    
+    FIL fh;
+    FRESULT res = f_open(&fh, n, openmode);
+    if (res) {
+        if (write)
+            local_count--;
+        return NULL;
+    }
+    if (flags & O_APPEND) {
+        f_lseek(&fh, fh.fsize);
+    }
+    return new USBFileHandle(fh, this, write);
+}
+
+bool USBFileSystem::localSafe( void ){
+    return (local_count == 0);
+}
+
+bool USBFileSystem::usbSafe( void ){
+    return usbfree;
+}
+
+int USBFileSystem::disk_status_fat() { 
+    int retval = _disk_status();
+    
+    if ((retval == 0) && (!usbSafe()))
+        return 4;
+    else 
+        return retval;
+ }
+ 
+ int USBFileSystem::disk_status_msd() { 
+    int retval = _disk_status();
+    
+    if ((retval == 0) && (!localSafe()))
+        return 4;
+    else 
+        return retval;
+ }
+ 
+int USBFileSystem::disk_write(const uint8_t * data, uint64_t block) {
+    if (localSafe()) {
+        if (usbfree && (usbFunction!=NULL) )
+            usbFunction(false);
+            
+        usbfree = false;
+        }
+  
+    int retval= _disk_write(data, block);
+
+    if (localSafe())
+        usb_write.attach_us(this, &USBFileSystem::usbFree, USBWRITE_TIMEOUT * 1000);
+
+    return retval;
+}
+
+void USBFileSystem::localOpen(bool open) {
+    if (open) {
+        local_count++;
+    } else {
+        local_count--;
+    }
+    
+    //Pseudo-IRQ
+    if (localFunction != NULL) {
+        if (open && (local_count == 1))
+            (*localFunction)(false);
+        if (!open && (local_count == 0))
+            (*localFunction)(true);
+    }
+}
+    
+void USBFileSystem::attachLocal(void (*function)(bool)) {
+    localFunction = function;
+}   
+
+void USBFileSystem::attachUSB(void (*function)(bool)) {
+    usbFunction = function;
+}   
+
+void USBFileSystem::usbFree( void ) {
+    usbfree = true;
+    if (usbFunction != NULL)
+        usbFunction(true);
+}
\ No newline at end of file