Initial Publish Leaning GPS/SDCARD
Dependencies: FileManager GPSGms6 SDFileSystem mbed
Fork of 2545_SD_Card by
Revision 1:f911149acd35, committed 2016-05-06
- Comitter:
- Lucyjungz
- Date:
- Fri May 06 19:30:02 2016 +0000
- Parent:
- 0:5448330e1a33
- Child:
- 2:c96b02fcb98e
- Commit message:
- Initial Commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPSGms6.cpp Fri May 06 19:30:02 2016 +0000 @@ -0,0 +1,165 @@ +#include "mbed.h" +#include "GPSGms6.h" +#include <string> + +#define GET_GPRMC_SECTION_SIZE(state) (unsigned int)gprms_tbl[state].size +#define GET_GPRMC_VARIABLE_ADDR(state) gprms_tbl[state].p_val +#define GET_GPRMC_NEXT_STATE(state) (GPS_ProcessState)gprms_tbl[state+1].state + + +GPRMC_Data_TypeDef m_gprmc; +GPRMC_Data_TypeDef m_valid_gprmc; +char m_RxBuf[RX_BUF_SIZE]; +bool m_available; +int m_index; +Serial serial_gps(PA_9, PA_10); +GPRMC_Tbl_TypeDef gprms_tbl[] = +{ + // index , section size , variable + {GPS_Process_Start , INVALID_VALUE ,(char *)INVALID_VALUE}, + {GPS_Process_Header , HEADER_SIZE , m_gprmc.header}, + {GPS_Process_Time , GPRMC_TIME_SIZE , m_gprmc.time}, + {GPS_Process_Status , GPRMC_STATUS_SIZE , m_gprmc.status}, + {GPS_Process_Latitude , GPRMC_LATITUDE_SIZE , m_gprmc.latitude}, + {GPS_Process_Latitude_hemis , GPRMC_LATITUDE_HEMI_SIZE , m_gprmc.latitude_hemi}, + {GPS_Process_Longitude , GPRMC_LONGITUDE_SIZE , m_gprmc.longitude}, + {GPS_Process_Longitude_hemis , GPRMC_LONGITUDE_HEMI_SIZE , m_gprmc.longitude_hemi}, + {GPS_Process_Speed , GPRMC_SPEED_SIZE , m_gprmc.speed}, + {GPS_Process_Course , GPRMC_COURSE_SIZE , m_gprmc.course}, + {GPS_Process_Date , GPRMC_DATE_SIZE , m_gprmc.date}, + {GPS_Process_Magnetic , GPRMC_MAGNETIC_SIZE , m_gprmc.magnetic}, + {GPS_Process_Magnetic_Dir , GPRMC_MAGNETIC_DIR_SIZE , m_gprmc.magnetic_dir}, + {GPS_Process_Indicator , GPRMC_INDICATOR_SIZE , m_gprmc.indicator}, + {GPS_Process_Complete ,INVALID_VALUE ,(char *)INVALID_VALUE} + +}; +static GPS_ProcessStatus GPS_ProcessGprmcSection(GPS_ProcessState state,char * buf , unsigned int buf_index,unsigned int buf_size, unsigned int section_size, char * ret_value){ + GPS_ProcessStatus status = GPS_Status_Valid; + if (buf_index >= (buf_size - section_size)){ + status = GPS_Status_NotEnough; + } + else if (buf[buf_index] == ','){ + status = GPS_Status_Empty; + } + else{ + unsigned int idx; + for(idx = 0;idx < section_size;idx++) + { + ret_value[idx] = buf[buf_index + idx]; + } + + } + return status; + +} +static void GPS_ProcessGpsData(char * buf, unsigned int size){ + unsigned int index; + unsigned int adv_index = 0; + GPS_ProcessStatus status = GPS_Status_Valid; + GPS_ProcessState state = GPS_Process_Start; + for(index = 0; index < size;index++) + { + if (state == GPS_Process_Start) + { + if (buf[index] == '$') + { + state = GPS_Process_Header; + } + else + { + continue; + } + } + else if (state == GPS_Process_Header) + { + if (index < (size - HEADER_SIZE)) + { + if (buf[index] == 'G' && + buf[index+1] == 'P' && + buf[index+2] == 'R' && + buf[index+3] == 'M' && + buf[index+4] == 'C' + ){ + unsigned int h_index; + for(h_index = 0;h_index < HEADER_SIZE;h_index++) + { + m_gprmc.header[h_index] = buf[index + h_index]; + } + index += HEADER_SIZE; + state = GPS_Process_Time; + } + } + else + { + break; + } + + } + else + { + status = GPS_ProcessGprmcSection(state, buf ,index, size, GET_GPRMC_SECTION_SIZE(state), GET_GPRMC_VARIABLE_ADDR(state)); + adv_index = GET_GPRMC_SECTION_SIZE(state); + state = GET_GPRMC_NEXT_STATE(state) ; + } + + + if (status == GPS_Status_NotEnough || state == GPS_Process_Complete) + { + break; + } + else if (status == GPS_Status_Valid) + { + index += adv_index; + } + } + + if (m_gprmc.indicator[0] == (char)'A' && + m_gprmc.indicator[0] == (char)'D' && + m_gprmc.indicator[0] == (char)'E' + ) + { + m_available= true; + memcpy(&m_valid_gprmc, &m_gprmc , sizeof(m_gprmc)); + } +} + + +void complete_callback() { + GPS_ProcessGpsData(m_RxBuf, RX_BUF_SIZE); + +} +void byte_callback() { + // Note: you need to actually read from the serial to clear the RX interrupt + //printf("%c", gps.getc()); + m_RxBuf[m_index] = serial_gps.getc(); + //printf("%c", m_RxBuf[m_index]); + m_index++; + if (m_index == RX_BUF_SIZE) + { + m_index = 0; + complete_callback(); + } +// myled = !myled; +} +GPSGms6::GPSGms6() +{ + + m_index = 0; + m_available = false; + + serial_gps.baud(9600); + serial_gps.attach(&byte_callback); +} +GPRMC_Data_TypeDef GPSGms6::latestGPRMC() +{ + return (m_gprmc); +} +GPRMC_Data_TypeDef GPSGms6::validGPRMC() +{ + m_available = false; + return (m_valid_gprmc); +} +bool GPSGms6::available() +{ + return (m_available); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPSGms6.h Fri May 06 19:30:02 2016 +0000 @@ -0,0 +1,114 @@ + + +#define HEADER_SIZE 5 +#define GPRMC_TIME_SIZE 10 +#define GPRMC_STATUS_SIZE 1 +#define GPRMC_LATITUDE_SIZE 9 +#define GPRMC_LATITUDE_HEMI_SIZE 1 +#define GPRMC_LONGITUDE_SIZE 10 +#define GPRMC_LONGITUDE_HEMI_SIZE 1 +#define GPRMC_SPEED_SIZE 5 +#define GPRMC_COURSE_SIZE 5 +#define GPRMC_DATE_SIZE 6 +#define GPRMC_MAGNETIC_SIZE 5 +#define GPRMC_MAGNETIC_DIR_SIZE 1 +#define GPRMC_INDICATOR_SIZE 1 + +#define INVALID_VALUE 0xFFFF +#define RX_BUF_SIZE 100 + +typedef struct +{ + char header[HEADER_SIZE]; + char time[GPRMC_TIME_SIZE]; + char status[GPRMC_TIME_SIZE]; + char latitude[GPRMC_LATITUDE_SIZE]; + char latitude_hemi[GPRMC_LATITUDE_HEMI_SIZE]; + char longitude[GPRMC_LONGITUDE_SIZE]; + char longitude_hemi[GPRMC_LATITUDE_HEMI_SIZE]; + char speed[GPRMC_SPEED_SIZE]; + char course[GPRMC_COURSE_SIZE]; + char date[GPRMC_DATE_SIZE]; + char magnetic[GPRMC_MAGNETIC_SIZE]; + char magnetic_dir[GPRMC_MAGNETIC_DIR_SIZE]; + char indicator[GPRMC_INDICATOR_SIZE]; +} GPRMC_Data_TypeDef; + +typedef enum GPS_ProcessState +{ + GPS_Process_Start = 0, + GPS_Process_Header, + GPS_Process_Time, + GPS_Process_Status, + GPS_Process_Latitude, + GPS_Process_Latitude_hemis, + GPS_Process_Longitude, + GPS_Process_Longitude_hemis, + GPS_Process_Speed, + GPS_Process_Course, + GPS_Process_Date, + GPS_Process_Magnetic, + GPS_Process_Magnetic_Dir, + GPS_Process_Indicator, + GPS_Process_Complete , + GPS_Process_SIZE +} GPS_ProcessState; + +typedef enum GPS_ProcessStatus +{ + GPS_Status_Valid, + GPS_Status_Empty, + GPS_Status_NotEnough, +} GPS_ProcessStatus; + +typedef struct +{ + GPS_ProcessState state; + int size; + char * p_val; +} GPRMC_Tbl_TypeDef; + + + +class GPSGms6 +{ +public: + + GPSGms6(); + + + /** Get Latest GPRMC Data + * + * @returns + * Latest GPRMC Data + */ + GPRMC_Data_TypeDef latestGPRMC(); + + /** Get Valid Data Interval + * + * @returns + * Valid Data Interval + */ + GPRMC_Data_TypeDef validGPRMC(); + + /** Get availability of gprmc + * + * @returns + * 'true' - if data is available + */ + bool available(); + + +private: + // States + + + + //Member variables + + + + //void GPS_ProcessGpsData(char * buf, unsigned int size, char * t, char * d); + //GPS_ProcessStatus GPS_ProcessGprmcSection(GPS_ProcessState state,char * buf , unsigned int buf_index,unsigned int buf_size, unsigned int section_size, char * ret_value); + +}; \ No newline at end of file
--- 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[])