![](/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
Revision 10:43fc07923c7f, committed 2016-05-10
- Comitter:
- Lucyjungz
- Date:
- Tue May 10 09:24:20 2016 +0000
- Parent:
- 9:4d0c81290638
- Commit message:
- Update to Library
Changed in this revision
--- a/FileManager.cpp Mon May 09 11:38:22 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,169 +0,0 @@ -#include "FileManager.h" -#include "SDFileSystem.h" - -char m_GpsInterval[XMLTEXT_SIZE]; -char m_DataInterval[XMLTEXT_SIZE]; -Variable_Data_TypeDef m_varList[MAX_VAR]; -unsigned int m_amountVarList; - -static void removeSpaces(char* s , int size); -static void getXmlText(char *str, char *ret); - - -static void removeSpaces(char* s , int size) -{ - char* cpy = s; // an alias to iterate through s without moving s - char* temp = s; - - for (int i = 0 ; i < size ; i++) { - if (*cpy != ' ') - *temp++ = *cpy; - cpy++; - } - *temp = 0; - return; -} -static 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); -} -void readSetupFile() -{ - // now open file for reading - FILE *fp = fopen(SETUP_FILE_NAME, "r"); - - if (fp == NULL) { // if it can't open the file then print error message - printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); - } 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); - - 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 - } -} -void logGPSData(char date[], char time[]) -{ - FILE *fp = fopen(GPS_LOG_FILE_NAME, "a"); - - if (fp == NULL) { // if it can't open the file then print error message - printf("Error! Unable to open file!\n"); - } else { // opened file so can write - printf("\r\n Writing to Gps Log File...."); - - fprintf(fp, "%s,%s\n",date,time); // print formatted string to file (CSV) - - printf("Done"); - fclose(fp); // ensure you close the file after writing - } -} -void logSystemData(float gps_interval) -{ - FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); - - - if (fp == NULL) { // if it can't open the file then print error message - printf("Error! Unable to open file!\n"); - } else { // opened file so can write - fprintf(fp, "Start Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches - fclose(fp); // ensure you close the file after writing - } -} -void delete_file(char filename[]) -{ - printf("Deleting file '%s'...",filename); - FILE *fp = fopen(filename, "r"); // try and open file - if (fp != NULL) { // if it does open... - fclose(fp); // close it - remove(filename); // and then delete - printf("Done!\n"); - } - // if we can't open it, it doesn't exist and so we can't delete it -} -int GPSInterval() -{ - //Return whether or not CRC is enabled - return atoi( m_GpsInterval ); -} -int DataInterval() -{ - //Return whether or not CRC is enabled - return atoi( m_DataInterval ); -} - -Variable_Data_TypeDef * readVarFile() -{ - // now open file for reading - FILE *fp = fopen(VARIABLE_FILE_NAME, "r"); - - if (fp == NULL) { // if it can't open the file then print error message - printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME); - return NULL; - } else { // opened file so can write - - char buf[1024]; - int index = 0; - memset(m_varList, ' ', sizeof(m_varList)); - while (fgets(buf, sizeof(buf), fp) != NULL) { - if (strstr (buf,VAR_NAME_TAG)) { - getXmlText(buf , m_varList[index].varName); - - } else if (strstr (buf,VAR_ADDR_TAG)) { - getXmlText(buf , m_varList[index].varAddress); - index++; - } - - } - fclose(fp); // ensure you close the file after reading - m_amountVarList = index; - return m_varList; - } -} -int getAmountVarList() -{ - return m_amountVarList; -} -Variable_Data_TypeDef * getVarList() -{ - return m_varList; -} - -
--- a/FileManager.h Mon May 09 11:38:22 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -#define SETUP_FILE_NAME "/sd/RMS_Tester.xml" -#define GPS_LOG_FILE_NAME "/sd/gps.csv" -#define VARIABLE_FILE_NAME "/sd/20160216185627_upload.xml" -#define MINIRMS_LOG_FILE_NAME "/sd/miniRMS.log" - -#define GPS_TAG "<Gps>" -#define DATA_TAG "<Data>" -#define UPDATE_INTERVAL_TAG "<Update_Interval>" -#define VAR_NAME_TAG "<varName>" -#define VAR_ADDR_TAG "<varAddress>" -#define XMLTEXT_SIZE 20 - -#define VAR_NAME_MAX_SIZE 20 -#define VAR_ADDR_MAX_SIZE 10 -#define MAX_VAR 50 - -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; - - -typedef struct { - char varName[VAR_NAME_MAX_SIZE]; - char varAddress[VAR_ADDR_MAX_SIZE+1]; -} Variable_Data_TypeDef; - -void readSetupFile(); -void delete_file(char filename[]); -int GPSInterval(); -int DataInterval(); -void logGPSData(char date[], char time[]); -void logSystemData(float gps_interval); -Variable_Data_TypeDef * readVarFile(); -int getAmountVarList(); -Variable_Data_TypeDef * getVarList();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FileManager.lib Tue May 10 09:24:20 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/nsrwsurasak/code/FileManager/#1f1f2b99756b
--- a/GPSGms6.cpp Mon May 09 11:38:22 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,178 +0,0 @@ -#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; - memset(ret_value,' ', section_size); - } else { - unsigned int idx; -// printf("\r\n state = %d =",state); - 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); - -} -void GPSGms6::start_GPS() -{ - 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); -} -tm GPSGms6::UTCTime() -{ - struct tm t; - if (m_gprmc.date[0] != ' ' && m_gprmc.time[0] != ' ' ) - { - char str[3]; - - memcpy( str, &m_gprmc.time[4], 2 ); - t.tm_sec = atoi(str); - memcpy( str, &m_gprmc.time[2], 2 ); - t.tm_min = atoi(str); - memcpy( str, &m_gprmc.time[0], 2 ); - t.tm_hour = atoi(str); - - memcpy( str, &m_gprmc.date[0], 2 ); - t.tm_mday = atoi(str); - - memcpy( str, &m_gprmc.date[2], 2 ); - t.tm_mon = atoi(str); - - memcpy( str, &m_gprmc.date[4], 2 ); - t.tm_year = atoi(str); - } - return (t); -} \ No newline at end of file
--- a/GPSGms6.h Mon May 09 11:38:22 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,111 +0,0 @@ - - -#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+1]; - char time[GPRMC_TIME_SIZE+1]; - char status[GPRMC_TIME_SIZE+1]; - char latitude[GPRMC_LATITUDE_SIZE+1]; - char latitude_hemi[GPRMC_LATITUDE_HEMI_SIZE+1]; - char longitude[GPRMC_LONGITUDE_SIZE+1]; - char longitude_hemi[GPRMC_LATITUDE_HEMI_SIZE+1]; - char speed[GPRMC_SPEED_SIZE+1]; - char course[GPRMC_COURSE_SIZE+1]; - char date[GPRMC_DATE_SIZE+1]; - char magnetic[GPRMC_MAGNETIC_SIZE+1]; - char magnetic_dir[GPRMC_MAGNETIC_DIR_SIZE+1]; - char indicator[GPRMC_INDICATOR_SIZE+1]; -} 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(); - - void start_GPS(); - tm UTCTime(); - /** 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPSGms6.lib Tue May 10 09:24:20 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/nsrwsurasak/code/GPSGms6/#7ef27b349b37
--- a/main.cpp Mon May 09 11:38:22 2016 +0000 +++ b/main.cpp Tue May 10 09:24:20 2016 +0000 @@ -5,7 +5,7 @@ // Connections to SD card holder on K64F (SPI interface) -SDFileSystem sd(PA_7, PA_6, PA_5, PA_0, "sd"); // MOSI, MISO, SCK, CS +SDFileSystem sd(PA_7, PA_6, PA_5, PA_0, DEVICE_NAME); // MOSI, MISO, SCK, CS Serial serial(USBTX, USBRX); // for PC debug GPSGms6 gps; Timeout t1; @@ -25,14 +25,22 @@ printf("\r\nGps long = %s", gps.latestGPRMC().longitude); printf("\r\nGps indicator = %s", gps.latestGPRMC().indicator); - logGPSData( gps.latestGPRMC().date, gps.latestGPRMC().time); + time_t seconds = time(NULL); + logGPSData( (long) seconds,gps.latestGPRMC().date, gps.latestGPRMC().time); serial.printf("\r\n#### Restart Timer #####"); tm t = gps.UTCTime(); printf("\r\n UTC Time is %d/%d/%d %d:%d", t.tm_mday , t.tm_mon , t.tm_year , t.tm_hour, t.tm_min); - time_t seconds = time(NULL); + printf("\r\nTime as a basic string = %s", ctime(&seconds)); + struct tm * ptm; + + time ( &seconds ); + + ptm = localtime ( &seconds ); + printf ("\r\nCurrent time around the World:"); + printf ("\r\nPhoenix, AZ (U.S.) : %2d:%02d\n %d-%d-%d", (ptm->tm_hour)%24, ptm->tm_min, t.tm_mday , t.tm_mon , t.tm_year); t1.attach(&t1out,gps_interval); } @@ -55,13 +63,23 @@ serial.printf("\r\n first addr name = %s ",var_list[i].varAddress); } + delete_file("/sd/2009-09-28.rtl.csv"); /////////////////////////////////////////////////// serial.printf("\r\n#### End of SD Card Initialization #####"); gps.start_GPS(); t1.attach(&t1out,gps_interval); + set_time(1460279787); // Set RTC time to Sun, 10 Apr 2016 09:16:27 GMT - set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 - while(1); + + while(1){ + + wait(5); + time_t seconds = time(NULL); + seconds = time(NULL); + float varList[] = { 1.0,2.2,3.1}; + logRMSData(seconds,varList, 3); + + } }