![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Initial Publish Leaning GPS/SDCARD
Dependencies: FileManager GPSGms6 SDFileSystem mbed
Fork of 2545_SD_Card by
main.cpp@1:f911149acd35, 2016-05-06 (annotated)
- Committer:
- Lucyjungz
- Date:
- Fri May 06 19:30:02 2016 +0000
- Revision:
- 1:f911149acd35
- Parent:
- 0:5448330e1a33
- Child:
- 2:c96b02fcb98e
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:5448330e1a33 | 1 | /* 2545_SD_Card Example |
eencae | 0:5448330e1a33 | 2 | |
eencae | 0:5448330e1a33 | 3 | Example of writing data to SD card. |
eencae | 0:5448330e1a33 | 4 | |
eencae | 0:5448330e1a33 | 5 | Based on FTF2014_lab4 Example |
eencae | 0:5448330e1a33 | 6 | |
eencae | 0:5448330e1a33 | 7 | https://developer.mbed.org/teams/Freescale/wiki/FTF2014_workshop |
eencae | 0:5448330e1a33 | 8 | |
eencae | 0:5448330e1a33 | 9 | Craig A. Evans, University of Leeds, Mar 2016 |
eencae | 0:5448330e1a33 | 10 | |
eencae | 0:5448330e1a33 | 11 | */ |
eencae | 0:5448330e1a33 | 12 | |
eencae | 0:5448330e1a33 | 13 | #include "mbed.h" |
eencae | 0:5448330e1a33 | 14 | #include "SDFileSystem.h" |
Lucyjungz | 1:f911149acd35 | 15 | #include "GPSGms6.h" |
Lucyjungz | 1:f911149acd35 | 16 | |
Lucyjungz | 1:f911149acd35 | 17 | #define GPS_TAG "<Gps>" |
Lucyjungz | 1:f911149acd35 | 18 | #define DATA_TAG "<Data>" |
Lucyjungz | 1:f911149acd35 | 19 | #define UPDATE_INTERVAL_TAG "<Update_Interval>" |
Lucyjungz | 1:f911149acd35 | 20 | |
Lucyjungz | 1:f911149acd35 | 21 | |
Lucyjungz | 1:f911149acd35 | 22 | #define XMLTEXT_SIZE 20 |
eencae | 0:5448330e1a33 | 23 | |
eencae | 0:5448330e1a33 | 24 | // Connections to SD card holder on K64F (SPI interface) |
Lucyjungz | 1:f911149acd35 | 25 | SDFileSystem sd(PA_7, PA_6, PA_5, PA_0, "sd"); // MOSI, MISO, SCK, CS |
eencae | 0:5448330e1a33 | 26 | Serial serial(USBTX, USBRX); // for PC debug |
Lucyjungz | 1:f911149acd35 | 27 | GPSGms6 gps; |
Lucyjungz | 1:f911149acd35 | 28 | Timeout t1; |
Lucyjungz | 1:f911149acd35 | 29 | DigitalOut myled(LED1); |
Lucyjungz | 1:f911149acd35 | 30 | |
Lucyjungz | 1:f911149acd35 | 31 | char m_GpsInterval[XMLTEXT_SIZE]; |
Lucyjungz | 1:f911149acd35 | 32 | char m_DataInterval[XMLTEXT_SIZE]; |
Lucyjungz | 1:f911149acd35 | 33 | typedef enum { |
Lucyjungz | 1:f911149acd35 | 34 | STATE_FINDING, /** Finding */ |
Lucyjungz | 1:f911149acd35 | 35 | STATE_FOUND_DATA, /** Found Data tag */ |
Lucyjungz | 1:f911149acd35 | 36 | STATE_FOUND_DATA_INTERVAL, /**< Found update internal of tag*/ |
Lucyjungz | 1:f911149acd35 | 37 | STATE_FOUND_GPS, /** Found GPS tag */ |
Lucyjungz | 1:f911149acd35 | 38 | STATE_FOUND_GPS_INTERVAL, /** Found update internal of GPS*/ |
Lucyjungz | 1:f911149acd35 | 39 | }ReadingFileState; |
Lucyjungz | 1:f911149acd35 | 40 | void delete_file(char filename[]); |
Lucyjungz | 1:f911149acd35 | 41 | void t1out(void) |
Lucyjungz | 1:f911149acd35 | 42 | { |
Lucyjungz | 1:f911149acd35 | 43 | myled = !myled; |
Lucyjungz | 1:f911149acd35 | 44 | printf("\r\nGps header = %s", gps.latestGPRMC().header); |
Lucyjungz | 1:f911149acd35 | 45 | printf("\r\nGps status = %s", gps.latestGPRMC().status); |
Lucyjungz | 1:f911149acd35 | 46 | printf("\r\nGps time = %s", gps.latestGPRMC().time); |
Lucyjungz | 1:f911149acd35 | 47 | printf("\r\nGps date = %s", gps.latestGPRMC().date); |
Lucyjungz | 1:f911149acd35 | 48 | printf("\r\nGps lat = %s", gps.latestGPRMC().latitude); |
Lucyjungz | 1:f911149acd35 | 49 | printf("\r\nGps long = %s", gps.latestGPRMC().longitude); |
Lucyjungz | 1:f911149acd35 | 50 | printf("\r\nGps indicator = %s", gps.latestGPRMC().indicator); |
Lucyjungz | 1:f911149acd35 | 51 | t1.attach(&t1out,5); |
Lucyjungz | 1:f911149acd35 | 52 | } |
Lucyjungz | 1:f911149acd35 | 53 | void removeSpaces(char* s , int size) |
Lucyjungz | 1:f911149acd35 | 54 | { |
Lucyjungz | 1:f911149acd35 | 55 | char* cpy = s; // an alias to iterate through s without moving s |
Lucyjungz | 1:f911149acd35 | 56 | char* temp = s; |
eencae | 0:5448330e1a33 | 57 | |
Lucyjungz | 1:f911149acd35 | 58 | for (int i = 0 ; i < size ; i++) |
Lucyjungz | 1:f911149acd35 | 59 | { |
Lucyjungz | 1:f911149acd35 | 60 | if (*cpy != ' ') |
Lucyjungz | 1:f911149acd35 | 61 | *temp++ = *cpy; |
Lucyjungz | 1:f911149acd35 | 62 | cpy++; |
Lucyjungz | 1:f911149acd35 | 63 | } |
Lucyjungz | 1:f911149acd35 | 64 | *temp = 0; |
Lucyjungz | 1:f911149acd35 | 65 | return; |
Lucyjungz | 1:f911149acd35 | 66 | } |
Lucyjungz | 1:f911149acd35 | 67 | void getXmlText(char *str, char *ret) |
Lucyjungz | 1:f911149acd35 | 68 | { |
Lucyjungz | 1:f911149acd35 | 69 | int size = strlen(str); |
Lucyjungz | 1:f911149acd35 | 70 | int i; |
Lucyjungz | 1:f911149acd35 | 71 | bool begin_text = false; |
Lucyjungz | 1:f911149acd35 | 72 | char * ret_addr = ret; |
Lucyjungz | 1:f911149acd35 | 73 | memset (ret,' ',XMLTEXT_SIZE); |
Lucyjungz | 1:f911149acd35 | 74 | |
Lucyjungz | 1:f911149acd35 | 75 | for(i = 0; i < size ; i++) |
Lucyjungz | 1:f911149acd35 | 76 | { |
Lucyjungz | 1:f911149acd35 | 77 | |
Lucyjungz | 1:f911149acd35 | 78 | if (*str == '>') |
Lucyjungz | 1:f911149acd35 | 79 | { |
Lucyjungz | 1:f911149acd35 | 80 | begin_text = true; |
Lucyjungz | 1:f911149acd35 | 81 | } |
Lucyjungz | 1:f911149acd35 | 82 | else if (begin_text && *str == '<') |
Lucyjungz | 1:f911149acd35 | 83 | { |
Lucyjungz | 1:f911149acd35 | 84 | begin_text = false; |
Lucyjungz | 1:f911149acd35 | 85 | break; |
Lucyjungz | 1:f911149acd35 | 86 | } |
Lucyjungz | 1:f911149acd35 | 87 | else if (begin_text && *str != ' ') |
Lucyjungz | 1:f911149acd35 | 88 | { |
Lucyjungz | 1:f911149acd35 | 89 | *ret = *str; |
Lucyjungz | 1:f911149acd35 | 90 | ret++; |
Lucyjungz | 1:f911149acd35 | 91 | } |
Lucyjungz | 1:f911149acd35 | 92 | |
Lucyjungz | 1:f911149acd35 | 93 | str++; |
Lucyjungz | 1:f911149acd35 | 94 | } |
Lucyjungz | 1:f911149acd35 | 95 | removeSpaces(ret_addr, XMLTEXT_SIZE); |
Lucyjungz | 1:f911149acd35 | 96 | } |
eencae | 0:5448330e1a33 | 97 | int main() |
eencae | 0:5448330e1a33 | 98 | { |
Lucyjungz | 1:f911149acd35 | 99 | serial.baud(9600); // full-speed! |
eencae | 0:5448330e1a33 | 100 | serial.printf("#### SD Card Example #####\n"); |
eencae | 0:5448330e1a33 | 101 | FILE *fp; // this is our file pointer |
eencae | 0:5448330e1a33 | 102 | wait(1); |
eencae | 0:5448330e1a33 | 103 | |
eencae | 0:5448330e1a33 | 104 | // Various examples below - can comment out ones you don't need |
eencae | 0:5448330e1a33 | 105 | |
eencae | 0:5448330e1a33 | 106 | /////////////////////// Deleting file example //////////////////////// |
eencae | 0:5448330e1a33 | 107 | |
eencae | 0:5448330e1a33 | 108 | // comment this line out if you don't want to delete the file! |
eencae | 0:5448330e1a33 | 109 | delete_file("/sd/test.txt"); |
eencae | 0:5448330e1a33 | 110 | |
eencae | 0:5448330e1a33 | 111 | ////////////////////// Simple writing example ////////////////////////// |
eencae | 0:5448330e1a33 | 112 | |
eencae | 0:5448330e1a33 | 113 | // open file for writing ('w') - creates file if it doesn't exist and overwrites |
eencae | 0:5448330e1a33 | 114 | // if it does. If you wish to add a score onto a list, then you can |
eencae | 0:5448330e1a33 | 115 | // append instead 'a'. This will open the file if it exists and start |
eencae | 0:5448330e1a33 | 116 | // writing at the end. It will create the file if it doesn't exist. |
eencae | 0:5448330e1a33 | 117 | fp = fopen("/sd/topscore.txt", "w"); |
eencae | 0:5448330e1a33 | 118 | int top_score = 56; // random example |
eencae | 0:5448330e1a33 | 119 | |
eencae | 0:5448330e1a33 | 120 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 0:5448330e1a33 | 121 | serial.printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 122 | } else { // opened file so can write |
eencae | 0:5448330e1a33 | 123 | serial.printf("Writing to file...."); |
eencae | 0:5448330e1a33 | 124 | fprintf(fp, "%d",top_score); // ensure data type matches |
eencae | 0:5448330e1a33 | 125 | serial.printf("Done.\n"); |
eencae | 0:5448330e1a33 | 126 | fclose(fp); // ensure you close the file after writing |
eencae | 0:5448330e1a33 | 127 | } |
eencae | 0:5448330e1a33 | 128 | |
eencae | 0:5448330e1a33 | 129 | ////////////////////// Simple reading example ////////////////////////// |
eencae | 0:5448330e1a33 | 130 | |
eencae | 0:5448330e1a33 | 131 | // now open file for reading |
Lucyjungz | 1:f911149acd35 | 132 | fp = fopen("/sd/RMS_Tester.xml", "r"); |
eencae | 0:5448330e1a33 | 133 | |
eencae | 0:5448330e1a33 | 134 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 0:5448330e1a33 | 135 | serial.printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 136 | } else { // opened file so can write |
Lucyjungz | 1:f911149acd35 | 137 | // fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
Lucyjungz | 1:f911149acd35 | 138 | // serial.printf("Read %d from file.\n",stored_top_score); |
Lucyjungz | 1:f911149acd35 | 139 | |
Lucyjungz | 1:f911149acd35 | 140 | ReadingFileState state = STATE_FINDING; |
Lucyjungz | 1:f911149acd35 | 141 | char buf[1024]; |
Lucyjungz | 1:f911149acd35 | 142 | while (fgets(buf, sizeof(buf), fp) != NULL) |
Lucyjungz | 1:f911149acd35 | 143 | { |
Lucyjungz | 1:f911149acd35 | 144 | if (strstr (buf,DATA_TAG)) |
Lucyjungz | 1:f911149acd35 | 145 | { |
Lucyjungz | 1:f911149acd35 | 146 | state = STATE_FOUND_DATA; |
Lucyjungz | 1:f911149acd35 | 147 | } |
Lucyjungz | 1:f911149acd35 | 148 | else if (strstr (buf,GPS_TAG)) |
Lucyjungz | 1:f911149acd35 | 149 | { |
Lucyjungz | 1:f911149acd35 | 150 | state = STATE_FOUND_GPS; |
Lucyjungz | 1:f911149acd35 | 151 | } |
Lucyjungz | 1:f911149acd35 | 152 | else if (strstr (buf,UPDATE_INTERVAL_TAG)) |
Lucyjungz | 1:f911149acd35 | 153 | { |
Lucyjungz | 1:f911149acd35 | 154 | if (state == STATE_FOUND_GPS) |
Lucyjungz | 1:f911149acd35 | 155 | { |
Lucyjungz | 1:f911149acd35 | 156 | getXmlText(buf, m_GpsInterval); |
Lucyjungz | 1:f911149acd35 | 157 | printf("\r\n-found GPS interval %s ", m_GpsInterval); |
Lucyjungz | 1:f911149acd35 | 158 | state = STATE_FINDING; |
Lucyjungz | 1:f911149acd35 | 159 | } |
Lucyjungz | 1:f911149acd35 | 160 | else if(state == STATE_FOUND_DATA) |
Lucyjungz | 1:f911149acd35 | 161 | { |
Lucyjungz | 1:f911149acd35 | 162 | getXmlText(buf, m_DataInterval); |
Lucyjungz | 1:f911149acd35 | 163 | printf("\r\n-found Data interval %s ", m_DataInterval); |
Lucyjungz | 1:f911149acd35 | 164 | state = STATE_FINDING; |
Lucyjungz | 1:f911149acd35 | 165 | } |
Lucyjungz | 1:f911149acd35 | 166 | } |
Lucyjungz | 1:f911149acd35 | 167 | } |
eencae | 0:5448330e1a33 | 168 | fclose(fp); // ensure you close the file after reading |
eencae | 0:5448330e1a33 | 169 | } |
eencae | 0:5448330e1a33 | 170 | |
eencae | 0:5448330e1a33 | 171 | ///////////////////// Writing list to file example ////////////////////// |
eencae | 0:5448330e1a33 | 172 | |
eencae | 0:5448330e1a33 | 173 | // for this example, I'll create some numbers to write to file in a big list |
eencae | 0:5448330e1a33 | 174 | // a data logger for example will usually append to a file - at a reading |
eencae | 0:5448330e1a33 | 175 | // at the end rather than creating a new file |
eencae | 0:5448330e1a33 | 176 | fp = fopen("/sd/test.txt", "a"); |
eencae | 0:5448330e1a33 | 177 | |
eencae | 0:5448330e1a33 | 178 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 0:5448330e1a33 | 179 | serial.printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 180 | } else { // opened file so can write |
eencae | 0:5448330e1a33 | 181 | serial.printf("Writing to file...."); |
eencae | 0:5448330e1a33 | 182 | for(int i = 1; i <= 50; i++) { |
eencae | 0:5448330e1a33 | 183 | float dummy = 1000.0F/i; // dummy variable |
eencae | 0:5448330e1a33 | 184 | fprintf(fp, "%d,%f\n",i,dummy); // print formatted string to file (CSV) |
eencae | 0:5448330e1a33 | 185 | } |
eencae | 0:5448330e1a33 | 186 | serial.printf("Done.\n"); |
eencae | 0:5448330e1a33 | 187 | fclose(fp); // ensure you close the file after writing |
eencae | 0:5448330e1a33 | 188 | } |
eencae | 0:5448330e1a33 | 189 | |
eencae | 0:5448330e1a33 | 190 | |
eencae | 0:5448330e1a33 | 191 | |
eencae | 0:5448330e1a33 | 192 | /////////////////////////////////////////////////// |
eencae | 0:5448330e1a33 | 193 | serial.printf("End of SD card example\n"); |
Lucyjungz | 1:f911149acd35 | 194 | |
Lucyjungz | 1:f911149acd35 | 195 | t1.attach(&t1out,5); |
Lucyjungz | 1:f911149acd35 | 196 | while(1); |
eencae | 0:5448330e1a33 | 197 | } |
eencae | 0:5448330e1a33 | 198 | |
eencae | 0:5448330e1a33 | 199 | void delete_file(char filename[]) |
eencae | 0:5448330e1a33 | 200 | { |
eencae | 0:5448330e1a33 | 201 | serial.printf("Deleting file '%s'...",filename); |
eencae | 0:5448330e1a33 | 202 | FILE *fp = fopen(filename, "r"); // try and open file |
eencae | 0:5448330e1a33 | 203 | if (fp != NULL) { // if it does open... |
eencae | 0:5448330e1a33 | 204 | fclose(fp); // close it |
eencae | 0:5448330e1a33 | 205 | remove(filename); // and then delete |
eencae | 0:5448330e1a33 | 206 | serial.printf("Done!\n"); |
eencae | 0:5448330e1a33 | 207 | } |
eencae | 0:5448330e1a33 | 208 | // if we can't open it, it doesn't exist and so we can't delete it |
eencae | 0:5448330e1a33 | 209 | } |