SDFileSystem, slightly modified for the ExoController
Dependents: Data-Management-Honka
Fork of SDFileSystem_HelloWorld by
SDFile.cpp@2:ec4d7e5fa68e, 2014-12-17 (annotated)
- Committer:
- mzling
- Date:
- Wed Dec 17 21:43:42 2014 +0000
- Revision:
- 2:ec4d7e5fa68e
- Child:
- 3:8f5903a77a13
Created library
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 | 2:ec4d7e5fa68e | 5 | |
mzling | 2:ec4d7e5fa68e | 6 | |
mzling | 2:ec4d7e5fa68e | 7 | SDFile::SDFile(string path, string filename) { |
mzling | 2:ec4d7e5fa68e | 8 | //Creates the necessary directory |
mzling | 2:ec4d7e5fa68e | 9 | char *a=new char[path.size()+1]; |
mzling | 2:ec4d7e5fa68e | 10 | a[path.size()]=0; |
mzling | 2:ec4d7e5fa68e | 11 | memcpy(a,path.c_str(),path.size()); |
mzling | 2:ec4d7e5fa68e | 12 | int dir = mkdir((const char *)a, 0777); |
mzling | 2:ec4d7e5fa68e | 13 | //Calculates the full filename, then creates the file |
mzling | 2:ec4d7e5fa68e | 14 | std::string fullname = path + filename; |
mzling | 2:ec4d7e5fa68e | 15 | |
mzling | 2:ec4d7e5fa68e | 16 | char *b=new char[filename.size()+1]; |
mzling | 2:ec4d7e5fa68e | 17 | b[filename.size()]=0; |
mzling | 2:ec4d7e5fa68e | 18 | memcpy(b,filename.c_str(),filename.size()); |
mzling | 2:ec4d7e5fa68e | 19 | fp = fopen((const char*)b, "w+"); |
mzling | 2:ec4d7e5fa68e | 20 | |
mzling | 2:ec4d7e5fa68e | 21 | |
mzling | 2:ec4d7e5fa68e | 22 | } |
mzling | 2:ec4d7e5fa68e | 23 | |
mzling | 2:ec4d7e5fa68e | 24 | //Reads from the file pointed to by FP to an array |
mzling | 2:ec4d7e5fa68e | 25 | int* SDFile::read(int *array, int length) { |
mzling | 2:ec4d7e5fa68e | 26 | |
mzling | 2:ec4d7e5fa68e | 27 | //shift to the end of the file and go back accounting for the commas, spaces, \n, and \r (6 places per data) |
mzling | 2:ec4d7e5fa68e | 28 | fseek(fp, -6*length, SEEK_END); |
mzling | 2:ec4d7e5fa68e | 29 | //cycle through the length of the vector and read the values. |
mzling | 2:ec4d7e5fa68e | 30 | for(int i=0; i<length; i++) { |
mzling | 2:ec4d7e5fa68e | 31 | fscanf(fp, "%x, ", &array[i]); |
mzling | 2:ec4d7e5fa68e | 32 | } |
mzling | 2:ec4d7e5fa68e | 33 | |
mzling | 2:ec4d7e5fa68e | 34 | return array; |
mzling | 2:ec4d7e5fa68e | 35 | } |
mzling | 2:ec4d7e5fa68e | 36 | |
mzling | 2:ec4d7e5fa68e | 37 | //writes from an array to the file pointed to by fp |
mzling | 2:ec4d7e5fa68e | 38 | void SDFile::write(int *array, int length) { |
mzling | 2:ec4d7e5fa68e | 39 | fseek(fp, 0, SEEK_SET); |
mzling | 2:ec4d7e5fa68e | 40 | for(int i=0; i<length-1; i++) { |
mzling | 2:ec4d7e5fa68e | 41 | fprintf(fp,"%04x, ", array[i]); |
mzling | 2:ec4d7e5fa68e | 42 | } |
mzling | 2:ec4d7e5fa68e | 43 | fprintf(fp, "%04x\r\n", array[length-1]); |
mzling | 2:ec4d7e5fa68e | 44 | } |
mzling | 2:ec4d7e5fa68e | 45 |