Lucy Luz / Mbed 2 deprecated RwSDCard_Xml_GPS

Dependencies:   FileManager GPSGms6 SDFileSystem mbed

Fork of 2545_SD_Card by Craig Evans

Committer:
Lucyjungz
Date:
Mon May 09 09:31:44 2016 +0000
Revision:
6:a05ec997c496
Parent:
5:07aaa6e3784c
Formatted Code

Who changed what in which revision?

UserRevisionLine numberNew 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 6:a05ec997c496 18 for (int i = 0 ; i < size ; i++) {
Lucyjungz 2:c96b02fcb98e 19 if (*cpy != ' ')
Lucyjungz 2:c96b02fcb98e 20 *temp++ = *cpy;
Lucyjungz 2:c96b02fcb98e 21 cpy++;
Lucyjungz 2:c96b02fcb98e 22 }
Lucyjungz 2:c96b02fcb98e 23 *temp = 0;
Lucyjungz 2:c96b02fcb98e 24 return;
Lucyjungz 6:a05ec997c496 25 }
Lucyjungz 2:c96b02fcb98e 26 static void getXmlText(char *str, char *ret)
Lucyjungz 2:c96b02fcb98e 27 {
Lucyjungz 2:c96b02fcb98e 28 int size = strlen(str);
Lucyjungz 2:c96b02fcb98e 29 int i;
Lucyjungz 2:c96b02fcb98e 30 bool begin_text = false;
Lucyjungz 2:c96b02fcb98e 31 char * ret_addr = ret;
Lucyjungz 2:c96b02fcb98e 32 memset (ret,' ',XMLTEXT_SIZE);
Lucyjungz 6:a05ec997c496 33
Lucyjungz 6:a05ec997c496 34 for(i = 0; i < size ; i++) {
Lucyjungz 6:a05ec997c496 35
Lucyjungz 6:a05ec997c496 36 if (*str == '>') {
Lucyjungz 2:c96b02fcb98e 37 begin_text = true;
Lucyjungz 6:a05ec997c496 38 } else if (begin_text && *str == '<') {
Lucyjungz 2:c96b02fcb98e 39 begin_text = false;
Lucyjungz 2:c96b02fcb98e 40 break;
Lucyjungz 6:a05ec997c496 41 } else if (begin_text && *str != ' ') {
Lucyjungz 2:c96b02fcb98e 42 *ret = *str;
Lucyjungz 2:c96b02fcb98e 43 ret++;
Lucyjungz 2:c96b02fcb98e 44 }
Lucyjungz 6:a05ec997c496 45
Lucyjungz 2:c96b02fcb98e 46 str++;
Lucyjungz 2:c96b02fcb98e 47 }
Lucyjungz 2:c96b02fcb98e 48 removeSpaces(ret_addr, XMLTEXT_SIZE);
Lucyjungz 2:c96b02fcb98e 49 }
Lucyjungz 2:c96b02fcb98e 50 void readSetupFile()
Lucyjungz 2:c96b02fcb98e 51 {
Lucyjungz 2:c96b02fcb98e 52 // now open file for reading
Lucyjungz 2:c96b02fcb98e 53 FILE *fp = fopen(SETUP_FILE_NAME, "r");
Lucyjungz 2:c96b02fcb98e 54
Lucyjungz 2:c96b02fcb98e 55 if (fp == NULL) { // if it can't open the file then print error message
Lucyjungz 2:c96b02fcb98e 56 printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME);
Lucyjungz 2:c96b02fcb98e 57 } else { // opened file so can write
Lucyjungz 2:c96b02fcb98e 58 // fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&)
Lucyjungz 2:c96b02fcb98e 59 // serial.printf("Read %d from file.\n",stored_top_score);
Lucyjungz 2:c96b02fcb98e 60
Lucyjungz 2:c96b02fcb98e 61 ReadingFileState state = STATE_FINDING;
Lucyjungz 2:c96b02fcb98e 62 char buf[1024];
Lucyjungz 6:a05ec997c496 63 while (fgets(buf, sizeof(buf), fp) != NULL) {
Lucyjungz 6:a05ec997c496 64 if (strstr (buf,DATA_TAG)) {
Lucyjungz 2:c96b02fcb98e 65 state = STATE_FOUND_DATA;
Lucyjungz 6:a05ec997c496 66 } else if (strstr (buf,GPS_TAG)) {
Lucyjungz 2:c96b02fcb98e 67 state = STATE_FOUND_GPS;
Lucyjungz 6:a05ec997c496 68 } else if (strstr (buf,UPDATE_INTERVAL_TAG)) {
Lucyjungz 6:a05ec997c496 69 if (state == STATE_FOUND_GPS) {
Lucyjungz 2:c96b02fcb98e 70 getXmlText(buf, m_GpsInterval);
Lucyjungz 2:c96b02fcb98e 71 printf("\r\n-found GPS interval %s ", m_GpsInterval);
Lucyjungz 2:c96b02fcb98e 72 state = STATE_FINDING;
Lucyjungz 6:a05ec997c496 73 } else if(state == STATE_FOUND_DATA) {
Lucyjungz 2:c96b02fcb98e 74 getXmlText(buf, m_DataInterval);
Lucyjungz 2:c96b02fcb98e 75 printf("\r\n-found Data interval %s ", m_DataInterval);
Lucyjungz 2:c96b02fcb98e 76 state = STATE_FINDING;
Lucyjungz 2:c96b02fcb98e 77 }
Lucyjungz 2:c96b02fcb98e 78 }
Lucyjungz 6:a05ec997c496 79 }
Lucyjungz 2:c96b02fcb98e 80 fclose(fp); // ensure you close the file after reading
Lucyjungz 6:a05ec997c496 81 }
Lucyjungz 2:c96b02fcb98e 82 }
Lucyjungz 2:c96b02fcb98e 83 void logGPSData(char date[], char time[])
Lucyjungz 2:c96b02fcb98e 84 {
Lucyjungz 2:c96b02fcb98e 85 FILE *fp = fopen(GPS_LOG_FILE_NAME, "a");
Lucyjungz 2:c96b02fcb98e 86
Lucyjungz 2:c96b02fcb98e 87 if (fp == NULL) { // if it can't open the file then print error message
Lucyjungz 2:c96b02fcb98e 88 printf("Error! Unable to open file!\n");
Lucyjungz 2:c96b02fcb98e 89 } else { // opened file so can write
Lucyjungz 4:aa7ac2ac6913 90 printf("\r\n Writing to Gps Log File....");
Lucyjungz 2:c96b02fcb98e 91
Lucyjungz 2:c96b02fcb98e 92 fprintf(fp, "%s,%s\n",date,time); // print formatted string to file (CSV)
Lucyjungz 2:c96b02fcb98e 93
Lucyjungz 2:c96b02fcb98e 94 printf("Done");
Lucyjungz 2:c96b02fcb98e 95 fclose(fp); // ensure you close the file after writing
Lucyjungz 6:a05ec997c496 96 }
Lucyjungz 2:c96b02fcb98e 97 }
Lucyjungz 2:c96b02fcb98e 98 void logSystemData(float gps_interval)
Lucyjungz 2:c96b02fcb98e 99 {
Lucyjungz 2:c96b02fcb98e 100 FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");
Lucyjungz 2:c96b02fcb98e 101
Lucyjungz 6:a05ec997c496 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 2:c96b02fcb98e 106 fprintf(fp, "Start Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches
Lucyjungz 2:c96b02fcb98e 107 fclose(fp); // ensure you close the file after writing
Lucyjungz 6:a05ec997c496 108 }
Lucyjungz 2:c96b02fcb98e 109 }
Lucyjungz 2:c96b02fcb98e 110 void delete_file(char filename[])
Lucyjungz 2:c96b02fcb98e 111 {
Lucyjungz 2:c96b02fcb98e 112 printf("Deleting file '%s'...",filename);
Lucyjungz 2:c96b02fcb98e 113 FILE *fp = fopen(filename, "r"); // try and open file
Lucyjungz 2:c96b02fcb98e 114 if (fp != NULL) { // if it does open...
Lucyjungz 2:c96b02fcb98e 115 fclose(fp); // close it
Lucyjungz 2:c96b02fcb98e 116 remove(filename); // and then delete
Lucyjungz 2:c96b02fcb98e 117 printf("Done!\n");
Lucyjungz 2:c96b02fcb98e 118 }
Lucyjungz 2:c96b02fcb98e 119 // if we can't open it, it doesn't exist and so we can't delete it
Lucyjungz 2:c96b02fcb98e 120 }
Lucyjungz 2:c96b02fcb98e 121 int GPSInterval()
Lucyjungz 2:c96b02fcb98e 122 {
Lucyjungz 2:c96b02fcb98e 123 //Return whether or not CRC is enabled
Lucyjungz 2:c96b02fcb98e 124 return atoi( m_GpsInterval );
Lucyjungz 2:c96b02fcb98e 125 }
Lucyjungz 2:c96b02fcb98e 126 int DataInterval()
Lucyjungz 2:c96b02fcb98e 127 {
Lucyjungz 2:c96b02fcb98e 128 //Return whether or not CRC is enabled
Lucyjungz 2:c96b02fcb98e 129 return atoi( m_DataInterval );
Lucyjungz 4:aa7ac2ac6913 130 }
Lucyjungz 4:aa7ac2ac6913 131
Lucyjungz 4:aa7ac2ac6913 132 Variable_Data_TypeDef * readVarFile()
Lucyjungz 4:aa7ac2ac6913 133 {
Lucyjungz 4:aa7ac2ac6913 134 // now open file for reading
Lucyjungz 4:aa7ac2ac6913 135 FILE *fp = fopen(VARIABLE_FILE_NAME, "r");
Lucyjungz 4:aa7ac2ac6913 136
Lucyjungz 4:aa7ac2ac6913 137 if (fp == NULL) { // if it can't open the file then print error message
Lucyjungz 4:aa7ac2ac6913 138 printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME);
Lucyjungz 4:aa7ac2ac6913 139 return NULL;
Lucyjungz 4:aa7ac2ac6913 140 } else { // opened file so can write
Lucyjungz 4:aa7ac2ac6913 141
Lucyjungz 4:aa7ac2ac6913 142 char buf[1024];
Lucyjungz 4:aa7ac2ac6913 143 int index = 0;
Lucyjungz 4:aa7ac2ac6913 144 memset(m_varList, ' ', sizeof(m_varList));
Lucyjungz 6:a05ec997c496 145 while (fgets(buf, sizeof(buf), fp) != NULL) {
Lucyjungz 6:a05ec997c496 146 if (strstr (buf,VAR_NAME_TAG)) {
Lucyjungz 4:aa7ac2ac6913 147 getXmlText(buf , m_varList[index].varName);
Lucyjungz 6:a05ec997c496 148
Lucyjungz 6:a05ec997c496 149 } else if (strstr (buf,VAR_ADDR_TAG)) {
Lucyjungz 4:aa7ac2ac6913 150 getXmlText(buf , m_varList[index].varAddress);
Lucyjungz 4:aa7ac2ac6913 151 index++;
Lucyjungz 4:aa7ac2ac6913 152 }
Lucyjungz 4:aa7ac2ac6913 153
Lucyjungz 6:a05ec997c496 154 }
Lucyjungz 4:aa7ac2ac6913 155 fclose(fp); // ensure you close the file after reading
Lucyjungz 4:aa7ac2ac6913 156 m_amountVarList = index;
Lucyjungz 4:aa7ac2ac6913 157 return m_varList;
Lucyjungz 6:a05ec997c496 158 }
Lucyjungz 4:aa7ac2ac6913 159 }
Lucyjungz 6:a05ec997c496 160 int getAmountVarList()
Lucyjungz 6:a05ec997c496 161 {
Lucyjungz 4:aa7ac2ac6913 162 return m_amountVarList;
Lucyjungz 6:a05ec997c496 163 }
Lucyjungz 6:a05ec997c496 164 Variable_Data_TypeDef * getVarList()
Lucyjungz 6:a05ec997c496 165 {
Lucyjungz 5:07aaa6e3784c 166 return m_varList;
Lucyjungz 6:a05ec997c496 167 }
Lucyjungz 5:07aaa6e3784c 168
Lucyjungz 6:a05ec997c496 169