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