Initial Publish Leaning GPS/SDCARD

Dependencies:   FileManager GPSGms6 SDFileSystem mbed

Fork of 2545_SD_Card by Craig Evans

main.cpp

Committer:
Lucyjungz
Date:
2016-05-06
Revision:
1:f911149acd35
Parent:
0:5448330e1a33
Child:
2:c96b02fcb98e

File content as of revision 1:f911149acd35:

/* 2545_SD_Card Example

Example of writing data to SD card.

Based on FTF2014_lab4 Example

https://developer.mbed.org/teams/Freescale/wiki/FTF2014_workshop

Craig A. Evans, University of Leeds, Mar 2016

*/

#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(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;

    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(9600);  // full-speed!
    serial.printf("#### SD Card Example #####\n");
    FILE *fp; // this is our file pointer
    wait(1);

    // Various examples below - can comment out ones you don't need

    /////////////////////// Deleting file example ////////////////////////

    // comment this line out if you don't want to delete the file!
    delete_file("/sd/test.txt");

    ////////////////////// Simple writing example //////////////////////////

    // open file for writing ('w') - creates file if it doesn't exist and overwrites
    // if it does. If you wish to add a score onto a list, then you can
    // append instead 'a'. This will open the file if it exists and start
    // writing at the end. It will create the file if it doesn't exist.
    fp = fopen("/sd/topscore.txt", "w");
    int top_score = 56;  // random example

    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
        serial.printf("Writing to file....");
        fprintf(fp, "%d",top_score); // ensure data type matches
        serial.printf("Done.\n");
        fclose(fp);  // ensure you close the file after writing
    }

    ////////////////////// Simple reading example //////////////////////////

    // now open file for 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);

        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
    }

    ///////////////////// Writing list to file example //////////////////////
    
    // for this example, I'll create some numbers to write to file in a big list
    // a data logger for example will usually append to a file - at a reading
    // at the end rather than creating a new file
    fp = fopen("/sd/test.txt", "a");

    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
        serial.printf("Writing to file....");
        for(int i = 1; i <= 50; i++) {
            float dummy = 1000.0F/i;  // dummy variable
            fprintf(fp, "%d,%f\n",i,dummy);  // print formatted string to file (CSV)
        }
        serial.printf("Done.\n");
        fclose(fp);  // ensure you close the file after writing
    }
    


    ///////////////////////////////////////////////////
    serial.printf("End of SD card example\n");
    
    t1.attach(&t1out,5);
    while(1);
}

void delete_file(char filename[])
{
    serial.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
        serial.printf("Done!\n");
    }
    // if we can't open it, it doesn't exist and so we can't delete it
}