File manager
FileManager.cpp@22:2fc64ad66e35, 2016-05-25 (annotated)
- Committer:
- nsrwsurasak
- Date:
- Wed May 25 11:43:45 2016 +0000
- Revision:
- 22:2fc64ad66e35
- Parent:
- 21:0c52dbc23f52
- Child:
- 23:7ffd9724e105
Add millisecond variable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lucyjungz | 14:4ba6147f067b | 1 | /** |
Lucyjungz | 14:4ba6147f067b | 2 | ****************************************************************************** |
Lucyjungz | 14:4ba6147f067b | 3 | * @file FileManager.cpp |
Lucyjungz | 14:4ba6147f067b | 4 | * @author Narut T |
Lucyjungz | 14:4ba6147f067b | 5 | * @version V1 |
Lucyjungz | 14:4ba6147f067b | 6 | * @date 19/05/2016 |
Lucyjungz | 14:4ba6147f067b | 7 | * @brief File Manager for managing file system in SD Card |
Lucyjungz | 14:4ba6147f067b | 8 | ******************************************************************************/ |
Lucyjungz | 14:4ba6147f067b | 9 | |
nsrwsurasak | 13:d83e2dcc882d | 10 | #include "main.h" |
nsrwsurasak | 0:a27e0d3581d1 | 11 | |
Lucyjungz | 14:4ba6147f067b | 12 | char m_StrGpsInterval[XMLTEXT_SIZE]; // GPS Interval |
Lucyjungz | 14:4ba6147f067b | 13 | char m_StrDataInterval[XMLTEXT_SIZE]; // Data Interval |
Lucyjungz | 14:4ba6147f067b | 14 | |
Lucyjungz | 14:4ba6147f067b | 15 | uint32_t m_GpsInterval; |
Lucyjungz | 14:4ba6147f067b | 16 | uint32_t m_DataInterval; |
Lucyjungz | 3:6e08d0bba1bb | 17 | Variable_Data_TypeDef m_varList[MAX_VAR]; // Variable List |
Lucyjungz | 3:6e08d0bba1bb | 18 | unsigned int m_amountVarList = 0; // Amount of variable list |
Lucyjungz | 21:0c52dbc23f52 | 19 | bool m_SdCardHasDisconnected = false; // flag to indicate sd card has disconnected |
Lucyjungz | 21:0c52dbc23f52 | 20 | char m_varListFileName[MAX_FILE_NAME_SIZE]; // Variable File Name |
Lucyjungz | 21:0c52dbc23f52 | 21 | bool m_sdCardIsPresent; // Flag to indicate sd card is present |
Lucyjungz | 21:0c52dbc23f52 | 22 | /** Connect pin for SD Card LED */ |
Lucyjungz | 11:e21d4c5bfd1b | 23 | #ifdef LED_SDCARD |
Lucyjungz | 17:d24d2b2bbd42 | 24 | DigitalOut ledStatus(LED_SDCARD); |
Lucyjungz | 11:e21d4c5bfd1b | 25 | #else |
Lucyjungz | 11:e21d4c5bfd1b | 26 | DigitalOut ledStatus(NC); |
Lucyjungz | 11:e21d4c5bfd1b | 27 | #endif |
Lucyjungz | 11:e21d4c5bfd1b | 28 | |
Lucyjungz | 17:d24d2b2bbd42 | 29 | /** Connect pin for SD Card Switch */ |
Lucyjungz | 17:d24d2b2bbd42 | 30 | #ifdef SDCARD_SWITCH |
nsrwsurasak | 22:2fc64ad66e35 | 31 | //InterruptIn sdSwitch(SDCARD_SWITCH); |
Lucyjungz | 17:d24d2b2bbd42 | 32 | #else |
Lucyjungz | 21:0c52dbc23f52 | 33 | InterruptIn sdSwitch(NC); |
Lucyjungz | 17:d24d2b2bbd42 | 34 | #endif |
Lucyjungz | 17:d24d2b2bbd42 | 35 | |
Lucyjungz | 3:6e08d0bba1bb | 36 | /* ############### Static function prototype ################## */ |
nsrwsurasak | 0:a27e0d3581d1 | 37 | |
Lucyjungz | 14:4ba6147f067b | 38 | static void FILEMANAGER_RemoveSpaces(char* s , int size); |
Lucyjungz | 14:4ba6147f067b | 39 | static void FILEMANAGER_GetXmlText(char *str, char *ret); |
Lucyjungz | 14:4ba6147f067b | 40 | static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name); |
Lucyjungz | 14:4ba6147f067b | 41 | static void FILEMANAGER_SetLedStatus(bool on); |
Lucyjungz | 3:6e08d0bba1bb | 42 | |
Lucyjungz | 3:6e08d0bba1bb | 43 | /** |
Lucyjungz | 3:6e08d0bba1bb | 44 | * @brief Utility function to Remove space charector from given char array |
Lucyjungz | 3:6e08d0bba1bb | 45 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 46 | * @param char array to process remove spaces |
Lucyjungz | 3:6e08d0bba1bb | 47 | * @param size of char array |
Lucyjungz | 3:6e08d0bba1bb | 48 | * @retval space removed char array |
Lucyjungz | 3:6e08d0bba1bb | 49 | */ |
Lucyjungz | 14:4ba6147f067b | 50 | static void FILEMANAGER_RemoveSpaces(char* s , int size) |
nsrwsurasak | 0:a27e0d3581d1 | 51 | { |
nsrwsurasak | 0:a27e0d3581d1 | 52 | char* cpy = s; // an alias to iterate through s without moving s |
nsrwsurasak | 0:a27e0d3581d1 | 53 | char* temp = s; |
nsrwsurasak | 0:a27e0d3581d1 | 54 | |
nsrwsurasak | 0:a27e0d3581d1 | 55 | for (int i = 0 ; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 56 | if (*cpy != ' ') |
nsrwsurasak | 0:a27e0d3581d1 | 57 | *temp++ = *cpy; |
nsrwsurasak | 0:a27e0d3581d1 | 58 | cpy++; |
nsrwsurasak | 0:a27e0d3581d1 | 59 | } |
nsrwsurasak | 0:a27e0d3581d1 | 60 | *temp = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 61 | return; |
nsrwsurasak | 0:a27e0d3581d1 | 62 | } |
Lucyjungz | 3:6e08d0bba1bb | 63 | /** |
Lucyjungz | 3:6e08d0bba1bb | 64 | * @brief Utility function to get XML tag |
Lucyjungz | 3:6e08d0bba1bb | 65 | * @note Only First tag will be returned |
Lucyjungz | 3:6e08d0bba1bb | 66 | * @param char array to get XML Text |
Lucyjungz | 3:6e08d0bba1bb | 67 | * @param char array to be populate XML text |
Lucyjungz | 3:6e08d0bba1bb | 68 | * @retval XML text |
Lucyjungz | 3:6e08d0bba1bb | 69 | */ |
Lucyjungz | 14:4ba6147f067b | 70 | static void FILEMANAGER_GetXmlText(char *str, char *ret) |
nsrwsurasak | 0:a27e0d3581d1 | 71 | { |
nsrwsurasak | 0:a27e0d3581d1 | 72 | int size = strlen(str); |
nsrwsurasak | 0:a27e0d3581d1 | 73 | int i; |
nsrwsurasak | 0:a27e0d3581d1 | 74 | bool begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 75 | char * ret_addr = ret; |
Lucyjungz | 3:6e08d0bba1bb | 76 | /* initialized our return value */ |
nsrwsurasak | 0:a27e0d3581d1 | 77 | memset (ret,' ',XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 78 | |
Lucyjungz | 3:6e08d0bba1bb | 79 | /* Loop to check XML tag symbols */ |
nsrwsurasak | 0:a27e0d3581d1 | 80 | for(i = 0; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 81 | |
nsrwsurasak | 0:a27e0d3581d1 | 82 | if (*str == '>') { |
Lucyjungz | 3:6e08d0bba1bb | 83 | /* Found begining of the tag */ |
nsrwsurasak | 0:a27e0d3581d1 | 84 | begin_text = true; |
nsrwsurasak | 0:a27e0d3581d1 | 85 | } else if (begin_text && *str == '<') { |
Lucyjungz | 3:6e08d0bba1bb | 86 | /* Reach the end of text message */ |
nsrwsurasak | 0:a27e0d3581d1 | 87 | begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 88 | break; |
nsrwsurasak | 0:a27e0d3581d1 | 89 | } else if (begin_text && *str != ' ') { |
Lucyjungz | 3:6e08d0bba1bb | 90 | /* Populate the return value */ |
nsrwsurasak | 0:a27e0d3581d1 | 91 | *ret = *str; |
nsrwsurasak | 0:a27e0d3581d1 | 92 | ret++; |
nsrwsurasak | 0:a27e0d3581d1 | 93 | } |
Lucyjungz | 3:6e08d0bba1bb | 94 | /* Move to next char */ |
nsrwsurasak | 0:a27e0d3581d1 | 95 | str++; |
nsrwsurasak | 0:a27e0d3581d1 | 96 | } |
Lucyjungz | 3:6e08d0bba1bb | 97 | |
Lucyjungz | 3:6e08d0bba1bb | 98 | /* Remove space from return value */ |
Lucyjungz | 14:4ba6147f067b | 99 | FILEMANAGER_RemoveSpaces(ret_addr, XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 100 | } |
Lucyjungz | 3:6e08d0bba1bb | 101 | /** |
Lucyjungz | 3:6e08d0bba1bb | 102 | * @brief Utility function to get File Name with Date |
Lucyjungz | 3:6e08d0bba1bb | 103 | * @note Format file will be YYYY-MM-DD.filename |
Lucyjungz | 3:6e08d0bba1bb | 104 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 105 | * @param file_name - char array to file name |
Lucyjungz | 3:6e08d0bba1bb | 106 | * @retval renamed file name |
Lucyjungz | 3:6e08d0bba1bb | 107 | */ |
Lucyjungz | 14:4ba6147f067b | 108 | static void FILEMANAGER_GenerateFileNameWithTime(time_t timestamp, char * file_name) |
Lucyjungz | 1:1f1f2b99756b | 109 | { |
Lucyjungz | 14:4ba6147f067b | 110 | char str[RENAME_FILE_BUFFER_SIZE]; |
Lucyjungz | 1:1f1f2b99756b | 111 | struct tm * ptm; |
Lucyjungz | 1:1f1f2b99756b | 112 | |
Lucyjungz | 3:6e08d0bba1bb | 113 | /* Convert timestamp to readable format */ |
Lucyjungz | 3:6e08d0bba1bb | 114 | ptm = localtime ( ×tamp ); |
Lucyjungz | 3:6e08d0bba1bb | 115 | |
Lucyjungz | 3:6e08d0bba1bb | 116 | /* Replacing YYYY to the converted year */ |
Lucyjungz | 14:4ba6147f067b | 117 | sprintf(str,"%04d", ptm->tm_year + YEAR_4DIGITS_OFFSET); |
Lucyjungz | 14:4ba6147f067b | 118 | memcpy(&file_name[TIMESTAMP_YEAR_OFFSET], str, TIMESTAMP_YEAR_SIZE); |
Lucyjungz | 1:1f1f2b99756b | 119 | |
Lucyjungz | 3:6e08d0bba1bb | 120 | /* Replacing MM to converted month */ |
Lucyjungz | 1:1f1f2b99756b | 121 | sprintf(str,"%02d", ptm->tm_mon+1); |
Lucyjungz | 14:4ba6147f067b | 122 | memcpy(&file_name[TIMESTAMP_MONTH_OFFSET], str, TIMESTAMP_MONTH_SIZE); |
Lucyjungz | 1:1f1f2b99756b | 123 | |
Lucyjungz | 3:6e08d0bba1bb | 124 | /* Replacing DD to converted date */ |
Lucyjungz | 1:1f1f2b99756b | 125 | sprintf(str,"%02d", ptm->tm_mday); |
Lucyjungz | 14:4ba6147f067b | 126 | memcpy(&file_name[TIMESTAMP_DATE_OFFSET], str,TIMESTAMP_DATE_SIZE); |
Lucyjungz | 1:1f1f2b99756b | 127 | } |
Lucyjungz | 3:6e08d0bba1bb | 128 | /** |
Lucyjungz | 3:6e08d0bba1bb | 129 | * @brief Function to perform read setup file |
Lucyjungz | 3:6e08d0bba1bb | 130 | * @note filename must be defined in FileManager.h, GPS/ Data interval will be stored in dedicated variable |
Lucyjungz | 3:6e08d0bba1bb | 131 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 132 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 133 | */ |
Lucyjungz | 14:4ba6147f067b | 134 | void FILEMANAGER_ReadSetupFile() |
nsrwsurasak | 0:a27e0d3581d1 | 135 | { |
Lucyjungz | 3:6e08d0bba1bb | 136 | /* Open file in reading mode */ |
nsrwsurasak | 0:a27e0d3581d1 | 137 | FILE *fp = fopen(SETUP_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 138 | |
Lucyjungz | 3:6e08d0bba1bb | 139 | if (fp == NULL) { |
Lucyjungz | 3:6e08d0bba1bb | 140 | /* In case of error, print the message */ |
nsrwsurasak | 0:a27e0d3581d1 | 141 | printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); |
Lucyjungz | 11:e21d4c5bfd1b | 142 | |
Lucyjungz | 11:e21d4c5bfd1b | 143 | /* Indicate LED Status (OFF)*/ |
Lucyjungz | 14:4ba6147f067b | 144 | FILEMANAGER_SetLedStatus(false); |
Lucyjungz | 3:6e08d0bba1bb | 145 | } else { |
nsrwsurasak | 0:a27e0d3581d1 | 146 | |
Lucyjungz | 3:6e08d0bba1bb | 147 | /* Initialized state */ |
nsrwsurasak | 0:a27e0d3581d1 | 148 | ReadingFileState state = STATE_FINDING; |
Lucyjungz | 3:6e08d0bba1bb | 149 | |
Lucyjungz | 3:6e08d0bba1bb | 150 | /* Allocate buffer for reading file */ |
Lucyjungz | 14:4ba6147f067b | 151 | char buf[READ_FILE_BUFFER_SIZE]; |
Lucyjungz | 3:6e08d0bba1bb | 152 | |
Lucyjungz | 11:e21d4c5bfd1b | 153 | /* Indicate LED Status (ON)*/ |
Lucyjungz | 14:4ba6147f067b | 154 | FILEMANAGER_SetLedStatus(true); |
Lucyjungz | 11:e21d4c5bfd1b | 155 | |
Lucyjungz | 3:6e08d0bba1bb | 156 | /* Read line from the file */ |
nsrwsurasak | 0:a27e0d3581d1 | 157 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Lucyjungz | 3:6e08d0bba1bb | 158 | |
Lucyjungz | 3:6e08d0bba1bb | 159 | /* Check the TAG */ |
Lucyjungz | 3:6e08d0bba1bb | 160 | if (strstr (buf,DATA_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 161 | { |
Lucyjungz | 3:6e08d0bba1bb | 162 | /* Found the DATA TAG */ |
nsrwsurasak | 0:a27e0d3581d1 | 163 | state = STATE_FOUND_DATA; |
Lucyjungz | 21:0c52dbc23f52 | 164 | } |
Lucyjungz | 21:0c52dbc23f52 | 165 | else if (strstr (buf,GPS_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 166 | { |
Lucyjungz | 3:6e08d0bba1bb | 167 | /* Found GPS TAG */ |
nsrwsurasak | 0:a27e0d3581d1 | 168 | state = STATE_FOUND_GPS; |
Lucyjungz | 21:0c52dbc23f52 | 169 | } |
Lucyjungz | 21:0c52dbc23f52 | 170 | else if (strstr (buf,UPDATE_INTERVAL_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 171 | { |
Lucyjungz | 3:6e08d0bba1bb | 172 | /* Found Interval TAG */ |
Lucyjungz | 3:6e08d0bba1bb | 173 | if (state == STATE_FOUND_GPS) |
Lucyjungz | 3:6e08d0bba1bb | 174 | { |
Lucyjungz | 3:6e08d0bba1bb | 175 | /* Get XML text for GPS Interval */ |
Lucyjungz | 14:4ba6147f067b | 176 | FILEMANAGER_GetXmlText(buf, m_StrGpsInterval); |
Lucyjungz | 14:4ba6147f067b | 177 | m_GpsInterval = atoi(m_StrGpsInterval); |
Lucyjungz | 8:5af4e12c43b2 | 178 | #if DEBUG |
Lucyjungz | 14:4ba6147f067b | 179 | printf("\r\n-found GPS interval %s ", m_StrGpsInterval); |
Lucyjungz | 8:5af4e12c43b2 | 180 | #endif |
Lucyjungz | 8:5af4e12c43b2 | 181 | |
Lucyjungz | 8:5af4e12c43b2 | 182 | /* Set state to indicate that we are finding */ |
nsrwsurasak | 0:a27e0d3581d1 | 183 | state = STATE_FINDING; |
Lucyjungz | 3:6e08d0bba1bb | 184 | } |
Lucyjungz | 3:6e08d0bba1bb | 185 | else if(state == STATE_FOUND_DATA) |
Lucyjungz | 3:6e08d0bba1bb | 186 | { |
Lucyjungz | 3:6e08d0bba1bb | 187 | /* Get XML text for Data Interval */ |
Lucyjungz | 14:4ba6147f067b | 188 | FILEMANAGER_GetXmlText(buf, m_StrDataInterval); |
Lucyjungz | 14:4ba6147f067b | 189 | m_DataInterval = atoi(m_StrDataInterval); |
Lucyjungz | 8:5af4e12c43b2 | 190 | #if DEBUG |
Lucyjungz | 14:4ba6147f067b | 191 | printf("\r\n-found Data interval %s ", m_StrDataInterval); |
Lucyjungz | 8:5af4e12c43b2 | 192 | #endif |
Lucyjungz | 8:5af4e12c43b2 | 193 | |
Lucyjungz | 8:5af4e12c43b2 | 194 | /* Set state to indicate that we are finding */ |
nsrwsurasak | 0:a27e0d3581d1 | 195 | state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 196 | } |
nsrwsurasak | 0:a27e0d3581d1 | 197 | } |
Lucyjungz | 21:0c52dbc23f52 | 198 | else if (strstr (buf,VAR_LIST_FILE_TAG)) |
Lucyjungz | 21:0c52dbc23f52 | 199 | { |
Lucyjungz | 21:0c52dbc23f52 | 200 | /* Get XML text for Variable File Name */ |
Lucyjungz | 21:0c52dbc23f52 | 201 | char bufFileName[MAX_FILE_NAME_SIZE]; |
Lucyjungz | 21:0c52dbc23f52 | 202 | FILEMANAGER_GetXmlText(buf, bufFileName); |
Lucyjungz | 21:0c52dbc23f52 | 203 | memcpy(m_varListFileName,PREFIX_FILE_NAME,strlen(PREFIX_FILE_NAME)); |
Lucyjungz | 21:0c52dbc23f52 | 204 | memcpy(&m_varListFileName[strlen(PREFIX_FILE_NAME)],bufFileName,strlen(bufFileName)); |
Lucyjungz | 21:0c52dbc23f52 | 205 | |
Lucyjungz | 21:0c52dbc23f52 | 206 | } |
nsrwsurasak | 0:a27e0d3581d1 | 207 | } |
Lucyjungz | 3:6e08d0bba1bb | 208 | /* Ensure file is closed */ |
Lucyjungz | 3:6e08d0bba1bb | 209 | fclose(fp); |
nsrwsurasak | 0:a27e0d3581d1 | 210 | } |
nsrwsurasak | 0:a27e0d3581d1 | 211 | } |
Lucyjungz | 3:6e08d0bba1bb | 212 | /** |
Lucyjungz | 3:6e08d0bba1bb | 213 | * @brief Function to log GPS Data |
Lucyjungz | 3:6e08d0bba1bb | 214 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 215 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 216 | * @param lat - char array for lattitude |
Lucyjungz | 3:6e08d0bba1bb | 217 | * @param longti - char array for longtitude |
Lucyjungz | 3:6e08d0bba1bb | 218 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 219 | */ |
Lucyjungz | 14:4ba6147f067b | 220 | void FILEMANAGER_LogGPSData(time_t timestamp ,char lat[], char longti[]) |
nsrwsurasak | 0:a27e0d3581d1 | 221 | { |
Lucyjungz | 3:6e08d0bba1bb | 222 | /* Get File name */ |
Lucyjungz | 1:1f1f2b99756b | 223 | char file_name[] = GPS_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 224 | |
Lucyjungz | 3:6e08d0bba1bb | 225 | /* Generate file name with time stamp */ |
Lucyjungz | 14:4ba6147f067b | 226 | FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 3:6e08d0bba1bb | 227 | |
Lucyjungz | 3:6e08d0bba1bb | 228 | /* Open file with "APPEND" mode */ |
Lucyjungz | 1:1f1f2b99756b | 229 | FILE *fp = fopen(file_name, "a"); |
Lucyjungz | 1:1f1f2b99756b | 230 | |
Lucyjungz | 3:6e08d0bba1bb | 231 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 232 | { |
Lucyjungz | 3:6e08d0bba1bb | 233 | /* if it can't open the file then print error message */ |
Lucyjungz | 1:1f1f2b99756b | 234 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 11:e21d4c5bfd1b | 235 | |
Lucyjungz | 11:e21d4c5bfd1b | 236 | /* Indicate LED Status (OFF)*/ |
Lucyjungz | 14:4ba6147f067b | 237 | FILEMANAGER_SetLedStatus(false); |
Lucyjungz | 3:6e08d0bba1bb | 238 | } |
Lucyjungz | 11:e21d4c5bfd1b | 239 | else |
Lucyjungz | 11:e21d4c5bfd1b | 240 | { |
Lucyjungz | 11:e21d4c5bfd1b | 241 | /* Indicate LED Status (ON)*/ |
Lucyjungz | 14:4ba6147f067b | 242 | FILEMANAGER_SetLedStatus(true); |
Lucyjungz | 11:e21d4c5bfd1b | 243 | |
Lucyjungz | 3:6e08d0bba1bb | 244 | /* Print some message for information */ |
Lucyjungz | 1:1f1f2b99756b | 245 | printf("\r\n Writing to Gps Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 246 | |
Lucyjungz | 3:6e08d0bba1bb | 247 | /* Write the line with lattitude and longtitude */ |
Lucyjungz | 3:6e08d0bba1bb | 248 | fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti); |
Lucyjungz | 1:1f1f2b99756b | 249 | |
Lucyjungz | 1:1f1f2b99756b | 250 | printf("Done"); |
Lucyjungz | 3:6e08d0bba1bb | 251 | /* Close file once it done */ |
Lucyjungz | 3:6e08d0bba1bb | 252 | fclose(fp); |
Lucyjungz | 1:1f1f2b99756b | 253 | } |
Lucyjungz | 1:1f1f2b99756b | 254 | } |
Lucyjungz | 3:6e08d0bba1bb | 255 | /** |
Lucyjungz | 3:6e08d0bba1bb | 256 | * @brief Function to log RMS Data |
Lucyjungz | 3:6e08d0bba1bb | 257 | * @note sequence must be in the same order with variableList |
Lucyjungz | 3:6e08d0bba1bb | 258 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 259 | * @param var - float array to be log in the file |
Lucyjungz | 3:6e08d0bba1bb | 260 | * @param size - size of the float array |
Lucyjungz | 3:6e08d0bba1bb | 261 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 262 | */ |
Lucyjungz | 21:0c52dbc23f52 | 263 | void FILEMANAGER_LogRMSData(time_t timestamp ,float * var, int size, bool * p_headerRequired) |
Lucyjungz | 1:1f1f2b99756b | 264 | { |
Lucyjungz | 3:6e08d0bba1bb | 265 | /* Get File name */ |
Lucyjungz | 1:1f1f2b99756b | 266 | char file_name[] = RTL_LOG_FILE_NAME; |
Lucyjungz | 1:1f1f2b99756b | 267 | |
Lucyjungz | 3:6e08d0bba1bb | 268 | /* Generate File name with timestamp */ |
Lucyjungz | 14:4ba6147f067b | 269 | FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 21:0c52dbc23f52 | 270 | if (*p_headerRequired || !FILEMANAGER_IsFileExist(file_name)) |
Lucyjungz | 1:1f1f2b99756b | 271 | { |
Lucyjungz | 3:6e08d0bba1bb | 272 | /* If file is not exist, log the header */ |
Lucyjungz | 14:4ba6147f067b | 273 | FILEMANAGER_LogRMSHeader(timestamp); |
Lucyjungz | 21:0c52dbc23f52 | 274 | |
Lucyjungz | 21:0c52dbc23f52 | 275 | /* Clear flag once header has been written */ |
Lucyjungz | 21:0c52dbc23f52 | 276 | *p_headerRequired = false; |
Lucyjungz | 1:1f1f2b99756b | 277 | } |
Lucyjungz | 3:6e08d0bba1bb | 278 | /* Open file with "APPEND" mode */ |
Lucyjungz | 1:1f1f2b99756b | 279 | FILE *fp = fopen(file_name, "a"); |
nsrwsurasak | 0:a27e0d3581d1 | 280 | |
Lucyjungz | 3:6e08d0bba1bb | 281 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 282 | { |
Lucyjungz | 3:6e08d0bba1bb | 283 | /* In case of error, print the error message */ |
Lucyjungz | 1:1f1f2b99756b | 284 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 11:e21d4c5bfd1b | 285 | |
Lucyjungz | 11:e21d4c5bfd1b | 286 | /* Indicate LED Status (OFF)*/ |
Lucyjungz | 14:4ba6147f067b | 287 | FILEMANAGER_SetLedStatus(false); |
Lucyjungz | 3:6e08d0bba1bb | 288 | } |
Lucyjungz | 3:6e08d0bba1bb | 289 | else |
Lucyjungz | 3:6e08d0bba1bb | 290 | { |
Lucyjungz | 11:e21d4c5bfd1b | 291 | /* Indicate LED Status (ON)*/ |
Lucyjungz | 14:4ba6147f067b | 292 | FILEMANAGER_SetLedStatus(true); |
Lucyjungz | 11:e21d4c5bfd1b | 293 | |
Lucyjungz | 3:6e08d0bba1bb | 294 | /* Print some message for information */ |
Lucyjungz | 1:1f1f2b99756b | 295 | printf("\r\n Writing to Log File (%s)....",file_name); |
Lucyjungz | 1:1f1f2b99756b | 296 | |
Lucyjungz | 3:6e08d0bba1bb | 297 | /* Write timestamp */ |
nsrwsurasak | 22:2fc64ad66e35 | 298 | fprintf(fp, "%d,%d",timestamp,(msec.read_ms()%MSEC_SCALE)); |
Lucyjungz | 1:1f1f2b99756b | 299 | |
Lucyjungz | 3:6e08d0bba1bb | 300 | /* Write variable data */ |
Lucyjungz | 1:1f1f2b99756b | 301 | for(int i = 0; i < size; i++) |
Lucyjungz | 1:1f1f2b99756b | 302 | { |
nsrwsurasak | 19:5af5c60e7a9f | 303 | fprintf(fp, ",%g",var[i]); |
Lucyjungz | 1:1f1f2b99756b | 304 | } |
Lucyjungz | 3:6e08d0bba1bb | 305 | /* Write new line as we done */ |
Lucyjungz | 3:6e08d0bba1bb | 306 | fprintf(fp, "\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 307 | |
Lucyjungz | 1:1f1f2b99756b | 308 | printf("Done"); |
Lucyjungz | 3:6e08d0bba1bb | 309 | /* Close the file */ |
Lucyjungz | 3:6e08d0bba1bb | 310 | fclose(fp); |
Lucyjungz | 3:6e08d0bba1bb | 311 | } |
Lucyjungz | 3:6e08d0bba1bb | 312 | } |
Lucyjungz | 3:6e08d0bba1bb | 313 | /** |
Lucyjungz | 3:6e08d0bba1bb | 314 | * @brief Function to log RMS Header |
Lucyjungz | 3:6e08d0bba1bb | 315 | * @note sequence must be in the same order with variableList |
Lucyjungz | 3:6e08d0bba1bb | 316 | * @param timestamp - time structure to get Date |
Lucyjungz | 3:6e08d0bba1bb | 317 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 318 | */ |
Lucyjungz | 14:4ba6147f067b | 319 | void FILEMANAGER_LogRMSHeader(time_t timestamp) |
Lucyjungz | 3:6e08d0bba1bb | 320 | { |
Lucyjungz | 3:6e08d0bba1bb | 321 | /* Get File name */ |
Lucyjungz | 3:6e08d0bba1bb | 322 | char file_name[] = RTL_LOG_FILE_NAME; |
Lucyjungz | 3:6e08d0bba1bb | 323 | |
Lucyjungz | 3:6e08d0bba1bb | 324 | /* Generate file name with time */ |
Lucyjungz | 14:4ba6147f067b | 325 | FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name); |
Lucyjungz | 3:6e08d0bba1bb | 326 | |
Lucyjungz | 3:6e08d0bba1bb | 327 | /* Open file in append mode */ |
Lucyjungz | 3:6e08d0bba1bb | 328 | FILE *fp = fopen(file_name, "a"); |
Lucyjungz | 3:6e08d0bba1bb | 329 | |
Lucyjungz | 3:6e08d0bba1bb | 330 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 331 | { |
Lucyjungz | 3:6e08d0bba1bb | 332 | /* In case of error, print the error message */ |
Lucyjungz | 3:6e08d0bba1bb | 333 | printf("Error! Unable to open file %s!\n",file_name); |
Lucyjungz | 11:e21d4c5bfd1b | 334 | |
Lucyjungz | 11:e21d4c5bfd1b | 335 | /* Indicate LED Status (OFF)*/ |
Lucyjungz | 14:4ba6147f067b | 336 | FILEMANAGER_SetLedStatus(false); |
Lucyjungz | 3:6e08d0bba1bb | 337 | } |
Lucyjungz | 3:6e08d0bba1bb | 338 | else |
Lucyjungz | 3:6e08d0bba1bb | 339 | { |
Lucyjungz | 11:e21d4c5bfd1b | 340 | /* Indicate LED Status (ON)*/ |
Lucyjungz | 14:4ba6147f067b | 341 | FILEMANAGER_SetLedStatus(true); |
Lucyjungz | 11:e21d4c5bfd1b | 342 | |
Lucyjungz | 3:6e08d0bba1bb | 343 | /* opened file so can write */ |
Lucyjungz | 3:6e08d0bba1bb | 344 | printf("\r\n Writing to Log File (%s)....",file_name); |
Lucyjungz | 3:6e08d0bba1bb | 345 | |
Lucyjungz | 3:6e08d0bba1bb | 346 | /* Write the header to the file */ |
Lucyjungz | 14:4ba6147f067b | 347 | fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND); |
Lucyjungz | 7:ab015947e368 | 348 | |
Lucyjungz | 14:4ba6147f067b | 349 | for(int i = 0; i < m_amountVarList; i++) |
Lucyjungz | 14:4ba6147f067b | 350 | { |
Lucyjungz | 14:4ba6147f067b | 351 | fprintf(fp, ",%s",m_varList[i].varName); |
Lucyjungz | 14:4ba6147f067b | 352 | } |
Lucyjungz | 14:4ba6147f067b | 353 | /* Write new line as done */ |
Lucyjungz | 14:4ba6147f067b | 354 | fprintf(fp, "\n"); |
Lucyjungz | 14:4ba6147f067b | 355 | |
Lucyjungz | 14:4ba6147f067b | 356 | /* Write the timestamp unit to the file */ |
Lucyjungz | 14:4ba6147f067b | 357 | fprintf(fp, "-,-"); |
Lucyjungz | 14:4ba6147f067b | 358 | |
Lucyjungz | 15:b63a539c3754 | 359 | |
Lucyjungz | 15:b63a539c3754 | 360 | /* Write the unit of variables to the file */ |
Lucyjungz | 3:6e08d0bba1bb | 361 | for(int i = 0; i < m_amountVarList; i++) |
Lucyjungz | 3:6e08d0bba1bb | 362 | { |
Lucyjungz | 15:b63a539c3754 | 363 | fprintf(fp, ",%s",m_varList[i].varUnit); |
Lucyjungz | 3:6e08d0bba1bb | 364 | } |
Lucyjungz | 15:b63a539c3754 | 365 | |
Lucyjungz | 3:6e08d0bba1bb | 366 | /* Write new line as done */ |
Lucyjungz | 3:6e08d0bba1bb | 367 | fprintf(fp, "\n"); |
Lucyjungz | 3:6e08d0bba1bb | 368 | |
Lucyjungz | 3:6e08d0bba1bb | 369 | printf("Done"); |
Lucyjungz | 3:6e08d0bba1bb | 370 | |
Lucyjungz | 3:6e08d0bba1bb | 371 | /* Close the file */ |
Lucyjungz | 3:6e08d0bba1bb | 372 | fclose(fp); |
Lucyjungz | 3:6e08d0bba1bb | 373 | } |
Lucyjungz | 3:6e08d0bba1bb | 374 | } |
Lucyjungz | 3:6e08d0bba1bb | 375 | /** |
Lucyjungz | 3:6e08d0bba1bb | 376 | * @brief Function to log Mini RMS System Data |
Lucyjungz | 3:6e08d0bba1bb | 377 | * @note this file is only for debug |
Lucyjungz | 3:6e08d0bba1bb | 378 | * @param gps_interval - |
Lucyjungz | 3:6e08d0bba1bb | 379 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 380 | */ |
Lucyjungz | 14:4ba6147f067b | 381 | void FILEMANAGER_LogSystemData(float gps_interval) |
Lucyjungz | 3:6e08d0bba1bb | 382 | { |
Lucyjungz | 3:6e08d0bba1bb | 383 | /* Open the file in append mode */ |
Lucyjungz | 3:6e08d0bba1bb | 384 | FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); |
Lucyjungz | 3:6e08d0bba1bb | 385 | |
Lucyjungz | 3:6e08d0bba1bb | 386 | if (fp == NULL) { |
Lucyjungz | 3:6e08d0bba1bb | 387 | /* In case of error, print the msg */ |
Lucyjungz | 3:6e08d0bba1bb | 388 | printf("Error! Unable to open file!\n"); |
Lucyjungz | 11:e21d4c5bfd1b | 389 | |
Lucyjungz | 11:e21d4c5bfd1b | 390 | /* Indicate LED Status (OFF)*/ |
Lucyjungz | 14:4ba6147f067b | 391 | FILEMANAGER_SetLedStatus(false); |
Lucyjungz | 3:6e08d0bba1bb | 392 | } |
Lucyjungz | 3:6e08d0bba1bb | 393 | else |
Lucyjungz | 3:6e08d0bba1bb | 394 | { |
Lucyjungz | 11:e21d4c5bfd1b | 395 | /* Indicate LED Status (ON)*/ |
Lucyjungz | 14:4ba6147f067b | 396 | FILEMANAGER_SetLedStatus(true); |
Lucyjungz | 11:e21d4c5bfd1b | 397 | |
Lucyjungz | 3:6e08d0bba1bb | 398 | /* Write some message, which is TBD */ |
Lucyjungz | 3:6e08d0bba1bb | 399 | fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); |
Lucyjungz | 1:1f1f2b99756b | 400 | fclose(fp); // ensure you close the file after writing |
Lucyjungz | 1:1f1f2b99756b | 401 | } |
Lucyjungz | 1:1f1f2b99756b | 402 | } |
Lucyjungz | 3:6e08d0bba1bb | 403 | /** |
Lucyjungz | 3:6e08d0bba1bb | 404 | * @brief Utility to delete file |
Lucyjungz | 3:6e08d0bba1bb | 405 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 406 | * @param filename - file name to be deleted |
Lucyjungz | 3:6e08d0bba1bb | 407 | * @retval None |
Lucyjungz | 3:6e08d0bba1bb | 408 | */ |
Lucyjungz | 14:4ba6147f067b | 409 | void FILEMANAGER_Deletefile(char filename[]) |
nsrwsurasak | 0:a27e0d3581d1 | 410 | { |
nsrwsurasak | 0:a27e0d3581d1 | 411 | printf("Deleting file '%s'...",filename); |
nsrwsurasak | 0:a27e0d3581d1 | 412 | FILE *fp = fopen(filename, "r"); // try and open file |
nsrwsurasak | 0:a27e0d3581d1 | 413 | if (fp != NULL) { // if it does open... |
nsrwsurasak | 0:a27e0d3581d1 | 414 | fclose(fp); // close it |
nsrwsurasak | 0:a27e0d3581d1 | 415 | remove(filename); // and then delete |
nsrwsurasak | 0:a27e0d3581d1 | 416 | printf("Done!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 417 | } |
nsrwsurasak | 0:a27e0d3581d1 | 418 | // if we can't open it, it doesn't exist and so we can't delete it |
nsrwsurasak | 0:a27e0d3581d1 | 419 | } |
Lucyjungz | 3:6e08d0bba1bb | 420 | /** |
Lucyjungz | 3:6e08d0bba1bb | 421 | * @brief Utility to check file available |
Lucyjungz | 3:6e08d0bba1bb | 422 | * @note |
Lucyjungz | 3:6e08d0bba1bb | 423 | * @param filename - file name to be checked |
Lucyjungz | 3:6e08d0bba1bb | 424 | * @retval true - file is exist , false - file is not exist |
Lucyjungz | 3:6e08d0bba1bb | 425 | */ |
Lucyjungz | 14:4ba6147f067b | 426 | bool FILEMANAGER_IsFileExist(char filename[]) |
Lucyjungz | 1:1f1f2b99756b | 427 | { |
Lucyjungz | 1:1f1f2b99756b | 428 | bool exist = false; |
Lucyjungz | 1:1f1f2b99756b | 429 | FILE *fp = fopen(filename, "r"); // try and open file |
Lucyjungz | 1:1f1f2b99756b | 430 | if (fp != NULL) { // if it does open... |
Lucyjungz | 1:1f1f2b99756b | 431 | fclose(fp); // close it |
Lucyjungz | 1:1f1f2b99756b | 432 | exist = true; |
Lucyjungz | 1:1f1f2b99756b | 433 | } |
Lucyjungz | 1:1f1f2b99756b | 434 | |
Lucyjungz | 1:1f1f2b99756b | 435 | return exist; |
Lucyjungz | 1:1f1f2b99756b | 436 | } |
Lucyjungz | 3:6e08d0bba1bb | 437 | /** |
Lucyjungz | 3:6e08d0bba1bb | 438 | * @brief Utility to get GPS Interval |
Lucyjungz | 14:4ba6147f067b | 439 | * @note must be called after read the setup file |
Lucyjungz | 3:6e08d0bba1bb | 440 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 441 | * @retval GPS interval |
Lucyjungz | 3:6e08d0bba1bb | 442 | */ |
Lucyjungz | 14:4ba6147f067b | 443 | int FILEMANAGER_GPSInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 444 | { |
Lucyjungz | 3:6e08d0bba1bb | 445 | /* Return interval in int */ |
Lucyjungz | 14:4ba6147f067b | 446 | return ( m_GpsInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 447 | } |
Lucyjungz | 3:6e08d0bba1bb | 448 | /** |
Lucyjungz | 3:6e08d0bba1bb | 449 | * @brief Utility to get Data Interval |
Lucyjungz | 14:4ba6147f067b | 450 | * @note must be called after read the setup file |
Lucyjungz | 3:6e08d0bba1bb | 451 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 452 | * @retval Data interval |
Lucyjungz | 3:6e08d0bba1bb | 453 | */ |
Lucyjungz | 14:4ba6147f067b | 454 | int FILEMANAGER_DataInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 455 | { |
Lucyjungz | 3:6e08d0bba1bb | 456 | /* Return interval in int */ |
Lucyjungz | 14:4ba6147f067b | 457 | return ( m_DataInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 458 | } |
Lucyjungz | 3:6e08d0bba1bb | 459 | /** |
Lucyjungz | 3:6e08d0bba1bb | 460 | * @brief Function to read the variable list file |
Lucyjungz | 3:6e08d0bba1bb | 461 | * @note Recommended to call this function at initilization phase |
Lucyjungz | 3:6e08d0bba1bb | 462 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 463 | * @retval pointer to variable list |
Lucyjungz | 3:6e08d0bba1bb | 464 | */ |
Lucyjungz | 14:4ba6147f067b | 465 | Variable_Data_TypeDef * FILEMANAGER_ReadVarFile() |
nsrwsurasak | 0:a27e0d3581d1 | 466 | { |
Lucyjungz | 3:6e08d0bba1bb | 467 | /* Open the file with reading mode */ |
Lucyjungz | 21:0c52dbc23f52 | 468 | FILE *fp = fopen(m_varListFileName, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 469 | |
Lucyjungz | 3:6e08d0bba1bb | 470 | if (fp == NULL) |
Lucyjungz | 3:6e08d0bba1bb | 471 | { |
Lucyjungz | 3:6e08d0bba1bb | 472 | /* if it can't open the file then print error message */ |
Lucyjungz | 21:0c52dbc23f52 | 473 | printf("\nError! Unable to open file! %s \n", m_varListFileName); |
Lucyjungz | 11:e21d4c5bfd1b | 474 | /* Indicate LED Status (OFF)*/ |
Lucyjungz | 14:4ba6147f067b | 475 | FILEMANAGER_SetLedStatus(false); |
Lucyjungz | 11:e21d4c5bfd1b | 476 | |
nsrwsurasak | 0:a27e0d3581d1 | 477 | return NULL; |
Lucyjungz | 3:6e08d0bba1bb | 478 | } |
Lucyjungz | 3:6e08d0bba1bb | 479 | else |
Lucyjungz | 3:6e08d0bba1bb | 480 | { |
Lucyjungz | 11:e21d4c5bfd1b | 481 | /* Indicate LED Status (ON)*/ |
Lucyjungz | 14:4ba6147f067b | 482 | FILEMANAGER_SetLedStatus(true); |
nsrwsurasak | 0:a27e0d3581d1 | 483 | |
Lucyjungz | 3:6e08d0bba1bb | 484 | /* Allocate buffer for reading */ |
Lucyjungz | 14:4ba6147f067b | 485 | char buf[READ_FILE_BUFFER_SIZE]; |
nsrwsurasak | 0:a27e0d3581d1 | 486 | int index = 0; |
Lucyjungz | 3:6e08d0bba1bb | 487 | |
Lucyjungz | 3:6e08d0bba1bb | 488 | /* Initialize return value */ |
nsrwsurasak | 0:a27e0d3581d1 | 489 | memset(m_varList, ' ', sizeof(m_varList)); |
Lucyjungz | 3:6e08d0bba1bb | 490 | |
Lucyjungz | 3:6e08d0bba1bb | 491 | /* Read line from the file */ |
Lucyjungz | 3:6e08d0bba1bb | 492 | while (fgets(buf, sizeof(buf), fp) != NULL) |
Lucyjungz | 3:6e08d0bba1bb | 493 | { |
Lucyjungz | 3:6e08d0bba1bb | 494 | /* Check the TAG */ |
Lucyjungz | 3:6e08d0bba1bb | 495 | if (strstr (buf,VAR_NAME_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 496 | { |
Lucyjungz | 3:6e08d0bba1bb | 497 | /* Found variable TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 498 | FILEMANAGER_GetXmlText(buf , m_varList[index].varName); |
Lucyjungz | 3:6e08d0bba1bb | 499 | } |
Lucyjungz | 3:6e08d0bba1bb | 500 | else if (strstr (buf,VAR_ADDR_TAG)) |
Lucyjungz | 3:6e08d0bba1bb | 501 | { |
Lucyjungz | 3:6e08d0bba1bb | 502 | /* Found variable address TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 503 | FILEMANAGER_GetXmlText(buf , m_varList[index].varAddress); |
Lucyjungz | 5:7c513eee7b2b | 504 | } |
Lucyjungz | 10:a8003d357cf2 | 505 | else if (strstr (buf,VAR_TYPE_TAG)) |
Lucyjungz | 10:a8003d357cf2 | 506 | { |
Lucyjungz | 10:a8003d357cf2 | 507 | /* Found variable type TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 508 | FILEMANAGER_GetXmlText(buf , m_varList[index].varType); |
Lucyjungz | 10:a8003d357cf2 | 509 | } |
Lucyjungz | 6:5bd75c0f607c | 510 | else if (strstr (buf,VAR_LSB1_TAG)) |
Lucyjungz | 6:5bd75c0f607c | 511 | { |
Lucyjungz | 6:5bd75c0f607c | 512 | /* Found variable LSB1 TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 513 | FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB1); |
Lucyjungz | 6:5bd75c0f607c | 514 | } |
Lucyjungz | 6:5bd75c0f607c | 515 | else if (strstr (buf,VAR_LSB2_TAG)) |
Lucyjungz | 5:7c513eee7b2b | 516 | { |
Lucyjungz | 6:5bd75c0f607c | 517 | /* Found variable LSB2 TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 518 | FILEMANAGER_GetXmlText(buf , m_varList[index].varLSB2); |
Lucyjungz | 6:5bd75c0f607c | 519 | } |
Lucyjungz | 6:5bd75c0f607c | 520 | else if (strstr (buf,VAR_BITMASK_TAG)) |
Lucyjungz | 6:5bd75c0f607c | 521 | { |
Lucyjungz | 6:5bd75c0f607c | 522 | /* Found variable BitMask TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 523 | FILEMANAGER_GetXmlText(buf , m_varList[index].varBitMask); |
Lucyjungz | 5:7c513eee7b2b | 524 | } |
Lucyjungz | 5:7c513eee7b2b | 525 | else if (strstr (buf,VAR_UNIT_TAG)) |
Lucyjungz | 5:7c513eee7b2b | 526 | { |
Lucyjungz | 5:7c513eee7b2b | 527 | /* Found variable unit TAG, populate it*/ |
Lucyjungz | 14:4ba6147f067b | 528 | FILEMANAGER_GetXmlText(buf , m_varList[index].varUnit); |
nsrwsurasak | 0:a27e0d3581d1 | 529 | index++; |
nsrwsurasak | 0:a27e0d3581d1 | 530 | } |
nsrwsurasak | 0:a27e0d3581d1 | 531 | |
nsrwsurasak | 0:a27e0d3581d1 | 532 | } |
Lucyjungz | 3:6e08d0bba1bb | 533 | /* Close File */ |
Lucyjungz | 3:6e08d0bba1bb | 534 | fclose(fp); |
Lucyjungz | 3:6e08d0bba1bb | 535 | |
Lucyjungz | 3:6e08d0bba1bb | 536 | /* Populate amount */ |
nsrwsurasak | 0:a27e0d3581d1 | 537 | m_amountVarList = index; |
Lucyjungz | 3:6e08d0bba1bb | 538 | |
Lucyjungz | 3:6e08d0bba1bb | 539 | /* Return variable list */ |
nsrwsurasak | 0:a27e0d3581d1 | 540 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 541 | } |
nsrwsurasak | 0:a27e0d3581d1 | 542 | } |
Lucyjungz | 3:6e08d0bba1bb | 543 | /** |
Lucyjungz | 3:6e08d0bba1bb | 544 | * @brief Function to amount of variable for data logging |
Lucyjungz | 3:6e08d0bba1bb | 545 | * @note Must be called after readVarFile |
Lucyjungz | 3:6e08d0bba1bb | 546 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 547 | * @retval amount of variable list |
Lucyjungz | 3:6e08d0bba1bb | 548 | */ |
Lucyjungz | 14:4ba6147f067b | 549 | int FILEMANAGER_GetAmountVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 550 | { |
nsrwsurasak | 0:a27e0d3581d1 | 551 | return m_amountVarList; |
nsrwsurasak | 0:a27e0d3581d1 | 552 | } |
Lucyjungz | 3:6e08d0bba1bb | 553 | /** |
Lucyjungz | 3:6e08d0bba1bb | 554 | * @brief Function to get variable list |
Lucyjungz | 3:6e08d0bba1bb | 555 | * @note Must be called after readVarFile |
Lucyjungz | 3:6e08d0bba1bb | 556 | * @param None |
Lucyjungz | 3:6e08d0bba1bb | 557 | * @retval pointer to variable list |
Lucyjungz | 3:6e08d0bba1bb | 558 | */ |
Lucyjungz | 14:4ba6147f067b | 559 | Variable_Data_TypeDef * FILEMANAGER_GetVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 560 | { |
nsrwsurasak | 0:a27e0d3581d1 | 561 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 562 | } |
Lucyjungz | 14:4ba6147f067b | 563 | |
Lucyjungz | 11:e21d4c5bfd1b | 564 | /** |
Lucyjungz | 11:e21d4c5bfd1b | 565 | * @brief Utility to play with LED status |
Lucyjungz | 11:e21d4c5bfd1b | 566 | * @note Libary user need to assign proper PinName to LED_SDCARD |
Lucyjungz | 11:e21d4c5bfd1b | 567 | * @param on - True for turning LED ON, otherwise LED off |
Lucyjungz | 11:e21d4c5bfd1b | 568 | * @retval None |
Lucyjungz | 11:e21d4c5bfd1b | 569 | */ |
Lucyjungz | 14:4ba6147f067b | 570 | static void FILEMANAGER_SetLedStatus(bool on) |
Lucyjungz | 11:e21d4c5bfd1b | 571 | { |
Lucyjungz | 12:0045fca3c160 | 572 | /* Check LED Connection */ |
nsrwsurasak | 22:2fc64ad66e35 | 573 | if (ledStatus.is_connected()) |
Lucyjungz | 11:e21d4c5bfd1b | 574 | { |
Lucyjungz | 21:0c52dbc23f52 | 575 | /* Set LED regarding to given argment */ |
nsrwsurasak | 22:2fc64ad66e35 | 576 | if (m_sdCardIsPresent) |
nsrwsurasak | 22:2fc64ad66e35 | 577 | { |
nsrwsurasak | 22:2fc64ad66e35 | 578 | ledStatus = on; |
nsrwsurasak | 22:2fc64ad66e35 | 579 | } |
nsrwsurasak | 22:2fc64ad66e35 | 580 | else |
nsrwsurasak | 22:2fc64ad66e35 | 581 | { |
nsrwsurasak | 22:2fc64ad66e35 | 582 | ledStatus = false; |
nsrwsurasak | 22:2fc64ad66e35 | 583 | } |
nsrwsurasak | 22:2fc64ad66e35 | 584 | |
Lucyjungz | 12:0045fca3c160 | 585 | } |
Lucyjungz | 12:0045fca3c160 | 586 | #if DEBUG |
Lucyjungz | 12:0045fca3c160 | 587 | else |
Lucyjungz | 12:0045fca3c160 | 588 | { |
Lucyjungz | 14:4ba6147f067b | 589 | printf("\r\nSDCard LED is not connected !!"); |
Lucyjungz | 12:0045fca3c160 | 590 | } |
Lucyjungz | 12:0045fca3c160 | 591 | #endif |
Lucyjungz | 11:e21d4c5bfd1b | 592 | } |
nsrwsurasak | 22:2fc64ad66e35 | 593 | #if 0 |
Lucyjungz | 21:0c52dbc23f52 | 594 | /** |
Lucyjungz | 21:0c52dbc23f52 | 595 | * @brief Function to init File Manager |
Lucyjungz | 21:0c52dbc23f52 | 596 | * @note |
Lucyjungz | 21:0c52dbc23f52 | 597 | * @param |
Lucyjungz | 21:0c52dbc23f52 | 598 | * @retval None |
Lucyjungz | 21:0c52dbc23f52 | 599 | */ |
Lucyjungz | 21:0c52dbc23f52 | 600 | void FILEMANAGER_Init() |
Lucyjungz | 21:0c52dbc23f52 | 601 | { |
Lucyjungz | 21:0c52dbc23f52 | 602 | if (sdSwitch) |
Lucyjungz | 21:0c52dbc23f52 | 603 | { |
Lucyjungz | 21:0c52dbc23f52 | 604 | m_sdCardIsPresent = true; |
Lucyjungz | 21:0c52dbc23f52 | 605 | } |
Lucyjungz | 21:0c52dbc23f52 | 606 | FILEMANAGER_SetLedStatus(true); |
nsrwsurasak | 22:2fc64ad66e35 | 607 | |
nsrwsurasak | 22:2fc64ad66e35 | 608 | printf("\r\n ===== SDCard Init !!"); |
nsrwsurasak | 22:2fc64ad66e35 | 609 | sdSwitch.rise(&FILEMANAGER_SDConnect); |
Lucyjungz | 21:0c52dbc23f52 | 610 | } |
Lucyjungz | 21:0c52dbc23f52 | 611 | /** |
Lucyjungz | 21:0c52dbc23f52 | 612 | * @brief Interrupt function for sd card disconnect |
Lucyjungz | 21:0c52dbc23f52 | 613 | * @note |
Lucyjungz | 21:0c52dbc23f52 | 614 | * @param |
Lucyjungz | 21:0c52dbc23f52 | 615 | * @retval None |
Lucyjungz | 21:0c52dbc23f52 | 616 | */ |
Lucyjungz | 21:0c52dbc23f52 | 617 | void FILEMANAGER_SDDisconnect() |
Lucyjungz | 21:0c52dbc23f52 | 618 | { |
nsrwsurasak | 22:2fc64ad66e35 | 619 | printf("\r\n ===== SDCard Disconnect !!"); |
nsrwsurasak | 22:2fc64ad66e35 | 620 | wait_ms(20); |
Lucyjungz | 21:0c52dbc23f52 | 621 | m_sdCardIsPresent = false; |
Lucyjungz | 21:0c52dbc23f52 | 622 | FILEMANAGER_SetLedStatus(false); |
nsrwsurasak | 22:2fc64ad66e35 | 623 | //sdSwitch.rise(&FILEMANAGER_SDConnect); |
Lucyjungz | 21:0c52dbc23f52 | 624 | |
Lucyjungz | 15:b63a539c3754 | 625 | |
Lucyjungz | 21:0c52dbc23f52 | 626 | } |
Lucyjungz | 21:0c52dbc23f52 | 627 | /** |
Lucyjungz | 21:0c52dbc23f52 | 628 | * @brief Interrupt function for sd card connect |
Lucyjungz | 21:0c52dbc23f52 | 629 | * @note |
Lucyjungz | 21:0c52dbc23f52 | 630 | * @param |
Lucyjungz | 21:0c52dbc23f52 | 631 | * @retval None |
Lucyjungz | 21:0c52dbc23f52 | 632 | */ |
Lucyjungz | 21:0c52dbc23f52 | 633 | void FILEMANAGER_SDConnect() |
Lucyjungz | 21:0c52dbc23f52 | 634 | { |
nsrwsurasak | 22:2fc64ad66e35 | 635 | printf("\r\n ===== SDCard Connect !!"); |
Lucyjungz | 21:0c52dbc23f52 | 636 | m_sdCardIsPresent = true; |
Lucyjungz | 21:0c52dbc23f52 | 637 | FILEMANAGER_SetLedStatus(true); |
nsrwsurasak | 22:2fc64ad66e35 | 638 | //sdSwitch.fall(&FILEMANAGER_SDDisconnect); |
Lucyjungz | 21:0c52dbc23f52 | 639 | NVIC_SystemReset(); |
Lucyjungz | 21:0c52dbc23f52 | 640 | } |
Lucyjungz | 21:0c52dbc23f52 | 641 | #endif |