SDFileSystem, slightly modified for the ExoController

Dependencies:   SDFileSystem

Dependents:   Data-Management-Honka

Fork of SDFileSystem_HelloWorld by Bradley Perry

Committer:
mzling
Date:
Mon Apr 27 21:58:36 2015 +0000
Revision:
10:23a3c0102ab0
Parent:
9:5bad923e18ce
Child:
11:b47fb3344ed9
prints

Who changed what in which revision?

UserRevisionLine numberNew 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 10:23a3c0102ab0 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 10:23a3c0102ab0 91 /** This function writes to a single position in the file
mzling 10:23a3c0102ab0 92 * @param index write to this index of the file, or at offset index*6 from the start
mzling 10:23a3c0102ab0 93 * @param value the new value to be written to that position
mzling 10:23a3c0102ab0 94 * @author Michael Ling
mzling 10:23a3c0102ab0 95 * @date 4/24/2015
mzling 10:23a3c0102ab0 96 */
mzling 9:5bad923e18ce 97 void SDFile::write_to_index(int index, int value)
mzling 9:5bad923e18ce 98 {
mzling 9:5bad923e18ce 99 fseek(_fp, 6*index, SEEK_SET);
mzling 9:5bad923e18ce 100 //pc.printf("Index %d, position %ld\r\n", 6*index, ftell(_fp));
mzling 9:5bad923e18ce 101 //fscanf(_fp, "%x, ", &val);
mzling 9:5bad923e18ce 102 //pc.printf("Val is %x\r\n", val);
mzling 9:5bad923e18ce 103 //pc.printf("%d, %d\r\n", sizeof value, sizeof 0xdead);
mzling 9:5bad923e18ce 104 //pc.printf("%04x, ", value);
mzling 9:5bad923e18ce 105 int w = fprintf(_fp, "%04x, ", value & 0xffff);
mzling 9:5bad923e18ce 106 //pc.printf("Wrote %d, now at %ld\r\n", w, ftell(_fp));
mzling 9:5bad923e18ce 107 fflush(_fp);
mzling 4:99e9c9e0dfb0 108 }
mzling 4:99e9c9e0dfb0 109
mzling 4:99e9c9e0dfb0 110 void SDFile::append(int length, int *array) {
mzling 4:99e9c9e0dfb0 111 fseek(_fp, 0, SEEK_END);
mzling 4:99e9c9e0dfb0 112 for(int i = 0; i < length-1; i++) {
mzling 4:99e9c9e0dfb0 113
mzling 3:8f5903a77a13 114 fprintf(_fp, "%04x, ", array[i]);
mzling 2:ec4d7e5fa68e 115 }
mzling 3:8f5903a77a13 116 fprintf(_fp, "%04x\r\n", array[length-1]);
mzling 2:ec4d7e5fa68e 117 }
mzling 4:99e9c9e0dfb0 118 /*
mzling 4:99e9c9e0dfb0 119 void SDFile::changeAccess(string path, string filename) {
mzling 4:99e9c9e0dfb0 120 fclose(_fp);
mzling 4:99e9c9e0dfb0 121 char *a = new char[path.size()+1];
mzling 4:99e9c9e0dfb0 122 a[path.size()] = 0;
mzling 4:99e9c9e0dfb0 123 memcpy(a,path.c_str(),path.size());
mzling 4:99e9c9e0dfb0 124 //Calculates the full filename, then creates the file
mzling 4:99e9c9e0dfb0 125 std::string fullname = path + filename;
mzling 4:99e9c9e0dfb0 126
mzling 4:99e9c9e0dfb0 127 char *b = new char[fullname.size()+1];
mzling 4:99e9c9e0dfb0 128
mzling 4:99e9c9e0dfb0 129 b[fullname.size()] = 0;
mzling 4:99e9c9e0dfb0 130 memcpy(b,fullname.c_str(),fullname.size());
mzling 4:99e9c9e0dfb0 131 printf("Using name %s\r\n", b);
mzling 4:99e9c9e0dfb0 132 _fp = fopen((const char *)b, "r+");
mzling 4:99e9c9e0dfb0 133 printf("new FP is %d\r\n", _fp);
mzling 4:99e9c9e0dfb0 134 }*/
mzling 4:99e9c9e0dfb0 135
mzling 4:99e9c9e0dfb0 136 void SDFile::open_for_read(string path, string filename) {
mzling 4:99e9c9e0dfb0 137 //Creates the necessary directory
mzling 5:3f554866f226 138 //printf("Trying to open for read\r\n");
mzling 4:99e9c9e0dfb0 139 char *a = new char[path.size()+1];
mzling 4:99e9c9e0dfb0 140 a[path.size()] = 0;
mzling 4:99e9c9e0dfb0 141 memcpy(a,path.c_str(),path.size());
mzling 4:99e9c9e0dfb0 142 //Calculates the full filename, then creates the file
mzling 4:99e9c9e0dfb0 143 std::string fullname = path + filename;
mzling 4:99e9c9e0dfb0 144
mzling 4:99e9c9e0dfb0 145 char *b = new char[fullname.size()+1];
mzling 4:99e9c9e0dfb0 146 b[fullname.size()] = 0;
mzling 4:99e9c9e0dfb0 147 memcpy(b,fullname.c_str(),fullname.size());
mzling 4:99e9c9e0dfb0 148 _fp = fopen((const char*)b, "r");
mzling 4:99e9c9e0dfb0 149
mzling 5:3f554866f226 150 //printf("fp is %d\r\n", _fp);
mzling 4:99e9c9e0dfb0 151 }
mzling 4:99e9c9e0dfb0 152
mzling 4:99e9c9e0dfb0 153 void SDFile::open_for_write(string path, string filename) {
mzling 4:99e9c9e0dfb0 154
mzling 4:99e9c9e0dfb0 155 //Creates the necessary directory
mzling 5:3f554866f226 156
mzling 4:99e9c9e0dfb0 157 char *a = new char[path.size()+1];
mzling 4:99e9c9e0dfb0 158 a[path.size()] = 0;
mzling 4:99e9c9e0dfb0 159 memcpy(a,path.c_str(),path.size());
mzling 4:99e9c9e0dfb0 160 //Calculates the full filename, then creates the file
mzling 4:99e9c9e0dfb0 161 std::string fullname = path + filename;
mzling 4:99e9c9e0dfb0 162
mzling 4:99e9c9e0dfb0 163 char *b = new char[fullname.size()+1];
mzling 4:99e9c9e0dfb0 164 b[fullname.size()] = 0;
mzling 4:99e9c9e0dfb0 165 memcpy(b,fullname.c_str(),fullname.size());
mzling 4:99e9c9e0dfb0 166 _fp = fopen((const char*)b, "w+");
mzling 4:99e9c9e0dfb0 167
mzling 5:3f554866f226 168 //printf("fp is %d\r\n", _fp);
mzling 4:99e9c9e0dfb0 169 }
mzling 4:99e9c9e0dfb0 170
mzling 4:99e9c9e0dfb0 171 void SDFile::close() {
mzling 4:99e9c9e0dfb0 172 fclose(_fp);
mzling 4:99e9c9e0dfb0 173 }