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@2:c96b02fcb98e, 2016-05-06 (annotated)
- Committer:
- Lucyjungz
- Date:
- Fri May 06 20:16:30 2016 +0000
- Revision:
- 2:c96b02fcb98e
- Child:
- 4:aa7ac2ac6913
Integrated with File Manager
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 | 2:c96b02fcb98e | 6 | |
| Lucyjungz | 2:c96b02fcb98e | 7 | static void removeSpaces(char* s , int size); |
| Lucyjungz | 2:c96b02fcb98e | 8 | static void getXmlText(char *str, char *ret); |
| Lucyjungz | 2:c96b02fcb98e | 9 | |
| Lucyjungz | 2:c96b02fcb98e | 10 | |
| Lucyjungz | 2:c96b02fcb98e | 11 | static void removeSpaces(char* s , int size) |
| Lucyjungz | 2:c96b02fcb98e | 12 | { |
| Lucyjungz | 2:c96b02fcb98e | 13 | char* cpy = s; // an alias to iterate through s without moving s |
| Lucyjungz | 2:c96b02fcb98e | 14 | char* temp = s; |
| Lucyjungz | 2:c96b02fcb98e | 15 | |
| Lucyjungz | 2:c96b02fcb98e | 16 | for (int i = 0 ; i < size ; i++) |
| Lucyjungz | 2:c96b02fcb98e | 17 | { |
| Lucyjungz | 2:c96b02fcb98e | 18 | if (*cpy != ' ') |
| Lucyjungz | 2:c96b02fcb98e | 19 | *temp++ = *cpy; |
| Lucyjungz | 2:c96b02fcb98e | 20 | cpy++; |
| Lucyjungz | 2:c96b02fcb98e | 21 | } |
| Lucyjungz | 2:c96b02fcb98e | 22 | *temp = 0; |
| Lucyjungz | 2:c96b02fcb98e | 23 | return; |
| Lucyjungz | 2:c96b02fcb98e | 24 | } |
| Lucyjungz | 2:c96b02fcb98e | 25 | static void getXmlText(char *str, char *ret) |
| Lucyjungz | 2:c96b02fcb98e | 26 | { |
| Lucyjungz | 2:c96b02fcb98e | 27 | int size = strlen(str); |
| Lucyjungz | 2:c96b02fcb98e | 28 | int i; |
| Lucyjungz | 2:c96b02fcb98e | 29 | bool begin_text = false; |
| Lucyjungz | 2:c96b02fcb98e | 30 | char * ret_addr = ret; |
| Lucyjungz | 2:c96b02fcb98e | 31 | memset (ret,' ',XMLTEXT_SIZE); |
| Lucyjungz | 2:c96b02fcb98e | 32 | |
| Lucyjungz | 2:c96b02fcb98e | 33 | for(i = 0; i < size ; i++) |
| Lucyjungz | 2:c96b02fcb98e | 34 | { |
| Lucyjungz | 2:c96b02fcb98e | 35 | |
| Lucyjungz | 2:c96b02fcb98e | 36 | if (*str == '>') |
| Lucyjungz | 2:c96b02fcb98e | 37 | { |
| Lucyjungz | 2:c96b02fcb98e | 38 | begin_text = true; |
| Lucyjungz | 2:c96b02fcb98e | 39 | } |
| Lucyjungz | 2:c96b02fcb98e | 40 | else if (begin_text && *str == '<') |
| Lucyjungz | 2:c96b02fcb98e | 41 | { |
| Lucyjungz | 2:c96b02fcb98e | 42 | begin_text = false; |
| Lucyjungz | 2:c96b02fcb98e | 43 | break; |
| Lucyjungz | 2:c96b02fcb98e | 44 | } |
| Lucyjungz | 2:c96b02fcb98e | 45 | else if (begin_text && *str != ' ') |
| Lucyjungz | 2:c96b02fcb98e | 46 | { |
| Lucyjungz | 2:c96b02fcb98e | 47 | *ret = *str; |
| Lucyjungz | 2:c96b02fcb98e | 48 | ret++; |
| Lucyjungz | 2:c96b02fcb98e | 49 | } |
| Lucyjungz | 2:c96b02fcb98e | 50 | |
| Lucyjungz | 2:c96b02fcb98e | 51 | str++; |
| Lucyjungz | 2:c96b02fcb98e | 52 | } |
| Lucyjungz | 2:c96b02fcb98e | 53 | removeSpaces(ret_addr, XMLTEXT_SIZE); |
| Lucyjungz | 2:c96b02fcb98e | 54 | } |
| Lucyjungz | 2:c96b02fcb98e | 55 | void readSetupFile() |
| Lucyjungz | 2:c96b02fcb98e | 56 | { |
| Lucyjungz | 2:c96b02fcb98e | 57 | // now open file for reading |
| Lucyjungz | 2:c96b02fcb98e | 58 | FILE *fp = fopen(SETUP_FILE_NAME, "r"); |
| Lucyjungz | 2:c96b02fcb98e | 59 | |
| Lucyjungz | 2:c96b02fcb98e | 60 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 2:c96b02fcb98e | 61 | printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); |
| Lucyjungz | 2:c96b02fcb98e | 62 | } else { // opened file so can write |
| Lucyjungz | 2:c96b02fcb98e | 63 | // fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
| Lucyjungz | 2:c96b02fcb98e | 64 | // serial.printf("Read %d from file.\n",stored_top_score); |
| Lucyjungz | 2:c96b02fcb98e | 65 | |
| Lucyjungz | 2:c96b02fcb98e | 66 | ReadingFileState state = STATE_FINDING; |
| Lucyjungz | 2:c96b02fcb98e | 67 | char buf[1024]; |
| Lucyjungz | 2:c96b02fcb98e | 68 | while (fgets(buf, sizeof(buf), fp) != NULL) |
| Lucyjungz | 2:c96b02fcb98e | 69 | { |
| Lucyjungz | 2:c96b02fcb98e | 70 | if (strstr (buf,DATA_TAG)) |
| Lucyjungz | 2:c96b02fcb98e | 71 | { |
| Lucyjungz | 2:c96b02fcb98e | 72 | state = STATE_FOUND_DATA; |
| Lucyjungz | 2:c96b02fcb98e | 73 | } |
| Lucyjungz | 2:c96b02fcb98e | 74 | else if (strstr (buf,GPS_TAG)) |
| Lucyjungz | 2:c96b02fcb98e | 75 | { |
| Lucyjungz | 2:c96b02fcb98e | 76 | state = STATE_FOUND_GPS; |
| Lucyjungz | 2:c96b02fcb98e | 77 | } |
| Lucyjungz | 2:c96b02fcb98e | 78 | else if (strstr (buf,UPDATE_INTERVAL_TAG)) |
| Lucyjungz | 2:c96b02fcb98e | 79 | { |
| Lucyjungz | 2:c96b02fcb98e | 80 | if (state == STATE_FOUND_GPS) |
| Lucyjungz | 2:c96b02fcb98e | 81 | { |
| Lucyjungz | 2:c96b02fcb98e | 82 | getXmlText(buf, m_GpsInterval); |
| Lucyjungz | 2:c96b02fcb98e | 83 | printf("\r\n-found GPS interval %s ", m_GpsInterval); |
| Lucyjungz | 2:c96b02fcb98e | 84 | state = STATE_FINDING; |
| Lucyjungz | 2:c96b02fcb98e | 85 | } |
| Lucyjungz | 2:c96b02fcb98e | 86 | else if(state == STATE_FOUND_DATA) |
| Lucyjungz | 2:c96b02fcb98e | 87 | { |
| Lucyjungz | 2:c96b02fcb98e | 88 | getXmlText(buf, m_DataInterval); |
| Lucyjungz | 2:c96b02fcb98e | 89 | printf("\r\n-found Data interval %s ", m_DataInterval); |
| Lucyjungz | 2:c96b02fcb98e | 90 | state = STATE_FINDING; |
| Lucyjungz | 2:c96b02fcb98e | 91 | } |
| Lucyjungz | 2:c96b02fcb98e | 92 | } |
| Lucyjungz | 2:c96b02fcb98e | 93 | } |
| Lucyjungz | 2:c96b02fcb98e | 94 | fclose(fp); // ensure you close the file after reading |
| Lucyjungz | 2:c96b02fcb98e | 95 | } |
| Lucyjungz | 2:c96b02fcb98e | 96 | } |
| Lucyjungz | 2:c96b02fcb98e | 97 | void logGPSData(char date[], char time[]) |
| Lucyjungz | 2:c96b02fcb98e | 98 | { |
| Lucyjungz | 2:c96b02fcb98e | 99 | FILE *fp = fopen(GPS_LOG_FILE_NAME, "a"); |
| Lucyjungz | 2:c96b02fcb98e | 100 | |
| Lucyjungz | 2:c96b02fcb98e | 101 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 2:c96b02fcb98e | 102 | printf("Error! Unable to open file!\n"); |
| Lucyjungz | 2:c96b02fcb98e | 103 | } else { // opened file so can write |
| Lucyjungz | 2:c96b02fcb98e | 104 | printf("\n Writing to Gps Log File...."); |
| Lucyjungz | 2:c96b02fcb98e | 105 | |
| Lucyjungz | 2:c96b02fcb98e | 106 | fprintf(fp, "%s,%s\n",date,time); // print formatted string to file (CSV) |
| Lucyjungz | 2:c96b02fcb98e | 107 | |
| Lucyjungz | 2:c96b02fcb98e | 108 | printf("Done"); |
| Lucyjungz | 2:c96b02fcb98e | 109 | fclose(fp); // ensure you close the file after writing |
| Lucyjungz | 2:c96b02fcb98e | 110 | } |
| Lucyjungz | 2:c96b02fcb98e | 111 | } |
| Lucyjungz | 2:c96b02fcb98e | 112 | void logSystemData(float gps_interval) |
| Lucyjungz | 2:c96b02fcb98e | 113 | { |
| Lucyjungz | 2:c96b02fcb98e | 114 | FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); |
| Lucyjungz | 2:c96b02fcb98e | 115 | |
| Lucyjungz | 2:c96b02fcb98e | 116 | |
| Lucyjungz | 2:c96b02fcb98e | 117 | if (fp == NULL) { // if it can't open the file then print error message |
| Lucyjungz | 2:c96b02fcb98e | 118 | printf("Error! Unable to open file!\n"); |
| Lucyjungz | 2:c96b02fcb98e | 119 | } else { // opened file so can write |
| Lucyjungz | 2:c96b02fcb98e | 120 | fprintf(fp, "Start Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches |
| Lucyjungz | 2:c96b02fcb98e | 121 | fclose(fp); // ensure you close the file after writing |
| Lucyjungz | 2:c96b02fcb98e | 122 | } |
| Lucyjungz | 2:c96b02fcb98e | 123 | } |
| Lucyjungz | 2:c96b02fcb98e | 124 | void delete_file(char filename[]) |
| Lucyjungz | 2:c96b02fcb98e | 125 | { |
| Lucyjungz | 2:c96b02fcb98e | 126 | printf("Deleting file '%s'...",filename); |
| Lucyjungz | 2:c96b02fcb98e | 127 | FILE *fp = fopen(filename, "r"); // try and open file |
| Lucyjungz | 2:c96b02fcb98e | 128 | if (fp != NULL) { // if it does open... |
| Lucyjungz | 2:c96b02fcb98e | 129 | fclose(fp); // close it |
| Lucyjungz | 2:c96b02fcb98e | 130 | remove(filename); // and then delete |
| Lucyjungz | 2:c96b02fcb98e | 131 | printf("Done!\n"); |
| Lucyjungz | 2:c96b02fcb98e | 132 | } |
| Lucyjungz | 2:c96b02fcb98e | 133 | // if we can't open it, it doesn't exist and so we can't delete it |
| Lucyjungz | 2:c96b02fcb98e | 134 | } |
| Lucyjungz | 2:c96b02fcb98e | 135 | int GPSInterval() |
| Lucyjungz | 2:c96b02fcb98e | 136 | { |
| Lucyjungz | 2:c96b02fcb98e | 137 | //Return whether or not CRC is enabled |
| Lucyjungz | 2:c96b02fcb98e | 138 | return atoi( m_GpsInterval ); |
| Lucyjungz | 2:c96b02fcb98e | 139 | } |
| Lucyjungz | 2:c96b02fcb98e | 140 | int DataInterval() |
| Lucyjungz | 2:c96b02fcb98e | 141 | { |
| Lucyjungz | 2:c96b02fcb98e | 142 | //Return whether or not CRC is enabled |
| Lucyjungz | 2:c96b02fcb98e | 143 | return atoi( m_DataInterval ); |
| Lucyjungz | 2:c96b02fcb98e | 144 | } |
