SDFileSystem, slightly modified for the ExoController
Dependents: Data-Management-Honka
Fork of SDFileSystem_HelloWorld by
SDFile.cpp@9:5bad923e18ce, 2015-04-24 (annotated)
- Committer:
- mzling
- Date:
- Fri Apr 24 19:32:53 2015 +0000
- Revision:
- 9:5bad923e18ce
- Parent:
- 8:964f832265b8
- Child:
- 10:23a3c0102ab0
Added write_to_index function
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 | 9:5bad923e18ce | 42 | int s = fseek(_fp, -6*length, SEEK_END); |
mzling | 9:5bad923e18ce | 43 | |
mzling | 9:5bad923e18ce | 44 | //pc.printf("reading to array at %d\r\n", &array[0]); |
mzling | 2:ec4d7e5fa68e | 45 | //cycle through the length of the vector and read the values. |
mzling | 3:8f5903a77a13 | 46 | for(int i = 0; i < length; i++) { |
mzling | 9:5bad923e18ce | 47 | pc.printf("File %d, array %d\r\n", _fp, &array[i]); |
mzling | 9:5bad923e18ce | 48 | s= fscanf(_fp, "%x, ", &array[i]); |
mzling | 9:5bad923e18ce | 49 | // pc.printf("HHHHH\r\n", s); |
mzling | 9:5bad923e18ce | 50 | //pc.printf("read %x now at %ld\r\n", array[i], ftell(_fp)); |
mzling | 2:ec4d7e5fa68e | 51 | } |
mzling | 2:ec4d7e5fa68e | 52 | |
mzling | 2:ec4d7e5fa68e | 53 | return array; |
mzling | 2:ec4d7e5fa68e | 54 | } |
mzling | 2:ec4d7e5fa68e | 55 | |
mzling | 4:99e9c9e0dfb0 | 56 | int* SDFile::read_from_start(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 57 | fseek(_fp, 0, SEEK_SET); |
mzling | 4:99e9c9e0dfb0 | 58 | for(int i = 0; i < length; i++) { |
mzling | 4:99e9c9e0dfb0 | 59 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 4:99e9c9e0dfb0 | 60 | } |
mzling | 4:99e9c9e0dfb0 | 61 | |
mzling | 4:99e9c9e0dfb0 | 62 | return array; |
mzling | 4:99e9c9e0dfb0 | 63 | } |
mzling | 4:99e9c9e0dfb0 | 64 | |
mzling | 3:8f5903a77a13 | 65 | /** |
mzling | 3:8f5903a77a13 | 66 | * This function writes from an array to the file pointed to by fp |
mzling | 3:8f5903a77a13 | 67 | * @param length length of data to write |
mzling | 3:8f5903a77a13 | 68 | * @param array array to draw written data from |
mzling | 3:8f5903a77a13 | 69 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 70 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 71 | */ |
mzling | 3:8f5903a77a13 | 72 | void SDFile::write(int length, int *array) |
mzling | 3:8f5903a77a13 | 73 | { |
mzling | 5:3f554866f226 | 74 | //printf("Seeking START...\r\n"); |
mzling | 3:8f5903a77a13 | 75 | fseek(_fp, 0, SEEK_SET); |
mzling | 7:51c9391e6c4c | 76 | // printf("Writing %d\r\n", array[0]); |
mzling | 5:3f554866f226 | 77 | //printf("Starting to write\r\n"); |
mzling | 9:5bad923e18ce | 78 | int w; |
mzling | 3:8f5903a77a13 | 79 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 80 | /* This line fails when run on testFile, reason unknown...*/ |
mzling | 5:3f554866f226 | 81 | //printf("%d\r\n", fprintf(_fp, "%04x, ", array[i])); |
mzling | 9:5bad923e18ce | 82 | pc.printf("Writing at %ld\r\n", ftell(_fp)); |
mzling | 9:5bad923e18ce | 83 | w = fprintf(_fp, "%04x, ", array[i]); |
mzling | 9:5bad923e18ce | 84 | pc.printf("Wrote %d, now at %ld\r\n", w, ftell(_fp)); |
mzling | 4:99e9c9e0dfb0 | 85 | } |
mzling | 7:51c9391e6c4c | 86 | //printf("%d\r\n", fprintf(_fp, "%04x\r\n", array[length-1])); |
mzling | 5:3f554866f226 | 87 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 9:5bad923e18ce | 88 | fflush(_fp); |
mzling | 9:5bad923e18ce | 89 | } |
mzling | 9:5bad923e18ce | 90 | |
mzling | 9:5bad923e18ce | 91 | void SDFile::write_to_index(int index, int value) |
mzling | 9:5bad923e18ce | 92 | { |
mzling | 9:5bad923e18ce | 93 | fseek(_fp, 6*index, SEEK_SET); |
mzling | 9:5bad923e18ce | 94 | //pc.printf("Index %d, position %ld\r\n", 6*index, ftell(_fp)); |
mzling | 9:5bad923e18ce | 95 | //fscanf(_fp, "%x, ", &val); |
mzling | 9:5bad923e18ce | 96 | //pc.printf("Val is %x\r\n", val); |
mzling | 9:5bad923e18ce | 97 | //pc.printf("%d, %d\r\n", sizeof value, sizeof 0xdead); |
mzling | 9:5bad923e18ce | 98 | //pc.printf("%04x, ", value); |
mzling | 9:5bad923e18ce | 99 | int w = fprintf(_fp, "%04x, ", value & 0xffff); |
mzling | 9:5bad923e18ce | 100 | //pc.printf("Wrote %d, now at %ld\r\n", w, ftell(_fp)); |
mzling | 9:5bad923e18ce | 101 | fflush(_fp); |
mzling | 4:99e9c9e0dfb0 | 102 | } |
mzling | 4:99e9c9e0dfb0 | 103 | |
mzling | 4:99e9c9e0dfb0 | 104 | void SDFile::append(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 105 | fseek(_fp, 0, SEEK_END); |
mzling | 4:99e9c9e0dfb0 | 106 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 107 | |
mzling | 3:8f5903a77a13 | 108 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 2:ec4d7e5fa68e | 109 | } |
mzling | 3:8f5903a77a13 | 110 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 2:ec4d7e5fa68e | 111 | } |
mzling | 4:99e9c9e0dfb0 | 112 | /* |
mzling | 4:99e9c9e0dfb0 | 113 | void SDFile::changeAccess(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 114 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 115 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 116 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 117 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 118 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 119 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 120 | |
mzling | 4:99e9c9e0dfb0 | 121 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 122 | |
mzling | 4:99e9c9e0dfb0 | 123 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 124 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 125 | printf("Using name %s\r\n", b); |
mzling | 4:99e9c9e0dfb0 | 126 | _fp = fopen((const char *)b, "r+"); |
mzling | 4:99e9c9e0dfb0 | 127 | printf("new FP is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 128 | }*/ |
mzling | 4:99e9c9e0dfb0 | 129 | |
mzling | 4:99e9c9e0dfb0 | 130 | void SDFile::open_for_read(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 131 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 132 | //printf("Trying to open for read\r\n"); |
mzling | 4:99e9c9e0dfb0 | 133 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 134 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 135 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 136 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 137 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 138 | |
mzling | 4:99e9c9e0dfb0 | 139 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 140 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 141 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 142 | _fp = fopen((const char*)b, "r"); |
mzling | 4:99e9c9e0dfb0 | 143 | |
mzling | 5:3f554866f226 | 144 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 145 | } |
mzling | 4:99e9c9e0dfb0 | 146 | |
mzling | 4:99e9c9e0dfb0 | 147 | void SDFile::open_for_write(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 148 | |
mzling | 4:99e9c9e0dfb0 | 149 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 150 | |
mzling | 4:99e9c9e0dfb0 | 151 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 152 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 153 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 154 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 155 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 156 | |
mzling | 4:99e9c9e0dfb0 | 157 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 158 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 159 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 160 | _fp = fopen((const char*)b, "w+"); |
mzling | 4:99e9c9e0dfb0 | 161 | |
mzling | 5:3f554866f226 | 162 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 163 | } |
mzling | 4:99e9c9e0dfb0 | 164 | |
mzling | 4:99e9c9e0dfb0 | 165 | void SDFile::close() { |
mzling | 4:99e9c9e0dfb0 | 166 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 167 | } |