Olli Vanhoja
/
mFS_sample
Examples for mFS.
main.cpp@0:6b88a0d4bab8, 2011-02-21 (annotated)
- Committer:
- HBP
- Date:
- Mon Feb 21 07:40:36 2011 +0000
- Revision:
- 0:6b88a0d4bab8
- Child:
- 1:1d0e27aaee62
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HBP | 0:6b88a0d4bab8 | 1 | /* TODO: *Test forward function |
HBP | 0:6b88a0d4bab8 | 2 | *Test multi block files (read/write & remove) |
HBP | 0:6b88a0d4bab8 | 3 | */ |
HBP | 0:6b88a0d4bab8 | 4 | |
HBP | 0:6b88a0d4bab8 | 5 | #include "common.h" |
HBP | 0:6b88a0d4bab8 | 6 | |
HBP | 0:6b88a0d4bab8 | 7 | void ls(mfs *fs); |
HBP | 0:6b88a0d4bab8 | 8 | void listUsedBlocks(mfs *fs); |
HBP | 0:6b88a0d4bab8 | 9 | |
HBP | 0:6b88a0d4bab8 | 10 | int main() { |
HBP | 0:6b88a0d4bab8 | 11 | char data[1]; |
HBP | 0:6b88a0d4bab8 | 12 | unsigned int n; |
HBP | 0:6b88a0d4bab8 | 13 | |
HBP | 0:6b88a0d4bab8 | 14 | mfs fs(0xA0); |
HBP | 0:6b88a0d4bab8 | 15 | pc.printf("\n\r\n\r[mFS] Formatting... %u bad block headers.\n\r", fs.mkfs(1)); // Quick format EEPROM |
HBP | 0:6b88a0d4bab8 | 16 | |
HBP | 0:6b88a0d4bab8 | 17 | // Mark block 2 as bad |
HBP | 0:6b88a0d4bab8 | 18 | fs.write((char[]){'\x00','\x01','\x01'}, 2, 0, 3); |
HBP | 0:6b88a0d4bab8 | 19 | pc.printf("Block 2 marked as a bad block.\n\r"); |
HBP | 0:6b88a0d4bab8 | 20 | |
HBP | 0:6b88a0d4bab8 | 21 | fs.read(data, 0, 0, 1); |
HBP | 0:6b88a0d4bab8 | 22 | pc.printf("\n\rFirst byte: %X\n\r", data[0]); |
HBP | 0:6b88a0d4bab8 | 23 | |
HBP | 0:6b88a0d4bab8 | 24 | char name[20]; |
HBP | 0:6b88a0d4bab8 | 25 | pc.printf("Create file a: %u\n\r", fs.createFile("testi.txt")); |
HBP | 0:6b88a0d4bab8 | 26 | pc.printf("Create file a: %u\n\r", fs.createFile("testi.txt")); |
HBP | 0:6b88a0d4bab8 | 27 | pc.printf("Create file b: %u\n\r", fs.createFile("koe.txt")); |
HBP | 0:6b88a0d4bab8 | 28 | pc.printf("Rm file a: %u\n\r", fs.removeFile("testi.txt")); |
HBP | 0:6b88a0d4bab8 | 29 | |
HBP | 0:6b88a0d4bab8 | 30 | n = fs.findNextFile(0, name); |
HBP | 0:6b88a0d4bab8 | 31 | pc.printf("Filename (%X): %s\n\r", n, name); |
HBP | 0:6b88a0d4bab8 | 32 | |
HBP | 0:6b88a0d4bab8 | 33 | pc.printf("Create file c: %u\n\r", fs.createFile("new.txt")); |
HBP | 0:6b88a0d4bab8 | 34 | pc.printf("Create file a: %u\n\r", fs.createFile("testi.txt")); |
HBP | 0:6b88a0d4bab8 | 35 | |
HBP | 0:6b88a0d4bab8 | 36 | n = fs.findNextFile(0, name); |
HBP | 0:6b88a0d4bab8 | 37 | pc.printf("Filename (%X): %s\n\r", n, name); |
HBP | 0:6b88a0d4bab8 | 38 | |
HBP | 0:6b88a0d4bab8 | 39 | // Reserve next free block |
HBP | 0:6b88a0d4bab8 | 40 | char fb; |
HBP | 0:6b88a0d4bab8 | 41 | fs.getNextFreeBlock(&fb); |
HBP | 0:6b88a0d4bab8 | 42 | fs.write((char[]){'\x0C','\x01','\x01'}, fb, 0, 3); |
HBP | 0:6b88a0d4bab8 | 43 | pc.printf("Reserved block: %X\n\r", fb); |
HBP | 0:6b88a0d4bab8 | 44 | |
HBP | 0:6b88a0d4bab8 | 45 | listUsedBlocks(&fs); |
HBP | 0:6b88a0d4bab8 | 46 | |
HBP | 0:6b88a0d4bab8 | 47 | ls(&fs); |
HBP | 0:6b88a0d4bab8 | 48 | |
HBP | 0:6b88a0d4bab8 | 49 | |
HBP | 0:6b88a0d4bab8 | 50 | file *fp = new file(&fs, "testi.txt", 1); |
HBP | 0:6b88a0d4bab8 | 51 | pc.printf("File opened successfully\n\r"); |
HBP | 0:6b88a0d4bab8 | 52 | fp->write("The quick brown fox jumps over the lazy dog", 44); |
HBP | 0:6b88a0d4bab8 | 53 | pc.printf("File write OK\n\r"); |
HBP | 0:6b88a0d4bab8 | 54 | fp->flush(); |
HBP | 0:6b88a0d4bab8 | 55 | |
HBP | 0:6b88a0d4bab8 | 56 | pc.printf("Create file d: %u\n\r", fs.createFile("yykaa.txt")); |
HBP | 0:6b88a0d4bab8 | 57 | |
HBP | 0:6b88a0d4bab8 | 58 | pc.printf("RAW read: "); |
HBP | 0:6b88a0d4bab8 | 59 | for (n=0; n < 50; n++) |
HBP | 0:6b88a0d4bab8 | 60 | { |
HBP | 0:6b88a0d4bab8 | 61 | fs.read(data, 1, n, 1); |
HBP | 0:6b88a0d4bab8 | 62 | pc.printf("%X ", data[0]); |
HBP | 0:6b88a0d4bab8 | 63 | } |
HBP | 0:6b88a0d4bab8 | 64 | |
HBP | 0:6b88a0d4bab8 | 65 | char buf[44]; |
HBP | 0:6b88a0d4bab8 | 66 | fp->rewind(); |
HBP | 0:6b88a0d4bab8 | 67 | fp->read(buf, 44); |
HBP | 0:6b88a0d4bab8 | 68 | pc.printf("\n\rSoft read: %s\n\r", buf); |
HBP | 0:6b88a0d4bab8 | 69 | |
HBP | 0:6b88a0d4bab8 | 70 | delete fp; |
HBP | 0:6b88a0d4bab8 | 71 | |
HBP | 0:6b88a0d4bab8 | 72 | pc.printf("Set file flags 0x07 for file new.txt\n\r"); |
HBP | 0:6b88a0d4bab8 | 73 | fs.setFileFlags((char[]){'\x07'}, "new.txt"); |
HBP | 0:6b88a0d4bab8 | 74 | ls(&fs); |
HBP | 0:6b88a0d4bab8 | 75 | listUsedBlocks(&fs); |
HBP | 0:6b88a0d4bab8 | 76 | |
HBP | 0:6b88a0d4bab8 | 77 | // Open read-only file |
HBP | 0:6b88a0d4bab8 | 78 | file *fp2 = new file(&fs, "new.txt", 0); /* Keep in mind that error will * |
HBP | 0:6b88a0d4bab8 | 79 | * be thrown if RO file is * |
HBP | 0:6b88a0d4bab8 | 80 | * opened in RW mode. So it's * |
HBP | 0:6b88a0d4bab8 | 81 | * better to check flags before * |
HBP | 0:6b88a0d4bab8 | 82 | * creating a handle. */ |
HBP | 0:6b88a0d4bab8 | 83 | // This should fail |
HBP | 0:6b88a0d4bab8 | 84 | pc.printf("Trying to write RO file new.txt: %X\n\r", fp2->write("aa", 3)); |
HBP | 0:6b88a0d4bab8 | 85 | delete fp2; |
HBP | 0:6b88a0d4bab8 | 86 | |
HBP | 0:6b88a0d4bab8 | 87 | file *fp3 = new file(&fs, "koe.txt", 1); |
HBP | 0:6b88a0d4bab8 | 88 | |
HBP | 0:6b88a0d4bab8 | 89 | // Try to write multi block file |
HBP | 0:6b88a0d4bab8 | 90 | pc.printf("Trying to write multi block file koe.txt..."); |
HBP | 0:6b88a0d4bab8 | 91 | for (n=0; n < 2*(BS-4)-20-1; n++) |
HBP | 0:6b88a0d4bab8 | 92 | fp3->write("b", 1); |
HBP | 0:6b88a0d4bab8 | 93 | pc.printf(" End\n\r"); |
HBP | 0:6b88a0d4bab8 | 94 | fp3->rewind(); |
HBP | 0:6b88a0d4bab8 | 95 | |
HBP | 0:6b88a0d4bab8 | 96 | listUsedBlocks(&fs); |
HBP | 0:6b88a0d4bab8 | 97 | } |
HBP | 0:6b88a0d4bab8 | 98 | |
HBP | 0:6b88a0d4bab8 | 99 | void ls(mfs *fs) { |
HBP | 0:6b88a0d4bab8 | 100 | unsigned int n, fileCount=0; |
HBP | 0:6b88a0d4bab8 | 101 | char name[20]; |
HBP | 0:6b88a0d4bab8 | 102 | |
HBP | 0:6b88a0d4bab8 | 103 | pc.printf("\n\rls:\n\rName:\t\t\tBlock:\tFlags:\n\r"); |
HBP | 0:6b88a0d4bab8 | 104 | n=0; |
HBP | 0:6b88a0d4bab8 | 105 | while (1) |
HBP | 0:6b88a0d4bab8 | 106 | { |
HBP | 0:6b88a0d4bab8 | 107 | n = fs->findNextFile(n, name); |
HBP | 0:6b88a0d4bab8 | 108 | if (n < 0xFFFF) |
HBP | 0:6b88a0d4bab8 | 109 | { |
HBP | 0:6b88a0d4bab8 | 110 | pc.printf("%s", name); |
HBP | 0:6b88a0d4bab8 | 111 | char len = strlen(name); |
HBP | 0:6b88a0d4bab8 | 112 | for (char i=0; i < 7-ceil((float)len/2); i++) |
HBP | 0:6b88a0d4bab8 | 113 | pc.printf("\t"); |
HBP | 0:6b88a0d4bab8 | 114 | |
HBP | 0:6b88a0d4bab8 | 115 | char flags[1]; |
HBP | 0:6b88a0d4bab8 | 116 | fs->getFileFlags(flags, name); |
HBP | 0:6b88a0d4bab8 | 117 | pc.printf("%X\t%X\n\r", n, flags[0]); |
HBP | 0:6b88a0d4bab8 | 118 | } else // Reach end of fs |
HBP | 0:6b88a0d4bab8 | 119 | break; |
HBP | 0:6b88a0d4bab8 | 120 | n++; |
HBP | 0:6b88a0d4bab8 | 121 | fileCount++; |
HBP | 0:6b88a0d4bab8 | 122 | } |
HBP | 0:6b88a0d4bab8 | 123 | |
HBP | 0:6b88a0d4bab8 | 124 | // Get number of free blocks |
HBP | 0:6b88a0d4bab8 | 125 | char iFreeBlocks = fs->free(); |
HBP | 0:6b88a0d4bab8 | 126 | // Calculate amount of space used |
HBP | 0:6b88a0d4bab8 | 127 | unsigned int iSpaceUsed = VOL_SIZE - (iFreeBlocks*BS); |
HBP | 0:6b88a0d4bab8 | 128 | // Summary |
HBP | 0:6b88a0d4bab8 | 129 | pc.printf("%u files total\n\r%u/%u kB space used, %u blocks free\n\r\n\r", fileCount, iSpaceUsed, VOL_SIZE, iFreeBlocks); |
HBP | 0:6b88a0d4bab8 | 130 | } |
HBP | 0:6b88a0d4bab8 | 131 | |
HBP | 0:6b88a0d4bab8 | 132 | void listUsedBlocks(mfs *fs) |
HBP | 0:6b88a0d4bab8 | 133 | { |
HBP | 0:6b88a0d4bab8 | 134 | char data[3]; |
HBP | 0:6b88a0d4bab8 | 135 | |
HBP | 0:6b88a0d4bab8 | 136 | pc.printf("\n\r"); |
HBP | 0:6b88a0d4bab8 | 137 | for (unsigned int n=0; n < BC; n++) |
HBP | 0:6b88a0d4bab8 | 138 | { |
HBP | 0:6b88a0d4bab8 | 139 | fs->read(data, n, 0, 3); |
HBP | 0:6b88a0d4bab8 | 140 | if (data[0] != 4) |
HBP | 0:6b88a0d4bab8 | 141 | pc.printf("b%XB0: %X, %X, %X\n\r", n, data[0], data[1], data[2]); |
HBP | 0:6b88a0d4bab8 | 142 | } |
HBP | 0:6b88a0d4bab8 | 143 | } |