SDFileSystem, slightly modified for the ExoController

Dependencies:   SDFileSystem

Dependents:   Data-Management-Honka

Fork of SDFileSystem_HelloWorld by Bradley Perry

Committer:
mzling
Date:
Wed Feb 04 23:58:12 2015 +0000
Revision:
3:8f5903a77a13
Parent:
2:ec4d7e5fa68e
Child:
4:99e9c9e0dfb0
Got code working!

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 2:ec4d7e5fa68e 5
mzling 2:ec4d7e5fa68e 6
mzling 3:8f5903a77a13 7 SDFile::SDFile(string path, string filename)
mzling 3:8f5903a77a13 8 {
mzling 2:ec4d7e5fa68e 9 //Creates the necessary directory
mzling 3:8f5903a77a13 10 printf("Creating SDFile object\r\n");
mzling 3:8f5903a77a13 11 char *a = new char[path.size()+1];
mzling 3:8f5903a77a13 12 a[path.size()] = 0;
mzling 2:ec4d7e5fa68e 13 memcpy(a,path.c_str(),path.size());
mzling 2:ec4d7e5fa68e 14 //Calculates the full filename, then creates the file
mzling 2:ec4d7e5fa68e 15 std::string fullname = path + filename;
mzling 2:ec4d7e5fa68e 16
mzling 3:8f5903a77a13 17 char *b = new char[fullname.size()+1];
mzling 3:8f5903a77a13 18 b[fullname.size()] = 0;
mzling 3:8f5903a77a13 19 memcpy(b,fullname.c_str(),fullname.size());
mzling 3:8f5903a77a13 20 _fp = fopen((const char*)b, "w+");
mzling 3:8f5903a77a13 21 printf("fp is %d\r\n", _fp);
mzling 2:ec4d7e5fa68e 22
mzling 2:ec4d7e5fa68e 23
mzling 2:ec4d7e5fa68e 24 }
mzling 2:ec4d7e5fa68e 25
mzling 3:8f5903a77a13 26 /**
mzling 3:8f5903a77a13 27 * This function reads bytes from the SDFile object.
mzling 3:8f5903a77a13 28 * @author Michael Ling
mzling 3:8f5903a77a13 29 * @param length The number of bytes to read
mzling 3:8f5903a77a13 30 * @param array The int array the bytes are copied to
mzling 3:8f5903a77a13 31 * @date 2/2/2015
mzling 3:8f5903a77a13 32 */
mzling 3:8f5903a77a13 33 int* SDFile::read(int length, int *array)
mzling 3:8f5903a77a13 34 {
mzling 2:ec4d7e5fa68e 35
mzling 2:ec4d7e5fa68e 36 //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 37 fseek(_fp, -6*length, SEEK_END);
mzling 2:ec4d7e5fa68e 38 //cycle through the length of the vector and read the values.
mzling 3:8f5903a77a13 39 for(int i = 0; i < length; i++) {
mzling 3:8f5903a77a13 40 fscanf(_fp, "%x, ", &array[i]);
mzling 2:ec4d7e5fa68e 41 }
mzling 2:ec4d7e5fa68e 42
mzling 2:ec4d7e5fa68e 43 return array;
mzling 2:ec4d7e5fa68e 44 }
mzling 2:ec4d7e5fa68e 45
mzling 3:8f5903a77a13 46 /**
mzling 3:8f5903a77a13 47 * This function writes from an array to the file pointed to by fp
mzling 3:8f5903a77a13 48 * @param length length of data to write
mzling 3:8f5903a77a13 49 * @param array array to draw written data from
mzling 3:8f5903a77a13 50 * @author Michael Ling
mzling 3:8f5903a77a13 51 * @date 2/2/2015
mzling 3:8f5903a77a13 52 */
mzling 3:8f5903a77a13 53 void SDFile::write(int length, int *array)
mzling 3:8f5903a77a13 54 {
mzling 3:8f5903a77a13 55 fseek(_fp, 0, SEEK_SET);
mzling 3:8f5903a77a13 56 for(int i = 0; i < length-1; i++) {
mzling 3:8f5903a77a13 57 fprintf(_fp, "%04x, ", array[i]);
mzling 2:ec4d7e5fa68e 58 }
mzling 3:8f5903a77a13 59 fprintf(_fp, "%04x\r\n", array[length-1]);
mzling 2:ec4d7e5fa68e 60 }
mzling 2:ec4d7e5fa68e 61