SDFileSystem, slightly modified for the ExoController
Dependents: Data-Management-Honka
Fork of SDFileSystem_HelloWorld by
SDFile.cpp@11:b47fb3344ed9, 2015-05-11 (annotated)
- Committer:
- mzling
- Date:
- Mon May 11 21:47:54 2015 +0000
- Revision:
- 11:b47fb3344ed9
- Parent:
- 10:23a3c0102ab0
Added debug functions/comments
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 | 11:b47fb3344ed9 | 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 | 11:b47fb3344ed9 | 25 | printf("Opening writable file %s\r\n", b); |
mzling | 4:99e9c9e0dfb0 | 26 | _fp = fopen((const char*)b, "w+"); |
mzling | 11:b47fb3344ed9 | 27 | if (_fp == NULL) { |
mzling | 11:b47fb3344ed9 | 28 | //boardLed3 = 1; |
mzling | 11:b47fb3344ed9 | 29 | newFile = true; |
mzling | 11:b47fb3344ed9 | 30 | printf("file %s not found\r\n", b); |
mzling | 11:b47fb3344ed9 | 31 | _fp = fopen((const char*)b, "w+"); |
mzling | 11:b47fb3344ed9 | 32 | } else { |
mzling | 11:b47fb3344ed9 | 33 | newFile = false; |
mzling | 11:b47fb3344ed9 | 34 | printf("file %s found\r\n", b); |
mzling | 11:b47fb3344ed9 | 35 | } |
mzling | 4:99e9c9e0dfb0 | 36 | } |
mzling | 8:964f832265b8 | 37 | //printf("fp is %d\r\n", _fp); |
mzling | 2:ec4d7e5fa68e | 38 | } |
mzling | 2:ec4d7e5fa68e | 39 | |
mzling | 3:8f5903a77a13 | 40 | /** |
mzling | 3:8f5903a77a13 | 41 | * This function reads bytes from the SDFile object. |
mzling | 3:8f5903a77a13 | 42 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 43 | * @param length The number of bytes to read |
mzling | 3:8f5903a77a13 | 44 | * @param array The int array the bytes are copied to |
mzling | 3:8f5903a77a13 | 45 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 46 | */ |
mzling | 3:8f5903a77a13 | 47 | int* SDFile::read(int length, int *array) |
mzling | 3:8f5903a77a13 | 48 | { |
mzling | 2:ec4d7e5fa68e | 49 | |
mzling | 2:ec4d7e5fa68e | 50 | //shift to the end of the file and go back accounting for the commas, spaces, \n, and \r (6 places per data) |
mzling | 11:b47fb3344ed9 | 51 | fseek(_fp, -6*length, SEEK_END); |
mzling | 9:5bad923e18ce | 52 | |
mzling | 9:5bad923e18ce | 53 | //pc.printf("reading to array at %d\r\n", &array[0]); |
mzling | 2:ec4d7e5fa68e | 54 | //cycle through the length of the vector and read the values. |
mzling | 3:8f5903a77a13 | 55 | for(int i = 0; i < length; i++) { |
mzling | 10:23a3c0102ab0 | 56 | //pc.printf("File %d, array %d\r\n", _fp, &array[i]); |
mzling | 11:b47fb3344ed9 | 57 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 9:5bad923e18ce | 58 | // pc.printf("HHHHH\r\n", s); |
mzling | 9:5bad923e18ce | 59 | //pc.printf("read %x now at %ld\r\n", array[i], ftell(_fp)); |
mzling | 2:ec4d7e5fa68e | 60 | } |
mzling | 2:ec4d7e5fa68e | 61 | |
mzling | 2:ec4d7e5fa68e | 62 | return array; |
mzling | 2:ec4d7e5fa68e | 63 | } |
mzling | 2:ec4d7e5fa68e | 64 | |
mzling | 4:99e9c9e0dfb0 | 65 | int* SDFile::read_from_start(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 66 | fseek(_fp, 0, SEEK_SET); |
mzling | 4:99e9c9e0dfb0 | 67 | for(int i = 0; i < length; i++) { |
mzling | 4:99e9c9e0dfb0 | 68 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 4:99e9c9e0dfb0 | 69 | } |
mzling | 4:99e9c9e0dfb0 | 70 | |
mzling | 4:99e9c9e0dfb0 | 71 | return array; |
mzling | 4:99e9c9e0dfb0 | 72 | } |
mzling | 4:99e9c9e0dfb0 | 73 | |
mzling | 11:b47fb3344ed9 | 74 | //Debug only--prints file contents, 6 chars to a line for up to 23 lines |
mzling | 11:b47fb3344ed9 | 75 | void SDFile::debug_print() { |
mzling | 11:b47fb3344ed9 | 76 | fseek(_fp, 0, SEEK_SET); |
mzling | 11:b47fb3344ed9 | 77 | char c; |
mzling | 11:b47fb3344ed9 | 78 | for (int i = 0; i < 23; i++) { |
mzling | 11:b47fb3344ed9 | 79 | for (int j = 0; j < 6; j++) { |
mzling | 11:b47fb3344ed9 | 80 | fscanf(_fp, "%c", &c); |
mzling | 11:b47fb3344ed9 | 81 | pc.printf("%c", c); |
mzling | 11:b47fb3344ed9 | 82 | } |
mzling | 11:b47fb3344ed9 | 83 | pc.printf("\r\n"); |
mzling | 11:b47fb3344ed9 | 84 | } |
mzling | 11:b47fb3344ed9 | 85 | } |
mzling | 11:b47fb3344ed9 | 86 | |
mzling | 3:8f5903a77a13 | 87 | /** |
mzling | 3:8f5903a77a13 | 88 | * This function writes from an array to the file pointed to by fp |
mzling | 3:8f5903a77a13 | 89 | * @param length length of data to write |
mzling | 3:8f5903a77a13 | 90 | * @param array array to draw written data from |
mzling | 3:8f5903a77a13 | 91 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 92 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 93 | */ |
mzling | 3:8f5903a77a13 | 94 | void SDFile::write(int length, int *array) |
mzling | 3:8f5903a77a13 | 95 | { |
mzling | 5:3f554866f226 | 96 | //printf("Seeking START...\r\n"); |
mzling | 3:8f5903a77a13 | 97 | fseek(_fp, 0, SEEK_SET); |
mzling | 7:51c9391e6c4c | 98 | // printf("Writing %d\r\n", array[0]); |
mzling | 5:3f554866f226 | 99 | //printf("Starting to write\r\n"); |
mzling | 11:b47fb3344ed9 | 100 | // int w; |
mzling | 3:8f5903a77a13 | 101 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 102 | /* This line fails when run on testFile, reason unknown...*/ |
mzling | 5:3f554866f226 | 103 | //printf("%d\r\n", fprintf(_fp, "%04x, ", array[i])); |
mzling | 11:b47fb3344ed9 | 104 | //pc.printf("Writing at %ld\r\n", ftell(_fp)); |
mzling | 11:b47fb3344ed9 | 105 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 11:b47fb3344ed9 | 106 | //pc.printf("Wrote %x, now at %ld\r\n", array[i], ftell(_fp)); |
mzling | 4:99e9c9e0dfb0 | 107 | } |
mzling | 7:51c9391e6c4c | 108 | //printf("%d\r\n", fprintf(_fp, "%04x\r\n", array[length-1])); |
mzling | 5:3f554866f226 | 109 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 11:b47fb3344ed9 | 110 | //fflush(_fp); |
mzling | 9:5bad923e18ce | 111 | } |
mzling | 9:5bad923e18ce | 112 | |
mzling | 10:23a3c0102ab0 | 113 | /** This function writes to a single position in the file |
mzling | 10:23a3c0102ab0 | 114 | * @param index write to this index of the file, or at offset index*6 from the start |
mzling | 10:23a3c0102ab0 | 115 | * @param value the new value to be written to that position |
mzling | 10:23a3c0102ab0 | 116 | * @author Michael Ling |
mzling | 10:23a3c0102ab0 | 117 | * @date 4/24/2015 |
mzling | 10:23a3c0102ab0 | 118 | */ |
mzling | 9:5bad923e18ce | 119 | void SDFile::write_to_index(int index, int value) |
mzling | 9:5bad923e18ce | 120 | { |
mzling | 9:5bad923e18ce | 121 | fseek(_fp, 6*index, SEEK_SET); |
mzling | 9:5bad923e18ce | 122 | //pc.printf("Index %d, position %ld\r\n", 6*index, ftell(_fp)); |
mzling | 9:5bad923e18ce | 123 | //fscanf(_fp, "%x, ", &val); |
mzling | 9:5bad923e18ce | 124 | //pc.printf("Val is %x\r\n", val); |
mzling | 9:5bad923e18ce | 125 | //pc.printf("%d, %d\r\n", sizeof value, sizeof 0xdead); |
mzling | 9:5bad923e18ce | 126 | //pc.printf("%04x, ", value); |
mzling | 11:b47fb3344ed9 | 127 | //The &0xffff forces the float to write only 4 characters--without it floats become 8 chars long, for some reason |
mzling | 9:5bad923e18ce | 128 | int w = fprintf(_fp, "%04x, ", value & 0xffff); |
mzling | 9:5bad923e18ce | 129 | //pc.printf("Wrote %d, now at %ld\r\n", w, ftell(_fp)); |
mzling | 9:5bad923e18ce | 130 | fflush(_fp); |
mzling | 4:99e9c9e0dfb0 | 131 | } |
mzling | 4:99e9c9e0dfb0 | 132 | |
mzling | 4:99e9c9e0dfb0 | 133 | void SDFile::append(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 134 | fseek(_fp, 0, SEEK_END); |
mzling | 4:99e9c9e0dfb0 | 135 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 136 | |
mzling | 3:8f5903a77a13 | 137 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 2:ec4d7e5fa68e | 138 | } |
mzling | 3:8f5903a77a13 | 139 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 2:ec4d7e5fa68e | 140 | } |
mzling | 4:99e9c9e0dfb0 | 141 | /* |
mzling | 4:99e9c9e0dfb0 | 142 | void SDFile::changeAccess(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 143 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 144 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 145 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 146 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 147 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 148 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 149 | |
mzling | 4:99e9c9e0dfb0 | 150 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 151 | |
mzling | 4:99e9c9e0dfb0 | 152 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 153 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 154 | printf("Using name %s\r\n", b); |
mzling | 4:99e9c9e0dfb0 | 155 | _fp = fopen((const char *)b, "r+"); |
mzling | 4:99e9c9e0dfb0 | 156 | printf("new FP is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 157 | }*/ |
mzling | 4:99e9c9e0dfb0 | 158 | |
mzling | 4:99e9c9e0dfb0 | 159 | void SDFile::open_for_read(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 160 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 161 | //printf("Trying to open for read\r\n"); |
mzling | 4:99e9c9e0dfb0 | 162 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 163 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 164 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 165 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 166 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 167 | |
mzling | 4:99e9c9e0dfb0 | 168 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 169 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 170 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 171 | _fp = fopen((const char*)b, "r"); |
mzling | 4:99e9c9e0dfb0 | 172 | |
mzling | 5:3f554866f226 | 173 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 174 | } |
mzling | 4:99e9c9e0dfb0 | 175 | |
mzling | 4:99e9c9e0dfb0 | 176 | void SDFile::open_for_write(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 177 | |
mzling | 4:99e9c9e0dfb0 | 178 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 179 | |
mzling | 4:99e9c9e0dfb0 | 180 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 181 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 182 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 183 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 184 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 185 | |
mzling | 4:99e9c9e0dfb0 | 186 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 187 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 188 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 189 | _fp = fopen((const char*)b, "w+"); |
mzling | 4:99e9c9e0dfb0 | 190 | |
mzling | 5:3f554866f226 | 191 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 192 | } |
mzling | 4:99e9c9e0dfb0 | 193 | |
mzling | 4:99e9c9e0dfb0 | 194 | void SDFile::close() { |
mzling | 4:99e9c9e0dfb0 | 195 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 196 | } |