UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
common/SDDataStore.cpp@7:9ab8809f9693, 2019-05-11 (annotated)
- Committer:
- Inscape_ao
- Date:
- Sat May 11 04:03:33 2019 +0000
- Revision:
- 7:9ab8809f9693
- Parent:
- 6:81a3d517fd56
- Child:
- 10:db2be22bc2f9
add Repeatable Controllor; add Dummy Devicedriver; add Sensing Control Command; (All Host->Device Commands are already implemented)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Inscape_ao | 5:a37e3a15444b | 1 | #include "SDDataStore.h" |
Inscape_ao | 5:a37e3a15444b | 2 | |
Inscape_ao | 5:a37e3a15444b | 3 | /* constructor, pSD, TimeManager */ |
Inscape_ao | 5:a37e3a15444b | 4 | SDDataStore::SDDataStore(TimeManager *pSetTM) |
Inscape_ao | 5:a37e3a15444b | 5 | { |
Inscape_ao | 6:81a3d517fd56 | 6 | pS = new SDFileSystem(PC_12, PC_11, PC_10, D4, "sd"); // MOSI, MISO, SCK, CS; |
Inscape_ao | 5:a37e3a15444b | 7 | pT = pSetTM; |
Inscape_ao | 5:a37e3a15444b | 8 | fpCurrent = NULL; |
Inscape_ao | 5:a37e3a15444b | 9 | fnameCurrent[0] = '\0'; |
Inscape_ao | 5:a37e3a15444b | 10 | } |
Inscape_ao | 5:a37e3a15444b | 11 | |
Inscape_ao | 5:a37e3a15444b | 12 | /* destructor */ |
Inscape_ao | 5:a37e3a15444b | 13 | SDDataStore::~SDDataStore() |
Inscape_ao | 5:a37e3a15444b | 14 | { |
Inscape_ao | 5:a37e3a15444b | 15 | closeFile(); |
Inscape_ao | 5:a37e3a15444b | 16 | } |
Inscape_ao | 5:a37e3a15444b | 17 | |
Inscape_ao | 5:a37e3a15444b | 18 | /* for Sensing and Logging generate (prefix)TimeStampFile.(ext)*/ |
Inscape_ao | 5:a37e3a15444b | 19 | bool SDDataStore::startFileWithTimeStamp(char *prefix, char *ext) |
Inscape_ao | 5:a37e3a15444b | 20 | { |
Inscape_ao | 5:a37e3a15444b | 21 | char timeStamp[TimeManager::TimeStampLength + 1] = {0}; |
Inscape_ao | 5:a37e3a15444b | 22 | char fname[MaxPathLength] = {0}; |
Inscape_ao | 5:a37e3a15444b | 23 | pT->getTimeStamp(timeStamp); |
Inscape_ao | 5:a37e3a15444b | 24 | sprintf(fname, "%s%s.%s", prefix, timeStamp, ext); |
Inscape_ao | 5:a37e3a15444b | 25 | if (this->checkFileExist(fname) == true) { |
Inscape_ao | 5:a37e3a15444b | 26 | this->removeFile(fname); |
Inscape_ao | 5:a37e3a15444b | 27 | } |
Inscape_ao | 5:a37e3a15444b | 28 | if (this->openFile(fname) != true) { |
Inscape_ao | 5:a37e3a15444b | 29 | return false; |
Inscape_ao | 5:a37e3a15444b | 30 | } |
Inscape_ao | 5:a37e3a15444b | 31 | return true; |
Inscape_ao | 5:a37e3a15444b | 32 | } |
Inscape_ao | 5:a37e3a15444b | 33 | |
Inscape_ao | 5:a37e3a15444b | 34 | /* checkFileExist, false=NOT-EXIST, true=EXIST */ |
Inscape_ao | 5:a37e3a15444b | 35 | bool SDDataStore::checkFileExist(char *fname) |
Inscape_ao | 5:a37e3a15444b | 36 | { |
Inscape_ao | 5:a37e3a15444b | 37 | FILE *fp; |
Inscape_ao | 5:a37e3a15444b | 38 | char filePath[MaxPathLength]; |
Inscape_ao | 5:a37e3a15444b | 39 | sprintf(filePath, "/sd/%s", fname); |
Inscape_ao | 5:a37e3a15444b | 40 | pathTermination(filePath); |
Inscape_ao | 5:a37e3a15444b | 41 | fp = fopen(filePath, "r"); |
Inscape_ao | 5:a37e3a15444b | 42 | if (fp == NULL) { |
Inscape_ao | 5:a37e3a15444b | 43 | return false; |
Inscape_ao | 5:a37e3a15444b | 44 | } |
Inscape_ao | 5:a37e3a15444b | 45 | fclose(fp); |
Inscape_ao | 5:a37e3a15444b | 46 | return true; |
Inscape_ao | 5:a37e3a15444b | 47 | } |
Inscape_ao | 5:a37e3a15444b | 48 | |
Inscape_ao | 5:a37e3a15444b | 49 | /* removeFile, false=NOT-EXIST, true=EXIST */ |
Inscape_ao | 5:a37e3a15444b | 50 | void SDDataStore::removeFile(char *fname) |
Inscape_ao | 5:a37e3a15444b | 51 | { |
Inscape_ao | 5:a37e3a15444b | 52 | char filePath[MaxPathLength]; |
Inscape_ao | 5:a37e3a15444b | 53 | sprintf(filePath, "/sd/%s", fname); |
Inscape_ao | 5:a37e3a15444b | 54 | pathTermination(filePath); |
Inscape_ao | 5:a37e3a15444b | 55 | remove(filePath); |
Inscape_ao | 5:a37e3a15444b | 56 | } |
Inscape_ao | 5:a37e3a15444b | 57 | |
Inscape_ao | 5:a37e3a15444b | 58 | /* open file at WriteMode FALSE=ERR, TRUE=OK */ |
Inscape_ao | 5:a37e3a15444b | 59 | bool SDDataStore::openFile(char *fname) |
Inscape_ao | 5:a37e3a15444b | 60 | { |
Inscape_ao | 5:a37e3a15444b | 61 | FILE *fp; |
Inscape_ao | 5:a37e3a15444b | 62 | char filePath[MaxPathLength]; |
Inscape_ao | 5:a37e3a15444b | 63 | sprintf(filePath, "/sd/%s", fname); |
Inscape_ao | 5:a37e3a15444b | 64 | pathTermination(filePath); |
Inscape_ao | 5:a37e3a15444b | 65 | |
Inscape_ao | 5:a37e3a15444b | 66 | fp = fopen(filePath, "w"); |
Inscape_ao | 5:a37e3a15444b | 67 | if (fp == NULL) { |
Inscape_ao | 5:a37e3a15444b | 68 | return false; |
Inscape_ao | 5:a37e3a15444b | 69 | } |
Inscape_ao | 5:a37e3a15444b | 70 | fpCurrent = fp; |
Inscape_ao | 5:a37e3a15444b | 71 | sprintf(fnameCurrent, "%s", fname); |
Inscape_ao | 5:a37e3a15444b | 72 | return true; |
Inscape_ao | 5:a37e3a15444b | 73 | } |
Inscape_ao | 5:a37e3a15444b | 74 | |
Inscape_ao | 5:a37e3a15444b | 75 | /* get FileName of open file */ |
Inscape_ao | 5:a37e3a15444b | 76 | char *SDDataStore::getFileName(void) |
Inscape_ao | 5:a37e3a15444b | 77 | { |
Inscape_ao | 5:a37e3a15444b | 78 | return fnameCurrent; |
Inscape_ao | 5:a37e3a15444b | 79 | } |
Inscape_ao | 5:a37e3a15444b | 80 | |
Inscape_ao | 5:a37e3a15444b | 81 | /* get current file pointer */ |
Inscape_ao | 5:a37e3a15444b | 82 | FILE *SDDataStore::getFilePointer(void) |
Inscape_ao | 5:a37e3a15444b | 83 | { |
Inscape_ao | 5:a37e3a15444b | 84 | return fpCurrent; |
Inscape_ao | 5:a37e3a15444b | 85 | } |
Inscape_ao | 5:a37e3a15444b | 86 | |
Inscape_ao | 5:a37e3a15444b | 87 | /* close file with this Class */ |
Inscape_ao | 5:a37e3a15444b | 88 | void SDDataStore::syncFile(void) |
Inscape_ao | 5:a37e3a15444b | 89 | { |
Inscape_ao | 5:a37e3a15444b | 90 | if (fpCurrent != NULL) { |
Inscape_ao | 5:a37e3a15444b | 91 | fflush(fpCurrent); |
Inscape_ao | 5:a37e3a15444b | 92 | } |
Inscape_ao | 5:a37e3a15444b | 93 | } |
Inscape_ao | 5:a37e3a15444b | 94 | |
Inscape_ao | 5:a37e3a15444b | 95 | /* close file with this Class */ |
Inscape_ao | 5:a37e3a15444b | 96 | void SDDataStore::closeFile(void) |
Inscape_ao | 5:a37e3a15444b | 97 | { |
Inscape_ao | 5:a37e3a15444b | 98 | if (fpCurrent != NULL) { |
Inscape_ao | 5:a37e3a15444b | 99 | fclose(fpCurrent); |
Inscape_ao | 5:a37e3a15444b | 100 | fpCurrent = NULL; |
Inscape_ao | 5:a37e3a15444b | 101 | fnameCurrent[0] = '\0'; |
Inscape_ao | 5:a37e3a15444b | 102 | } |
Inscape_ao | 5:a37e3a15444b | 103 | } |
Inscape_ao | 5:a37e3a15444b | 104 | |
Inscape_ao | 5:a37e3a15444b | 105 | void SDDataStore::pathTermination(char *pathArray) |
Inscape_ao | 5:a37e3a15444b | 106 | { |
Inscape_ao | 5:a37e3a15444b | 107 | pathArray[MaxPathLength-1] = '\0'; |
Inscape_ao | 5:a37e3a15444b | 108 | } |