Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FileManager GPSGms6 SDFileSystem mbed
Fork of 2545_SD_Card by
FileManager.cpp@4:aa7ac2ac6913, 2016-05-09 (annotated)
- Committer:
- Lucyjungz
- Date:
- Mon May 09 08:59:23 2016 +0000
- Revision:
- 4:aa7ac2ac6913
- Parent:
- 2:c96b02fcb98e
- Child:
- 5:07aaa6e3784c
read variable list from another xml file
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Lucyjungz | 2:c96b02fcb98e | 1 | #include "FileManager.h" |
| Lucyjungz | 2:c96b02fcb98e | 2 | #include "SDFileSystem.h" |
| Lucyjungz | 2:c96b02fcb98e | 3 | |
| Lucyjungz | 2:c96b02fcb98e | 4 | char m_GpsInterval[XMLTEXT_SIZE]; |
| Lucyjungz | 2:c96b02fcb98e | 5 | char m_DataInterval[XMLTEXT_SIZE]; |
| Lucyjungz | 4:aa7ac2ac6913 | 6 | Variable_Data_TypeDef m_varList[MAX_VAR]; |
| Lucyjungz | 4:aa7ac2ac6913 | 7 | unsigned int m_amountVarList; |
| Lucyjungz | 2:c96b02fcb98e | 8 | |
| Lucyjungz | 2:c96b02fcb98e | 9 | static void removeSpaces(char* s , int size); |
| Lucyjungz | 2:c96b02fcb98e | 10 | static void getXmlText(char *str, char *ret); |
| Lucyjungz | 2:c96b02fcb98e | 11 | |
| Lucyjungz | 2:c96b02fcb98e | 12 | |
| Lucyjungz | 2:c96b02fcb98e | 13 | static void removeSpaces(char* s , int size) |
| Lucyjungz | 2:c96b02fcb98e | 14 | { |
| Lucyjungz | 2:c96b02fcb98e | 15 | char* cpy = s; // an alias to iterate through s without moving s |
| Lucyjungz | 2:c96b02fcb98e | 16 | char* temp = s; |
| Lucyjungz | 2:c96b02fcb98e | 17 | |
| Lucyjungz | 2:c96b02fcb98e | 18 | for (int i = 0 ; i < size ; i++) |
| Lucyjungz | 2:c96b02fcb98e | 19 | { |
| Lucyjungz | 2:c96b02fcb98e | 20 | if (*cpy != ' ') |
| Lucyjungz | 2:c96b02fcb98e | 21 | *temp++ = *cpy; |
| Lucyjungz | 2:c96b02fcb98e | 22 | cpy++; |
| Lucyjungz | 2:c96b02fcb98e | 23 | } |
| Lucyjungz | 2:c96b02fcb98e | 24 | *temp = 0; |
| Lucyjungz | 2:c96b02fcb98e | 25 | return; |
| Lucyjungz | 2:c96b02fcb98e | 26 | } |
| Lucyjungz | 2:c96b02fcb98e | 27 | static void getXmlText(char *str, char *ret) |
| Lucyjungz | 2:c96b02fcb98e | 28 | { |
| Lucyjungz | 2:c96b02fcb98e | 29 | int size = strlen(str); |
| Lucyjungz | 2:c96b02fcb98e | 30 | int i; |
| Lucyjungz | 2:c96b02fcb98e | 31 | bool begin_text = false; |
| Lucyjungz | 2:c96b02fcb98e | 32 | char * ret_addr = ret; |
| Lucyjungz | 2:c96b02fcb98e | 33 | memset (ret,' ',XMLTEXT_SIZE); |
| Lucyjungz | 2:c96b02fcb98e | 34 | |
| Lucyjungz | 2:c96b02fcb98e | 35 | for(i = 0; i < size ; i++) |
| Lucyjungz | 2:c96b02fcb98e | 36 | { |
| Lucyjungz | 2:c96b02fcb98e | 37 | |
| Lucyjungz | 2:c96b02fcb98e | 38 | if (*str == '>') |
| Lucyjungz | 2:c96b02fcb98e | 39 | { |
| Lucyjungz | 2:c96b02fcb98e | 40 | begin_text = true; |
| Lucyjungz | 2:c96b02fcb98e | 41 | } |
| Lucyjungz | 2:c96b02fcb98e | 42 | else if (begin_text && *str == '<') |
| Lucyjungz | 2:c96b02fcb98e | 43 | { |
| Lucyjungz | 2:c96b02fcb98e | 44 | begin_text = false; |
| Lucyjungz | 2:c96b02fcb98e | 45 | break; |
| Lucyjungz | 2:c96b02fcb98e | 46 | } |
| Lucyjungz | 2:c96b02fcb98e | 47 | else if (begin_text && *str != ' ') |
| Lucyjungz | 2:c96b02fcb98e | 48 | { |
| Lucyjungz | 2:c96b02fcb98e | 49 | *ret = *str; |
| Lucyjungz | 2:c96b02fcb98e | 50 | ret++; |
| Lucyjungz | 2:c96b02fcb98e | 51 | } |
| Lucyjungz | 2:c96b02fcb98e | 52 | |
| Lucyjungz | 2:c96b02fcb98e | 53 | str++; |
| Lucyjungz | 2:c96b02fcb98e | 54 | } |
| Lucyjungz | 2:c96b02fcb98e | 55 | removeSpaces(ret_addr, XMLTEXT_SIZE); |
| Lucyjungz | 2:c96b02fcb98e | 56 | } |
| Lucyjungz | 2:c96b02fcb98e | 57 | void readSetupFile() |
| Lucyjungz | 2:c96b02fcb98e | 58 | { |
| Lucyjungz | 2:c96b02fcb98e | 59 | // now open file for reading |
| Lucyjungz | 2:c96b02fcb98e | 60 | FILE *fp = fopen(SETUP_FILE_NAME, "r"); |
| Lucyjungz | 2:c96b02fcb98e | 61 | |
| Lucyjungz | 2:c96b02fcb98e | 62 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 2:c96b02fcb98e | 63 | printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); |
| Lucyjungz | 2:c96b02fcb98e | 64 | } else { // opened file so can write |
| Lucyjungz | 2:c96b02fcb98e | 65 | // fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
| Lucyjungz | 2:c96b02fcb98e | 66 | // serial.printf("Read %d from file.\n",stored_top_score); |
| Lucyjungz | 2:c96b02fcb98e | 67 | |
| Lucyjungz | 2:c96b02fcb98e | 68 | ReadingFileState state = STATE_FINDING; |
| Lucyjungz | 2:c96b02fcb98e | 69 | char buf[1024]; |
| Lucyjungz | 2:c96b02fcb98e | 70 | while (fgets(buf, sizeof(buf), fp) != NULL) |
| Lucyjungz | 2:c96b02fcb98e | 71 | { |
| Lucyjungz | 2:c96b02fcb98e | 72 | if (strstr (buf,DATA_TAG)) |
| Lucyjungz | 2:c96b02fcb98e | 73 | { |
| Lucyjungz | 2:c96b02fcb98e | 74 | state = STATE_FOUND_DATA; |
| Lucyjungz | 2:c96b02fcb98e | 75 | } |
| Lucyjungz | 2:c96b02fcb98e | 76 | else if (strstr (buf,GPS_TAG)) |
| Lucyjungz | 2:c96b02fcb98e | 77 | { |
| Lucyjungz | 2:c96b02fcb98e | 78 | state = STATE_FOUND_GPS; |
| Lucyjungz | 2:c96b02fcb98e | 79 | } |
| Lucyjungz | 2:c96b02fcb98e | 80 | else if (strstr (buf,UPDATE_INTERVAL_TAG)) |
| Lucyjungz | 2:c96b02fcb98e | 81 | { |
| Lucyjungz | 2:c96b02fcb98e | 82 | if (state == STATE_FOUND_GPS) |
| Lucyjungz | 2:c96b02fcb98e | 83 | { |
| Lucyjungz | 2:c96b02fcb98e | 84 | getXmlText(buf, m_GpsInterval); |
| Lucyjungz | 2:c96b02fcb98e | 85 | printf("\r\n-found GPS interval %s ", m_GpsInterval); |
| Lucyjungz | 2:c96b02fcb98e | 86 | state = STATE_FINDING; |
| Lucyjungz | 2:c96b02fcb98e | 87 | } |
| Lucyjungz | 2:c96b02fcb98e | 88 | else if(state == STATE_FOUND_DATA) |
| Lucyjungz | 2:c96b02fcb98e | 89 | { |
| Lucyjungz | 2:c96b02fcb98e | 90 | getXmlText(buf, m_DataInterval); |
| Lucyjungz | 2:c96b02fcb98e | 91 | printf("\r\n-found Data interval %s ", m_DataInterval); |
| Lucyjungz | 2:c96b02fcb98e | 92 | state = STATE_FINDING; |
| Lucyjungz | 2:c96b02fcb98e | 93 | } |
| Lucyjungz | 2:c96b02fcb98e | 94 | } |
| Lucyjungz | 2:c96b02fcb98e | 95 | } |
| Lucyjungz | 2:c96b02fcb98e | 96 | fclose(fp); // ensure you close the file after reading |
| Lucyjungz | 2:c96b02fcb98e | 97 | } |
| Lucyjungz | 2:c96b02fcb98e | 98 | } |
| Lucyjungz | 2:c96b02fcb98e | 99 | void logGPSData(char date[], char time[]) |
| Lucyjungz | 2:c96b02fcb98e | 100 | { |
| Lucyjungz | 2:c96b02fcb98e | 101 | FILE *fp = fopen(GPS_LOG_FILE_NAME, "a"); |
| Lucyjungz | 2:c96b02fcb98e | 102 | |
| Lucyjungz | 2:c96b02fcb98e | 103 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 2:c96b02fcb98e | 104 | printf("Error! Unable to open file!\n"); |
| Lucyjungz | 2:c96b02fcb98e | 105 | } else { // opened file so can write |
| Lucyjungz | 4:aa7ac2ac6913 | 106 | printf("\r\n Writing to Gps Log File...."); |
| Lucyjungz | 2:c96b02fcb98e | 107 | |
| Lucyjungz | 2:c96b02fcb98e | 108 | fprintf(fp, "%s,%s\n",date,time); // print formatted string to file (CSV) |
| Lucyjungz | 2:c96b02fcb98e | 109 | |
| Lucyjungz | 2:c96b02fcb98e | 110 | printf("Done"); |
| Lucyjungz | 2:c96b02fcb98e | 111 | fclose(fp); // ensure you close the file after writing |
| Lucyjungz | 2:c96b02fcb98e | 112 | } |
| Lucyjungz | 2:c96b02fcb98e | 113 | } |
| Lucyjungz | 2:c96b02fcb98e | 114 | void logSystemData(float gps_interval) |
| Lucyjungz | 2:c96b02fcb98e | 115 | { |
| Lucyjungz | 2:c96b02fcb98e | 116 | FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); |
| Lucyjungz | 2:c96b02fcb98e | 117 | |
| Lucyjungz | 2:c96b02fcb98e | 118 | |
| Lucyjungz | 2:c96b02fcb98e | 119 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 2:c96b02fcb98e | 120 | printf("Error! Unable to open file!\n"); |
| Lucyjungz | 2:c96b02fcb98e | 121 | } else { // opened file so can write |
| Lucyjungz | 2:c96b02fcb98e | 122 | fprintf(fp, "Start Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches |
| Lucyjungz | 2:c96b02fcb98e | 123 | fclose(fp); // ensure you close the file after writing |
| Lucyjungz | 2:c96b02fcb98e | 124 | } |
| Lucyjungz | 2:c96b02fcb98e | 125 | } |
| Lucyjungz | 2:c96b02fcb98e | 126 | void delete_file(char filename[]) |
| Lucyjungz | 2:c96b02fcb98e | 127 | { |
| Lucyjungz | 2:c96b02fcb98e | 128 | printf("Deleting file '%s'...",filename); |
| Lucyjungz | 2:c96b02fcb98e | 129 | FILE *fp = fopen(filename, "r"); // try and open file |
| Lucyjungz | 2:c96b02fcb98e | 130 | if (fp != NULL) { // if it does open... |
| Lucyjungz | 2:c96b02fcb98e | 131 | fclose(fp); // close it |
| Lucyjungz | 2:c96b02fcb98e | 132 | remove(filename); // and then delete |
| Lucyjungz | 2:c96b02fcb98e | 133 | printf("Done!\n"); |
| Lucyjungz | 2:c96b02fcb98e | 134 | } |
| Lucyjungz | 2:c96b02fcb98e | 135 | // if we can't open it, it doesn't exist and so we can't delete it |
| Lucyjungz | 2:c96b02fcb98e | 136 | } |
| Lucyjungz | 2:c96b02fcb98e | 137 | int GPSInterval() |
| Lucyjungz | 2:c96b02fcb98e | 138 | { |
| Lucyjungz | 2:c96b02fcb98e | 139 | //Return whether or not CRC is enabled |
| Lucyjungz | 2:c96b02fcb98e | 140 | return atoi( m_GpsInterval ); |
| Lucyjungz | 2:c96b02fcb98e | 141 | } |
| Lucyjungz | 2:c96b02fcb98e | 142 | int DataInterval() |
| Lucyjungz | 2:c96b02fcb98e | 143 | { |
| Lucyjungz | 2:c96b02fcb98e | 144 | //Return whether or not CRC is enabled |
| Lucyjungz | 2:c96b02fcb98e | 145 | return atoi( m_DataInterval ); |
| Lucyjungz | 4:aa7ac2ac6913 | 146 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 147 | |
| Lucyjungz | 4:aa7ac2ac6913 | 148 | Variable_Data_TypeDef * readVarFile() |
| Lucyjungz | 4:aa7ac2ac6913 | 149 | { |
| Lucyjungz | 4:aa7ac2ac6913 | 150 | // now open file for reading |
| Lucyjungz | 4:aa7ac2ac6913 | 151 | FILE *fp = fopen(VARIABLE_FILE_NAME, "r"); |
| Lucyjungz | 4:aa7ac2ac6913 | 152 | |
| Lucyjungz | 4:aa7ac2ac6913 | 153 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 4:aa7ac2ac6913 | 154 | printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME); |
| Lucyjungz | 4:aa7ac2ac6913 | 155 | return NULL; |
| Lucyjungz | 4:aa7ac2ac6913 | 156 | } else { // opened file so can write |
| Lucyjungz | 4:aa7ac2ac6913 | 157 | |
| Lucyjungz | 4:aa7ac2ac6913 | 158 | char buf[1024]; |
| Lucyjungz | 4:aa7ac2ac6913 | 159 | int index = 0; |
| Lucyjungz | 4:aa7ac2ac6913 | 160 | memset(m_varList, ' ', sizeof(m_varList)); |
| Lucyjungz | 4:aa7ac2ac6913 | 161 | while (fgets(buf, sizeof(buf), fp) != NULL) |
| Lucyjungz | 4:aa7ac2ac6913 | 162 | { |
| Lucyjungz | 4:aa7ac2ac6913 | 163 | if (strstr (buf,VAR_NAME_TAG)) |
| Lucyjungz | 4:aa7ac2ac6913 | 164 | { |
| Lucyjungz | 4:aa7ac2ac6913 | 165 | getXmlText(buf , m_varList[index].varName); |
| Lucyjungz | 4:aa7ac2ac6913 | 166 | |
| Lucyjungz | 4:aa7ac2ac6913 | 167 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 168 | else if (strstr (buf,VAR_ADDR_TAG)) |
| Lucyjungz | 4:aa7ac2ac6913 | 169 | { |
| Lucyjungz | 4:aa7ac2ac6913 | 170 | getXmlText(buf , m_varList[index].varAddress); |
| Lucyjungz | 4:aa7ac2ac6913 | 171 | index++; |
| Lucyjungz | 4:aa7ac2ac6913 | 172 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 173 | |
| Lucyjungz | 4:aa7ac2ac6913 | 174 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 175 | fclose(fp); // ensure you close the file after reading |
| Lucyjungz | 4:aa7ac2ac6913 | 176 | m_amountVarList = index; |
| Lucyjungz | 4:aa7ac2ac6913 | 177 | return m_varList; |
| Lucyjungz | 4:aa7ac2ac6913 | 178 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 179 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 180 | int getVarListAmount(){ |
| Lucyjungz | 4:aa7ac2ac6913 | 181 | return m_amountVarList; |
| Lucyjungz | 4:aa7ac2ac6913 | 182 | } |
| Lucyjungz | 4:aa7ac2ac6913 | 183 |
