Initial Publish Leaning GPS/SDCARD
Dependencies: FileManager GPSGms6 SDFileSystem mbed
Fork of 2545_SD_Card by
Diff: main.cpp
- Revision:
- 1:f911149acd35
- Parent:
- 0:5448330e1a33
- Child:
- 2:c96b02fcb98e
--- a/main.cpp Fri Mar 11 16:07:41 2016 +0000 +++ b/main.cpp Fri May 06 19:30:02 2016 +0000 @@ -12,16 +12,91 @@ #include "mbed.h" #include "SDFileSystem.h" +#include "GPSGms6.h" + +#define GPS_TAG "<Gps>" +#define DATA_TAG "<Data>" +#define UPDATE_INTERVAL_TAG "<Update_Interval>" + + +#define XMLTEXT_SIZE 20 // Connections to SD card holder on K64F (SPI interface) -SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS +SDFileSystem sd(PA_7, PA_6, PA_5, PA_0, "sd"); // MOSI, MISO, SCK, CS Serial serial(USBTX, USBRX); // for PC debug +GPSGms6 gps; +Timeout t1; +DigitalOut myled(LED1); + +char m_GpsInterval[XMLTEXT_SIZE]; +char m_DataInterval[XMLTEXT_SIZE]; +typedef enum { + STATE_FINDING, /** Finding */ + STATE_FOUND_DATA, /** Found Data tag */ + STATE_FOUND_DATA_INTERVAL, /**< Found update internal of tag*/ + STATE_FOUND_GPS, /** Found GPS tag */ + STATE_FOUND_GPS_INTERVAL, /** Found update internal of GPS*/ +}ReadingFileState; +void delete_file(char filename[]); +void t1out(void) +{ + myled = !myled; + printf("\r\nGps header = %s", gps.latestGPRMC().header); + printf("\r\nGps status = %s", gps.latestGPRMC().status); + printf("\r\nGps time = %s", gps.latestGPRMC().time); + printf("\r\nGps date = %s", gps.latestGPRMC().date); + printf("\r\nGps lat = %s", gps.latestGPRMC().latitude); + printf("\r\nGps long = %s", gps.latestGPRMC().longitude); + printf("\r\nGps indicator = %s", gps.latestGPRMC().indicator); + t1.attach(&t1out,5); +} +void removeSpaces(char* s , int size) +{ + char* cpy = s; // an alias to iterate through s without moving s + char* temp = s; -void delete_file(char filename[]); - + for (int i = 0 ; i < size ; i++) + { + if (*cpy != ' ') + *temp++ = *cpy; + cpy++; + } + *temp = 0; + return; +} +void getXmlText(char *str, char *ret) +{ + int size = strlen(str); + int i; + bool begin_text = false; + char * ret_addr = ret; + memset (ret,' ',XMLTEXT_SIZE); + + for(i = 0; i < size ; i++) + { + + if (*str == '>') + { + begin_text = true; + } + else if (begin_text && *str == '<') + { + begin_text = false; + break; + } + else if (begin_text && *str != ' ') + { + *ret = *str; + ret++; + } + + str++; + } + removeSpaces(ret_addr, XMLTEXT_SIZE); +} int main() { - serial.baud(115200); // full-speed! + serial.baud(9600); // full-speed! serial.printf("#### SD Card Example #####\n"); FILE *fp; // this is our file pointer wait(1); @@ -54,14 +129,42 @@ ////////////////////// Simple reading example ////////////////////////// // now open file for reading - fp = fopen("/sd/topscore.txt", "r"); - int stored_top_score = -1; // -1 to demonstrate it has changed after reading + fp = fopen("/sd/RMS_Tester.xml", "r"); if (fp == NULL) { // if it can't open the file then print error message serial.printf("Error! Unable to open file!\n"); } else { // opened file so can write - fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) - serial.printf("Read %d from file.\n",stored_top_score); +// fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) +// serial.printf("Read %d from file.\n",stored_top_score); + + ReadingFileState state = STATE_FINDING; + char buf[1024]; + while (fgets(buf, sizeof(buf), fp) != NULL) + { + if (strstr (buf,DATA_TAG)) + { + state = STATE_FOUND_DATA; + } + else if (strstr (buf,GPS_TAG)) + { + state = STATE_FOUND_GPS; + } + else if (strstr (buf,UPDATE_INTERVAL_TAG)) + { + if (state == STATE_FOUND_GPS) + { + getXmlText(buf, m_GpsInterval); + printf("\r\n-found GPS interval %s ", m_GpsInterval); + state = STATE_FINDING; + } + else if(state == STATE_FOUND_DATA) + { + getXmlText(buf, m_DataInterval); + printf("\r\n-found Data interval %s ", m_DataInterval); + state = STATE_FINDING; + } + } + } fclose(fp); // ensure you close the file after reading } @@ -84,80 +187,13 @@ fclose(fp); // ensure you close the file after writing } - // you can comment out the writing example to check that the writing has - // worked - when you run it after commenting, it should still open the - // file that exists on the SD card - assuming you didn't delete it! - /////////////////////// Reading from file example //////////////////////// - - // now open file for reading...note the 'r' - fp = fopen("/sd/test.txt", "r"); - if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { - serial.printf("Reading file....\n"); - int i; // create suitable variables to store the data in the file - float value; - - // in this example, we keep reading (using fscanf) until we reach - // the 'end of file'. Note we use the address operator & to write - // to the variables. Also the format of the string must match what - // is in the file - while (fscanf(fp, "%d,%f", &i, &value) != EOF) { - serial.printf("%d,%f\n",i,value); - } - serial.printf("Done.\n"); - fclose(fp); // ensure you close the file after reading - } - - ///////////////// Advanced Reading from file example /////////////////// - - // the previous example just read the values into variables and printed to - // serial, we'll now read files into an array. - - // now open file for reading...note the 'r' - fp = fopen("/sd/test.txt", "r"); - - int n=0; // going to store the number of lines in the file - int *index_array; // pointers to create dynamic arrays later - float *value_array; // note memory will be in heap rather than on the stack - - if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { - serial.printf("Counting lines in file....\n"); - //Since we may not know the - // number of lines in the files ahead of time, we'll first count them - // * means scan but don't save - while (fscanf(fp, "%*d,%*f") != EOF) { - n++; // increment counter when read a line - } - - - serial.printf("Read %d lines\n",n); - serial.printf("Creating dynamic arrays...\n"); - // calloc creates an array and initilises to 0 - // malloc returns unitialised array - diffrent syntax - index_array = (int *)calloc(n, sizeof (int)); - value_array = (float *)calloc(n, sizeof (float)); - - int i=0; - rewind(fp); // 'scrolled' to end of file, so go back to beginning - serial.printf("Reading into arrays...\n"); - while (fscanf(fp, "%d,%f",&index_array[i],&value_array[i]) != EOF) { - i++; // read data into array and increment index - } - serial.printf("Done.\n"); - fclose(fp); // ensure you close the file after reading - } - - // we should now have the data in the arrays, will print to serial to check - for(int i=0; i<n ; i++) { - serial.printf("[%d] %d,%f\n",i,index_array[i],value_array[i]); - } /////////////////////////////////////////////////// serial.printf("End of SD card example\n"); + + t1.attach(&t1out,5); + while(1); } void delete_file(char filename[])