SDFileSystem, slightly modified for the ExoController
Dependents: Data-Management-Honka
Fork of SDFileSystem_HelloWorld by
SDFile.cpp@8:964f832265b8, 2015-03-11 (annotated)
- Committer:
- mzling
- Date:
- Wed Mar 11 19:39:20 2015 +0000
- Revision:
- 8:964f832265b8
- Parent:
- 7:51c9391e6c4c
- Child:
- 9:5bad923e18ce
trivial changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzling | 2:ec4d7e5fa68e | 1 | #include "mbed.h" |
mzling | 2:ec4d7e5fa68e | 2 | #include "SDFileSystem.h" |
mzling | 2:ec4d7e5fa68e | 3 | #include "SDFile.h" |
mzling | 2:ec4d7e5fa68e | 4 | #include "errno.h" |
mzling | 4:99e9c9e0dfb0 | 5 | #include "initDatabed.h" |
mzling | 2:ec4d7e5fa68e | 6 | |
mzling | 2:ec4d7e5fa68e | 7 | |
mzling | 4:99e9c9e0dfb0 | 8 | SDFile::SDFile(string path, string filename, bool readOnly) |
mzling | 3:8f5903a77a13 | 9 | { |
mzling | 2:ec4d7e5fa68e | 10 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 11 | //printf("Creating SDFile object\r\n"); |
mzling | 3:8f5903a77a13 | 12 | char *a = new char[path.size()+1]; |
mzling | 3:8f5903a77a13 | 13 | a[path.size()] = 0; |
mzling | 2:ec4d7e5fa68e | 14 | memcpy(a,path.c_str(),path.size()); |
mzling | 2:ec4d7e5fa68e | 15 | //Calculates the full filename, then creates the file |
mzling | 2:ec4d7e5fa68e | 16 | std::string fullname = path + filename; |
mzling | 2:ec4d7e5fa68e | 17 | |
mzling | 3:8f5903a77a13 | 18 | char *b = new char[fullname.size()+1]; |
mzling | 3:8f5903a77a13 | 19 | b[fullname.size()] = 0; |
mzling | 3:8f5903a77a13 | 20 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 21 | if (readOnly) { |
mzling | 4:99e9c9e0dfb0 | 22 | //printf("Opening readonly file\r\n"); |
mzling | 4:99e9c9e0dfb0 | 23 | //_fp = fopen((const char*)b, "r"); |
mzling | 4:99e9c9e0dfb0 | 24 | } else { |
mzling | 5:3f554866f226 | 25 | //printf("Opening writable file\r\n"); |
mzling | 4:99e9c9e0dfb0 | 26 | _fp = fopen((const char*)b, "w+"); |
mzling | 4:99e9c9e0dfb0 | 27 | } |
mzling | 8:964f832265b8 | 28 | //printf("fp is %d\r\n", _fp); |
mzling | 2:ec4d7e5fa68e | 29 | } |
mzling | 2:ec4d7e5fa68e | 30 | |
mzling | 3:8f5903a77a13 | 31 | /** |
mzling | 3:8f5903a77a13 | 32 | * This function reads bytes from the SDFile object. |
mzling | 3:8f5903a77a13 | 33 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 34 | * @param length The number of bytes to read |
mzling | 3:8f5903a77a13 | 35 | * @param array The int array the bytes are copied to |
mzling | 3:8f5903a77a13 | 36 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 37 | */ |
mzling | 3:8f5903a77a13 | 38 | int* SDFile::read(int length, int *array) |
mzling | 3:8f5903a77a13 | 39 | { |
mzling | 2:ec4d7e5fa68e | 40 | |
mzling | 2:ec4d7e5fa68e | 41 | //shift to the end of the file and go back accounting for the commas, spaces, \n, and \r (6 places per data) |
mzling | 3:8f5903a77a13 | 42 | fseek(_fp, -6*length, SEEK_END); |
mzling | 2:ec4d7e5fa68e | 43 | //cycle through the length of the vector and read the values. |
mzling | 3:8f5903a77a13 | 44 | for(int i = 0; i < length; i++) { |
mzling | 3:8f5903a77a13 | 45 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 2:ec4d7e5fa68e | 46 | } |
mzling | 2:ec4d7e5fa68e | 47 | |
mzling | 2:ec4d7e5fa68e | 48 | return array; |
mzling | 2:ec4d7e5fa68e | 49 | } |
mzling | 2:ec4d7e5fa68e | 50 | |
mzling | 4:99e9c9e0dfb0 | 51 | int* SDFile::read_from_start(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 52 | fseek(_fp, 0, SEEK_SET); |
mzling | 4:99e9c9e0dfb0 | 53 | for(int i = 0; i < length; i++) { |
mzling | 4:99e9c9e0dfb0 | 54 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 4:99e9c9e0dfb0 | 55 | } |
mzling | 4:99e9c9e0dfb0 | 56 | |
mzling | 4:99e9c9e0dfb0 | 57 | return array; |
mzling | 4:99e9c9e0dfb0 | 58 | } |
mzling | 4:99e9c9e0dfb0 | 59 | |
mzling | 3:8f5903a77a13 | 60 | /** |
mzling | 3:8f5903a77a13 | 61 | * This function writes from an array to the file pointed to by fp |
mzling | 3:8f5903a77a13 | 62 | * @param length length of data to write |
mzling | 3:8f5903a77a13 | 63 | * @param array array to draw written data from |
mzling | 3:8f5903a77a13 | 64 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 65 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 66 | */ |
mzling | 3:8f5903a77a13 | 67 | void SDFile::write(int length, int *array) |
mzling | 3:8f5903a77a13 | 68 | { |
mzling | 5:3f554866f226 | 69 | //printf("Seeking START...\r\n"); |
mzling | 3:8f5903a77a13 | 70 | fseek(_fp, 0, SEEK_SET); |
mzling | 7:51c9391e6c4c | 71 | // printf("Writing %d\r\n", array[0]); |
mzling | 5:3f554866f226 | 72 | //printf("Starting to write\r\n"); |
mzling | 3:8f5903a77a13 | 73 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 74 | /* This line fails when run on testFile, reason unknown...*/ |
mzling | 5:3f554866f226 | 75 | //printf("%d\r\n", fprintf(_fp, "%04x, ", array[i])); |
mzling | 5:3f554866f226 | 76 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 4:99e9c9e0dfb0 | 77 | } |
mzling | 7:51c9391e6c4c | 78 | //printf("%d\r\n", fprintf(_fp, "%04x\r\n", array[length-1])); |
mzling | 5:3f554866f226 | 79 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 4:99e9c9e0dfb0 | 80 | } |
mzling | 4:99e9c9e0dfb0 | 81 | |
mzling | 4:99e9c9e0dfb0 | 82 | void SDFile::append(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 83 | fseek(_fp, 0, SEEK_END); |
mzling | 4:99e9c9e0dfb0 | 84 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 85 | |
mzling | 3:8f5903a77a13 | 86 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 2:ec4d7e5fa68e | 87 | } |
mzling | 3:8f5903a77a13 | 88 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 2:ec4d7e5fa68e | 89 | } |
mzling | 4:99e9c9e0dfb0 | 90 | /* |
mzling | 4:99e9c9e0dfb0 | 91 | void SDFile::changeAccess(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 92 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 93 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 94 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 95 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 96 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 97 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 98 | |
mzling | 4:99e9c9e0dfb0 | 99 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 100 | |
mzling | 4:99e9c9e0dfb0 | 101 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 102 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 103 | printf("Using name %s\r\n", b); |
mzling | 4:99e9c9e0dfb0 | 104 | _fp = fopen((const char *)b, "r+"); |
mzling | 4:99e9c9e0dfb0 | 105 | printf("new FP is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 106 | }*/ |
mzling | 4:99e9c9e0dfb0 | 107 | |
mzling | 4:99e9c9e0dfb0 | 108 | void SDFile::open_for_read(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 109 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 110 | //printf("Trying to open for read\r\n"); |
mzling | 4:99e9c9e0dfb0 | 111 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 112 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 113 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 114 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 115 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 116 | |
mzling | 4:99e9c9e0dfb0 | 117 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 118 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 119 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 120 | _fp = fopen((const char*)b, "r"); |
mzling | 4:99e9c9e0dfb0 | 121 | |
mzling | 5:3f554866f226 | 122 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 123 | } |
mzling | 4:99e9c9e0dfb0 | 124 | |
mzling | 4:99e9c9e0dfb0 | 125 | void SDFile::open_for_write(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 126 | |
mzling | 4:99e9c9e0dfb0 | 127 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 128 | |
mzling | 4:99e9c9e0dfb0 | 129 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 130 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 131 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 132 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 133 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 134 | |
mzling | 4:99e9c9e0dfb0 | 135 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 136 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 137 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 138 | _fp = fopen((const char*)b, "w+"); |
mzling | 4:99e9c9e0dfb0 | 139 | |
mzling | 5:3f554866f226 | 140 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 141 | } |
mzling | 4:99e9c9e0dfb0 | 142 | |
mzling | 4:99e9c9e0dfb0 | 143 | void SDFile::close() { |
mzling | 4:99e9c9e0dfb0 | 144 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 145 | } |