Emulation of LocalFileSystem with virtual COM.
Dependents: KL46Z-lpc81isp lpcterm2
#include "USBLocalFileSystem.h" int main() { USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB) while(1) { usb_local->lock(true); usb_local->remount(); char filename[32]; if (usb_local->find(filename, sizeof(filename), "*.TXT")) { FILE* fp = fopen(filename, "r"); if (fp) { int c; while((c = fgetc(fp)) != EOF) { usb_local->putc(c); } fclose(fp); } } usb_local->lock(false); wait_ms(1000*5); } }
Sample application:
Import programKL46Z-lpc81isp
ISP example program.
Import programlpcterm2
semihost server example program
src/Storage.cpp@0:39eb4d5b97df, 2014-05-03 (annotated)
- Committer:
- va009039
- Date:
- Sat May 03 11:21:37 2014 +0000
- Revision:
- 0:39eb4d5b97df
- Child:
- 4:8f6857784854
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:39eb4d5b97df | 1 | #include "Storage.h" |
va009039 | 0:39eb4d5b97df | 2 | #include "FATFileSystem.h" |
va009039 | 0:39eb4d5b97df | 3 | #include "mbed_debug.h" |
va009039 | 0:39eb4d5b97df | 4 | #include "mystring.h" |
va009039 | 0:39eb4d5b97df | 5 | #include <ctype.h> |
va009039 | 0:39eb4d5b97df | 6 | |
va009039 | 0:39eb4d5b97df | 7 | #if (DEBUG2 > 3) |
va009039 | 0:39eb4d5b97df | 8 | #define STORAGE_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 0:39eb4d5b97df | 9 | #else |
va009039 | 0:39eb4d5b97df | 10 | #define STORAGE_DBG(...) |
va009039 | 0:39eb4d5b97df | 11 | #endif |
va009039 | 0:39eb4d5b97df | 12 | |
va009039 | 0:39eb4d5b97df | 13 | #define LOCAL_DIR "local" |
va009039 | 0:39eb4d5b97df | 14 | |
va009039 | 0:39eb4d5b97df | 15 | LocalStorage::LocalStorage(StorageInterface* storage, const char* name) |
va009039 | 0:39eb4d5b97df | 16 | : FATFileSystem(name),_storage(storage) |
va009039 | 0:39eb4d5b97df | 17 | { |
va009039 | 0:39eb4d5b97df | 18 | _name = name; |
va009039 | 0:39eb4d5b97df | 19 | } |
va009039 | 0:39eb4d5b97df | 20 | |
va009039 | 0:39eb4d5b97df | 21 | extern FILINFO FATDirHandle_finfo; // fst/FATDirHandle.cpp |
va009039 | 0:39eb4d5b97df | 22 | /* static */ bool LocalStorage::find_bin(mystring& filename) |
va009039 | 0:39eb4d5b97df | 23 | { |
va009039 | 0:39eb4d5b97df | 24 | DIR *dir = ::opendir("/"LOCAL_DIR); |
va009039 | 0:39eb4d5b97df | 25 | if (dir == NULL) { |
va009039 | 0:39eb4d5b97df | 26 | return false; |
va009039 | 0:39eb4d5b97df | 27 | } |
va009039 | 0:39eb4d5b97df | 28 | uint32_t fdatetime = 0; |
va009039 | 0:39eb4d5b97df | 29 | bool found = false; |
va009039 | 0:39eb4d5b97df | 30 | struct dirent *entry; |
va009039 | 0:39eb4d5b97df | 31 | while ((entry = readdir(dir)) != NULL) { |
va009039 | 0:39eb4d5b97df | 32 | mystring name(entry->d_name); |
va009039 | 0:39eb4d5b97df | 33 | int len = name.size(); |
va009039 | 0:39eb4d5b97df | 34 | if (name[len-4] == '.' && |
va009039 | 0:39eb4d5b97df | 35 | toupper(name[len-3]) == 'B' && |
va009039 | 0:39eb4d5b97df | 36 | toupper(name[len-2]) == 'I' && |
va009039 | 0:39eb4d5b97df | 37 | toupper(name[len-1]) == 'N') { |
va009039 | 0:39eb4d5b97df | 38 | FILINFO* fi = &FATDirHandle_finfo; |
va009039 | 0:39eb4d5b97df | 39 | uint32_t datetime = fi->ftime | (fi->fdate<<16); |
va009039 | 0:39eb4d5b97df | 40 | STORAGE_DBG("datetime=%08x [%s]", datetime, entry->d_name); |
va009039 | 0:39eb4d5b97df | 41 | if (datetime > fdatetime) { |
va009039 | 0:39eb4d5b97df | 42 | fdatetime = datetime; |
va009039 | 0:39eb4d5b97df | 43 | filename = "/"LOCAL_DIR"/"; |
va009039 | 0:39eb4d5b97df | 44 | filename += entry->d_name; |
va009039 | 0:39eb4d5b97df | 45 | found = true; |
va009039 | 0:39eb4d5b97df | 46 | } |
va009039 | 0:39eb4d5b97df | 47 | } |
va009039 | 0:39eb4d5b97df | 48 | } |
va009039 | 0:39eb4d5b97df | 49 | closedir(dir); |
va009039 | 0:39eb4d5b97df | 50 | return found; |
va009039 | 0:39eb4d5b97df | 51 | } |