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